Skip to content

Commit d811afc

Browse files
kunitokiactions-user
authored andcommitted
Update amalgamation file
1 parent 63cbb44 commit d811afc

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

Distribution/LuaBridge/LuaBridge.h

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,49 @@ inline lua_Integer tointeger(lua_State* L, int idx, int* isnum)
335335
#endif
336336
}
337337

338+
/**
339+
* @brief Register main thread, only supported on 5.1.
340+
*/
341+
inline constexpr char main_thread_name[] = "__luabridge_main_thread";
342+
343+
inline void register_main_thread(lua_State* threadL)
344+
{
345+
#if LUA_VERSION_NUM < 502
346+
if (threadL == nullptr)
347+
lua_pushnil(threadL);
348+
else
349+
lua_pushthread(threadL);
350+
351+
lua_setglobal(threadL, main_thread_name);
352+
#else
353+
unused(threadL);
354+
#endif
355+
}
356+
357+
/**
358+
* @brief Get main thread, not supported on 5.1.
359+
*/
360+
inline lua_State* main_thread(lua_State* threadL)
361+
{
362+
#if LUA_VERSION_NUM < 502
363+
lua_getglobal(threadL, main_thread_name);
364+
if (lua_isthread(threadL, -1))
365+
{
366+
auto L = lua_tothread(threadL, -1);
367+
lua_pop(threadL, 1);
368+
return L;
369+
}
370+
assert(false); // Have you forgot to call luabridge::registerMainThread ?
371+
lua_pop(threadL, 1);
372+
return threadL;
373+
#else
374+
lua_rawgeti(threadL, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
375+
lua_State* L = lua_tothread(threadL, -1);
376+
lua_pop(threadL, 1);
377+
return L;
378+
#endif
379+
}
380+
338381
/**
339382
* @brief Get a table value, bypassing metamethods.
340383
*/
@@ -4436,7 +4479,7 @@ class LuaRefBase
44364479
};
44374480

44384481
LuaRefBase(lua_State* L)
4439-
: m_L(L)
4482+
: m_L(main_thread(L))
44404483
{
44414484
}
44424485

@@ -5263,6 +5306,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
52635306
*/
52645307
LuaRef(lua_State* L, int index, FromStack)
52655308
: LuaRefBase(L)
5309+
, m_ref(LUA_NOREF)
52665310
{
52675311
#if LUABRIDGE_SAFE_STACK_CHECKS
52685312
if (! lua_checkstack(m_L, 1))
@@ -5284,6 +5328,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
52845328
*/
52855329
LuaRef(lua_State* L)
52865330
: LuaRefBase(L)
5331+
, m_ref(LUA_NOREF)
52875332
{
52885333
}
52895334

@@ -5297,6 +5342,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
52975342
template <class T>
52985343
LuaRef(lua_State* L, const T& v)
52995344
: LuaRefBase(L)
5345+
, m_ref(LUA_NOREF)
53005346
{
53015347
std::error_code ec;
53025348
if (! Stack<T>::push(m_L, v, ec))
@@ -7898,6 +7944,21 @@ inline Namespace getNamespaceFromStack(lua_State* L)
78987944
return Namespace::getNamespaceFromStack(L);
78997945
}
79007946

7947+
//=================================================================================================
7948+
/**
7949+
* @brief Registers main thread.
7950+
*
7951+
* This is a backward compatibility mitigation for lua 5.1 not supporting LUA_RIDX_MAINTHREAD.
7952+
*
7953+
* @param L The main Lua state that will be regstered as main thread.
7954+
*
7955+
* @returns A namespace registration object.
7956+
*/
7957+
inline void registerMainThread(lua_State* L)
7958+
{
7959+
register_main_thread(L);
7960+
}
7961+
79017962
} // namespace luabridge
79027963

79037964
// End File: Source/LuaBridge/detail/Namespace.h

0 commit comments

Comments
 (0)