From 37f9824273fc3233f84bcff4ecfe1132dad74185 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 23 Nov 2018 21:27:56 +0800 Subject: =?UTF-8?q?*=E6=9B=B4=E6=96=B0=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/common/je_lua.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/lua/common/je_lua.cpp') diff --git a/src/lua/common/je_lua.cpp b/src/lua/common/je_lua.cpp index ad3f756..d53898d 100644 --- a/src/lua/common/je_lua.cpp +++ b/src/lua/common/je_lua.cpp @@ -115,5 +115,62 @@ namespace JinEngine return 1; } + void luax_getreference(lua_State* L, SharedBase* shared) + { + luax_getreferencestable(L); + luax_pushlightuserdata(L, shared); + luax_gettable(L, -2); + luax_remove(L, -2); + } + + bool luax_addreference(lua_State* L, SharedBase* shared, SharedBase* dep) + { + luax_getreference(L, shared); + // If no dependencies table, add one. + if (luax_isnil(L, -1)) + { + luax_pop(L, 1); + luax_getreferencestable(L); + luax_newtable(L); + luax_pushlightuserdata(L, shared); + luax_pushvalue(L, -2); + luax_settable(L, -4); + luax_remove(L, -2); // Remove references table. + } + luax_pushlightuserdata(L, dep); + luax_getobject(L, dep); + if (luax_isnil(L, -1)) + { + luax_pop(L, 3); // Pop nil, dep, reftbl + return false; + } + luax_settable(L, -3); + luax_pop(L, 1); + return true; + } + + void luax_removereference(lua_State* L, SharedBase* shared) + { + luax_getreferencestable(L); + luax_pushlightuserdata(L, shared); + luax_pushnil(L); + luax_settable(L, -3); + luax_pop(L, 1); + } + + void luax_removereference(lua_State* L, SharedBase* shared, SharedBase* dep) + { + luax_getreference(L, shared); + if (luax_isnil(L, -1)) + { + luax_pop(L, 1); + return; + } + luax_pushlightuserdata(L, dep); + luax_pushnil(L); + luax_settable(L, -3); + luax_pop(L, 1); + } + } } \ No newline at end of file -- cgit v1.1-26-g67d0