diff --git a/LuaIntf/LuaCompat.h b/LuaIntf/LuaCompat.h index 5f3a1ff..ca9c55e 100644 --- a/LuaIntf/LuaCompat.h +++ b/LuaIntf/LuaCompat.h @@ -222,6 +222,9 @@ int luaL_fileresult(lua_State* L, int stat, const char* fname); #endif +void lua_register_mainthread(lua_State* L); +lua_State* lua_get_mainthread(lua_State* L); + //--------------------------------------------------------------------------- #if LUAINTF_HEADERS_ONLY diff --git a/LuaIntf/LuaRef.h b/LuaIntf/LuaRef.h index 1e8838b..8aedc65 100644 --- a/LuaIntf/LuaRef.h +++ b/LuaIntf/LuaRef.h @@ -420,7 +420,7 @@ class LuaRef * Create reference to Lua nil. */ LuaRef(lua_State* state, std::nullptr_t) - : L(state) + : L(lua_get_mainthread(state)) , m_ref(LUA_REFNIL) { assert(L); @@ -433,7 +433,7 @@ class LuaRef * @param index position on stack */ LuaRef(lua_State* state, int index) - : L(state) + : L(lua_get_mainthread(state)) { assert(L); lua_pushvalue(L, index); @@ -447,7 +447,7 @@ class LuaRef * @param name the global name, may contains '.' to access sub obejct */ LuaRef(lua_State* state, const char* name) - : L(state) + : L(lua_get_mainthread(state)) { assert(L); Lua::pushGlobal(L, name); @@ -1190,7 +1190,7 @@ class LuaRef * Special constructor for popFromStack. */ explicit LuaRef(lua_State* state) - : L(state) + : L(lua_get_mainthread(state)) { assert(L); m_ref = luaL_ref(state, LUA_REGISTRYINDEX); @@ -1234,7 +1234,7 @@ class LuaRef static R invoke(lua_State* L, const LuaRef& f, P&&... args) { lua_pushcfunction(L, &LuaException::traceback); - f.pushToStack(); + f.pushToStack(L); pushArg(L, std::forward

(args)...); if (lua_pcall(L, sizeof...(P), 1, -int(sizeof...(P) + 2)) != LUA_OK) { lua_remove(L, -2); @@ -1252,7 +1252,7 @@ class LuaRef static void invoke(lua_State* L, const LuaRef& f, P&&... args) { lua_pushcfunction(L, &LuaException::traceback); - f.pushToStack(); + f.pushToStack(L); pushArg(L, std::forward

(args)...); if (lua_pcall(L, sizeof...(P), 0, -int(sizeof...(P) + 2)) != LUA_OK) { lua_remove(L, -2); @@ -1268,7 +1268,7 @@ class LuaRef static std::tuple invoke(lua_State* L, const LuaRef& f, P&&... args) { lua_pushcfunction(L, &LuaException::traceback); - f.pushToStack(); + f.pushToStack(L); pushArg(L, std::forward

(args)...); if (lua_pcall(L, sizeof...(P), sizeof...(R), -int(sizeof...(P) + 2)) != LUA_OK) { lua_remove(L, -2); diff --git a/LuaIntf/impl/CppBindModule.h b/LuaIntf/impl/CppBindModule.h index 477b812..f4d131a 100644 --- a/LuaIntf/impl/CppBindModule.h +++ b/LuaIntf/impl/CppBindModule.h @@ -584,7 +584,7 @@ class LuaBinding * LuaRef mod = LuaRef::createTable(L); * LuaBinding(mod) * ...; - * mod.pushToStack(); + * mod.pushToStack(L); * return 1; * } */ diff --git a/LuaIntf/impl/CppFunction.h b/LuaIntf/impl/CppFunction.h index 541a231..bf30dad 100644 --- a/LuaIntf/impl/CppFunction.h +++ b/LuaIntf/impl/CppFunction.h @@ -136,7 +136,7 @@ struct LuaCppFunction { using CppProc = CppBindMethod; LuaRef ref = LuaRef::createUserDataFrom(L, proc); - ref.pushToStack(); + ref.pushToStack(L); lua_pushlightuserdata(L, CppSignature::value()); lua_pushcclosure(L, &CppProc::call, 2); } diff --git a/LuaIntf/src/LuaCompat.cpp b/LuaIntf/src/LuaCompat.cpp index 23f18e5..67ecc60 100644 --- a/LuaIntf/src/LuaCompat.cpp +++ b/LuaIntf/src/LuaCompat.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #define LUA_PACKAGE_KEY "_LUAINTF_COMPAT" @@ -460,6 +461,33 @@ LUA_INLINE int luaL_fileresult(lua_State* L, int stat, const char* fname) } } +#if LUA_VERSION_NUM <= 501 +#define LUA_RIDX_MAINTHREAD -3 +#endif + +void lua_register_mainthread(lua_State* L) { +#if LUA_VERSION_NUM <= 501 + if (lua_pushthread(L) == 1) + lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); + else + lua_pop(L, 1); +#endif +} + +lua_State* lua_get_mainthread(lua_State* L) { + assert(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); + lua_State* L1 = lua_tothread(L, -1); + lua_pop(L, 1); + if (!L1) { + lua_register_mainthread(L); + lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); + L1 = lua_tothread(L, -1); + lua_pop(L, 1); + } + return L1 ? L1 : L; +} + //--------------------------------------------------------------------------- #endif