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 (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