From d162ae41059d9b251148193849724865cfd2d3ef Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Wed, 24 Feb 2016 23:01:11 +0200 Subject: [PATCH] Move objects from global table to internal registery --- ElunaTemplate.h | 13 ++++++++----- LuaEngine.cpp | 5 +++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/ElunaTemplate.h b/ElunaTemplate.h index a000dbf..ce821ee 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -148,7 +148,7 @@ public: manageMemory = gc; // create metatable for userdata of this type - lua_newtable(E->L); + luaL_newmetatable(E->L, tname); int metatable = lua_gettop(E->L); // push methodtable to stack to be accessed and modified by users @@ -239,7 +239,8 @@ public: ASSERT(methodTable); // get metatable - lua_getglobal(E->L, tname); + lua_pushstring(E->L, tname); + lua_rawget(E->L, LUA_REGISTRYINDEX); ASSERT(lua_istable(E->L, -1)); for (; methodTable && methodTable->name && methodTable->mfunc; ++methodTable) @@ -264,7 +265,8 @@ public: void* obj_voidptr = static_cast(const_cast(obj)); - lua_getglobal(L, ELUNA_OBJECT_STORE); + lua_pushstring(L, ELUNA_OBJECT_STORE); + lua_rawget(L, LUA_REGISTRYINDEX); ASSERT(lua_istable(L, -1)); lua_pushlightuserdata(L, obj_voidptr); lua_rawget(L, -2); @@ -292,7 +294,8 @@ public: *ptrHold = new ElunaObject(const_cast(obj), manageMemory); // Set metatable for it - lua_getglobal(L, tname); + lua_pushstring(L, tname); + lua_rawget(L, LUA_REGISTRYINDEX); if (!lua_istable(L, -1)) { ELUNA_LOG_ERROR("%s missing metatable", tname); @@ -384,7 +387,7 @@ public: static int ToString(lua_State* L) { T* obj = Eluna::CHECKOBJ(L, 1, true); // get self - lua_pushfstring(L, "%s: (%p)", tname, obj); + lua_pushfstring(L, "%s: %p", tname, obj); return 1; } diff --git a/LuaEngine.cpp b/LuaEngine.cpp index 779e6af..d237bb4 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -242,7 +242,7 @@ void Eluna::OpenLua() lua_pushstring(L, "v"); lua_setfield(L, -2, "__mode"); lua_setmetatable(L, -2); - lua_setglobal(L, ELUNA_OBJECT_STORE); + lua_setfield(L, LUA_REGISTRYINDEX, ELUNA_OBJECT_STORE); // Set lua require folder paths (scripts folder structure) lua_getglobal(L, "package"); @@ -534,7 +534,8 @@ void Eluna::RunScripts() void Eluna::InvalidateObjects() { - lua_getglobal(L, ELUNA_OBJECT_STORE); + lua_pushstring(L, ELUNA_OBJECT_STORE); + lua_rawget(L, LUA_REGISTRYINDEX); ASSERT(lua_istable(L, -1)); lua_pushnil(L);