|
John Dunn:
When the coroutine version is running L is not the main Lua state but the one from the coroutine making the trigger method calling "function() coroutine.resume(co) end" on the same thread as co.static int timer_call_after(lua_State* L) { std::cout << __FUNCTION__ << std::endl; timer_t** timer = (timer_t**)lua_newuserdata(L, sizeof(timer_t*)); *timer = new timer_t(); (*timer)->L = L;
In the coroutine case the callback is called on the co thread and resumes it with a wrong stack layout.// store function lua_pushvalue(L, 1); (*timer)->handler_index = luaL_ref(L, LUA_REGISTRYINDEX);; // stash timer the_timer = *timer; return 0; } const char* bad_lua = R"XXX( function wait(time) co = coroutine.running() Timer.CallAfter(function() coroutine.resume(co) end, time)
Instead of using the current Lua state with the timer the main Lua state should call the callback.return coroutine.yield() end
Regards, Xmilia