umka-lang icon indicating copy to clipboard operation
umka-lang copied to clipboard

Umka->C->Umka call chains are impossible

Open vtereshkov opened this issue 3 years ago • 2 comments

C source:

// fwd.c
#include "umka_api.h"

void umproc(UmkaStackSlot *param, UmkaStackSlot *result) 
{
  int n = param[0].intVal;
  void *umka = result->ptrVal;

  const int callback = umkaGetFunc(umka, NULL, "callback");

  int sum = 0;
  for (int i = 0; i < n; i++)
  {
    UmkaStackSlot callbackParam[] = {{.intVal = i}};
    UmkaStackSlot callbackResult;
    umkaCall(umka, callback, 1, callbackParam, &callbackResult);
    sum += callbackResult.intVal;
  }

  result->intVal = sum;
}

Umka source:

// fwd.um
fn umproc*(n: int): int // implemented in C

fn callback(x: int): int {
  printf("x = %d\n", x)
  return 2 * x
}

fn main() {
  printf("main\n")
  sum := umproc(3)
  printf("sum = %d\n", sum)
}

Build command (MinGW):

gcc fwd.c -o fwd.umi -shared -static -Wl,--dll -lumka -L%CD%

Run command:

umka fwd.um

Output:

main
x = 0
x = 1
x = 2
main
x = 0
x = 1
x = 2
main
x = 0
x = 1
x = 2
...

vtereshkov avatar Dec 10 '22 23:12 vtereshkov

In contrast, a Lua->C->Lua call chain is quite possible:

int l_loop (lua_State *L) {
	for (;;) {
		lua_pushvalue(L, 1);
		lua_call(L, 0, 1);
		if (lua_isnil(L, -1)) break;
		printf("%s\n", lua_tostring(L, -1));
		lua_pop(L, 1);
	}
	return 0;
}

(Example is taken from https://www.lua.org/doc/hopl.pdf)

vtereshkov avatar Jun 19 '23 23:06 vtereshkov

The problem is not that trivial, as such call chains imply recursive calls to vmLoop in the current Umka implementation.

vtereshkov avatar Jun 29 '23 00:06 vtereshkov