int resume_continuation_func(lua_State *co, int status, lua_KContext ctx) {
int res;
printf("gggggggggggggggggggggggg\n");
if (status != LUA_OK && status != LUA_YIELD) {
luaL_error(co, "Error resuming testMe function: %s\n", lua_tostring(co, -1));
exit(1);
}
int nres = 0;
res = lua_resume(co, (lua_State *) ctx, 0, &nres);
if (res != LUA_OK) {
luaL_error(co, "Error ressuming testMe function. %s \n", lua_tostring(co, -1));
exit(1);
}
return 0;
}
static int foreach(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TFUNCTION);
printf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n");
lua_State *co = lua_newthread(L);
lua_pushvalue(L, 2);
lua_xmove(L, co, 1);
lua_pushvalue(L, 1);
lua_xmove(L, co, 1);
int res = lua_pcallk(co, 1, 0, 0, (lua_KContext)L, resume_continuation_func);
if (res != LUA_OK && res != LUA_YIELD) {
luaL_error(L, "Error calling function: %s\n", lua_tostring(L, -1));
exit(1);
}
printf("aaaaaaaaaaaaaaaaaaaaaaaaa\n");
// Don't close the thread here, as it may still be in use
return 0;
}
int main(void) {
int res;
lua_State *L = luaL_newstate();
if (L == NULL) luaL_error(L, "Error creating new state. %s\n", lua_tostring(L, -1));
luaL_openlibs(L);
lua_pushcfunction(L, foreach);
lua_setglobal(L, "foreach");
res = luaL_dofile(L, "test_lua_continuation.lua");
if (res != LUA_OK) {
luaL_error(L, "Error running test_lua_continuation: %s\n", lua_tostring(L, -1));
exit(1);
}
// Close Lua state when done
lua_close(L);
return 0;
}
test_lua_continuation.lua file contains:
local testMe = function(k, v)
if (v == 20) then
print('aaaaddddaaaaddd')
coroutine.yield()
print(k, v)
end
end
foreach({x = 10, y = 20}, function(tbl)
for k, v in pairs(tbl) do
testMe(k, v)
end
end)
I get :
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
aaaaddddaaaaddd
it cannot get into resume_continuation_func. I did whatever doc says, i can not get into the continuation function after yielding, this is one of my tryings, any sugg wld be great. I need this following pattern to extend lua's functionality.