Skip to content

Commit

Permalink
Update amalgamation file
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki authored and actions-user committed Sep 26, 2022
1 parent 63cbb44 commit d811afc
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion Distribution/LuaBridge/LuaBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,49 @@ inline lua_Integer tointeger(lua_State* L, int idx, int* isnum)
#endif
}

/**
* @brief Register main thread, only supported on 5.1.
*/
inline constexpr char main_thread_name[] = "__luabridge_main_thread";

inline void register_main_thread(lua_State* threadL)
{
#if LUA_VERSION_NUM < 502
if (threadL == nullptr)
lua_pushnil(threadL);
else
lua_pushthread(threadL);

lua_setglobal(threadL, main_thread_name);
#else
unused(threadL);
#endif
}

/**
* @brief Get main thread, not supported on 5.1.
*/
inline lua_State* main_thread(lua_State* threadL)
{
#if LUA_VERSION_NUM < 502
lua_getglobal(threadL, main_thread_name);
if (lua_isthread(threadL, -1))
{
auto L = lua_tothread(threadL, -1);
lua_pop(threadL, 1);
return L;
}
assert(false); // Have you forgot to call luabridge::registerMainThread ?
lua_pop(threadL, 1);
return threadL;
#else
lua_rawgeti(threadL, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
lua_State* L = lua_tothread(threadL, -1);
lua_pop(threadL, 1);
return L;
#endif
}

/**
* @brief Get a table value, bypassing metamethods.
*/
Expand Down Expand Up @@ -4436,7 +4479,7 @@ class LuaRefBase
};

LuaRefBase(lua_State* L)
: m_L(L)
: m_L(main_thread(L))
{
}

Expand Down Expand Up @@ -5263,6 +5306,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
*/
LuaRef(lua_State* L, int index, FromStack)
: LuaRefBase(L)
, m_ref(LUA_NOREF)
{
#if LUABRIDGE_SAFE_STACK_CHECKS
if (! lua_checkstack(m_L, 1))
Expand All @@ -5284,6 +5328,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
*/
LuaRef(lua_State* L)
: LuaRefBase(L)
, m_ref(LUA_NOREF)
{
}

Expand All @@ -5297,6 +5342,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
template <class T>
LuaRef(lua_State* L, const T& v)
: LuaRefBase(L)
, m_ref(LUA_NOREF)
{
std::error_code ec;
if (! Stack<T>::push(m_L, v, ec))
Expand Down Expand Up @@ -7898,6 +7944,21 @@ inline Namespace getNamespaceFromStack(lua_State* L)
return Namespace::getNamespaceFromStack(L);
}

//=================================================================================================
/**
* @brief Registers main thread.
*
* This is a backward compatibility mitigation for lua 5.1 not supporting LUA_RIDX_MAINTHREAD.
*
* @param L The main Lua state that will be regstered as main thread.
*
* @returns A namespace registration object.
*/
inline void registerMainThread(lua_State* L)
{
register_main_thread(L);
}

} // namespace luabridge

// End File: Source/LuaBridge/detail/Namespace.h
Expand Down

0 comments on commit d811afc

Please sign in to comment.