Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Umka->C->Umka call chains are impossible #248

Open
vtereshkov opened this issue Dec 10, 2022 · 2 comments
Open

Umka->C->Umka call chains are impossible #248

vtereshkov opened this issue Dec 10, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@vtereshkov
Copy link
Owner

vtereshkov commented Dec 10, 2022

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 vtereshkov added the bug Something isn't working label Dec 10, 2022
@vtereshkov vtereshkov changed the title Forwarding a C callback to Umka causes the Umka script to restart Umka->C->Umka call chains are impossible Jun 19, 2023
@vtereshkov
Copy link
Owner Author

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
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant