From f7ac800d7e4778ad5a8651b9af1ed28501188a2a Mon Sep 17 00:00:00 2001 From: cracyc Date: Tue, 27 Mar 2018 21:04:22 -0500 Subject: luaegine: save a ref to waiting coroutine to prevent it from being gc'd (nw) --- src/frontend/mame/luaengine.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index 0711f5efc2d..e8e45bbf2fd 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -799,7 +799,11 @@ void lua_engine::initialize() emu["wait"] = lua_CFunction([](lua_State *L) { lua_engine *engine = mame_machine_manager::instance()->lua(); luaL_argcheck(L, lua_isnumber(L, 1), 1, "waiting duration expected"); - engine->machine().scheduler().timer_set(attotime::from_double(lua_tonumber(L, 1)), timer_expired_delegate(FUNC(lua_engine::resume), engine), 0, L); + int ret = lua_pushthread(L); + if(ret == 1) + return luaL_error(L, "cannot wait from outside coroutine"); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + engine->machine().scheduler().timer_set(attotime::from_double(lua_tonumber(L, 1)), timer_expired_delegate(FUNC(lua_engine::resume), engine), ref, nullptr); return lua_yield(L, 0); }); emu["lang_translate"] = &lang_translate; @@ -2021,13 +2025,16 @@ void lua_engine::close() void lua_engine::resume(void *ptr, int nparam) { - lua_State *L = static_cast(ptr); + lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, nparam); + lua_State *L = lua_tothread(m_lua_state, -1); + lua_pop(m_lua_state, 1); int stat = lua_resume(L, nullptr, 0); if((stat != LUA_OK) && (stat != LUA_YIELD)) { osd_printf_error("[LUA ERROR] in resume: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); } + luaL_unref(m_lua_state, LUA_REGISTRYINDEX, nparam); } void lua_engine::run(sol::load_result res) -- cgit v1.2.3