Skip to content

Commit

Permalink
store mainthread in LuaRef
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Aug 20, 2016
1 parent 8bf404e commit c3020ef
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 3 additions & 0 deletions LuaIntf/LuaCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions LuaIntf/LuaRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<P>(args)...);
if (lua_pcall(L, sizeof...(P), 1, -int(sizeof...(P) + 2)) != LUA_OK) {
lua_remove(L, -2);
Expand All @@ -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<P>(args)...);
if (lua_pcall(L, sizeof...(P), 0, -int(sizeof...(P) + 2)) != LUA_OK) {
lua_remove(L, -2);
Expand All @@ -1268,7 +1268,7 @@ class LuaRef
static std::tuple<R...> invoke(lua_State* L, const LuaRef& f, P&&... args)
{
lua_pushcfunction(L, &LuaException::traceback);
f.pushToStack();
f.pushToStack(L);
pushArg(L, std::forward<P>(args)...);
if (lua_pcall(L, sizeof...(P), sizeof...(R), -int(sizeof...(P) + 2)) != LUA_OK) {
lua_remove(L, -2);
Expand Down
2 changes: 1 addition & 1 deletion LuaIntf/impl/CppBindModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class LuaBinding
* LuaRef mod = LuaRef::createTable(L);
* LuaBinding(mod)
* ...;
* mod.pushToStack();
* mod.pushToStack(L);
* return 1;
* }
*/
Expand Down
2 changes: 1 addition & 1 deletion LuaIntf/impl/CppFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct LuaCppFunction
{
using CppProc = CppBindMethod<FN>;
LuaRef ref = LuaRef::createUserDataFrom(L, proc);
ref.pushToStack();
ref.pushToStack(L);
lua_pushlightuserdata(L, CppSignature<FN>::value());
lua_pushcclosure(L, &CppProc::call, 2);
}
Expand Down
28 changes: 28 additions & 0 deletions LuaIntf/src/LuaCompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <assert.h>

#define LUA_PACKAGE_KEY "_LUAINTF_COMPAT"

Expand Down Expand Up @@ -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

0 comments on commit c3020ef

Please sign in to comment.