lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

 John Dunn:

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;
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.
   // 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)
In the coroutine case the callback is called on the co thread and resumes it with a wrong stack layout.
   return coroutine.yield()
Instead of using the current Lua state with the timer the main Lua state should call the callback.