mirror of
https://github.com/love2d/love.git
synced 2026-08-14 01:20:56 +02:00
Move the references from _G to the registry
This commit is contained in:
@@ -22,6 +22,8 @@
|
||||
|
||||
namespace love
|
||||
{
|
||||
const char REFERENCE_TABLE_NAME[] = "love-references";
|
||||
|
||||
Reference::Reference()
|
||||
: L(0), idx(LUA_REFNIL)
|
||||
{
|
||||
@@ -42,14 +44,19 @@ namespace love
|
||||
{
|
||||
unref(); // Just to be safe.
|
||||
this->L = L;
|
||||
idx = luaL_ref(L, LUA_GLOBALSINDEX);
|
||||
luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME);
|
||||
lua_insert(L, -2); // Move reference table behind value.
|
||||
idx = luaL_ref(L, -2);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
void Reference::unref()
|
||||
{
|
||||
if (idx != LUA_REFNIL)
|
||||
{
|
||||
luaL_unref(L, LUA_GLOBALSINDEX, idx);
|
||||
luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME);
|
||||
luaL_unref(L, -1, idx);
|
||||
lua_pop(L, 1);
|
||||
idx = LUA_REFNIL;
|
||||
}
|
||||
}
|
||||
@@ -57,7 +64,11 @@ namespace love
|
||||
void Reference::push()
|
||||
{
|
||||
if (idx != LUA_REFNIL)
|
||||
lua_rawgeti(L, LUA_GLOBALSINDEX, idx);
|
||||
{
|
||||
luax_insist(L, LUA_REGISTRYINDEX, REFERENCE_TABLE_NAME);
|
||||
lua_rawgeti(L, -1, idx);
|
||||
lua_remove(L, -2);
|
||||
}
|
||||
else
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user