diff options
Diffstat (limited to 'src/libjin-lua/common/l_runtime.cpp')
-rw-r--r-- | src/libjin-lua/common/l_runtime.cpp | 408 |
1 files changed, 204 insertions, 204 deletions
diff --git a/src/libjin-lua/common/l_runtime.cpp b/src/libjin-lua/common/l_runtime.cpp index 0972ee5..d57259f 100644 --- a/src/libjin-lua/common/l_runtime.cpp +++ b/src/libjin-lua/common/l_runtime.cpp @@ -8,208 +8,208 @@ using namespace JinEngine::Math; namespace JinEngine { - namespace Lua - { - - static const char* Jin_Lua_Objects_Table = "Jin_Objects_Table"; - - static const char* Jin_Lua_Reference_Table = "Jin_Reference_Table"; - - static const char* Jin_Lua_Modules_Table = "Jin_Modules_Table"; - - using DepsMap = LuaObject::DepsMap; - - LuaObject* luax_newinstance(lua_State* L, const char* type, Shared* shared) - { - LuaObject* obj = static_cast<LuaObject*>(luax_newinstance(L, type, sizeof(LuaObject))); - obj->state = L; - obj->type = type; - obj->bind(shared); - // Add to objects_table, objects_table[shared] = luaObj - luax_getobjectstable(L); - luax_pushlightuserdata(L, shared); - luax_pushvalue(L, -3); - luax_settable(L, -3); - luax_pop(L, 1); // Pop objects table. - return obj; - } - - LuaObject* luax_checkobject(lua_State* L, int idx, const char* type) - { - return (LuaObject*)luax_checktype(L, idx, type); - } - - LuaObject* luax_copyinstance(lua_State* to, LuaObject* src) - { - if (to == src->state) - return nullptr; - // Copy dependencies. - DepsMap& srcDeps = *src->dependencies; - for (DepsMap::iterator it = srcDeps.begin(); it != srcDeps.end(); ++it) - { - LuaObject* obj = it->second; - Shared* shr = obj->shared; - // Try get lua object. - luax_getobject(src->state, obj); - LuaObject* luaObj = (LuaObject*)luax_checktype(src->state, -1, obj->getObjectType()); - luax_pop(src->state, 1); // Pop lua object. - luax_copyinstance(to, luaObj); - luax_pop(to, 1); // Pop reference object. - } - Shared* shr = src->getShared(); - LuaObject* obj = luax_newinstance(to, src->getObjectType(), shr); - (*obj->dependencies) = (*src->dependencies); - // Add to objects_table. - luax_getobjectstable(to); - luax_pushlightuserdata(to, shr); - luax_pushvalue(to, -3); - luax_settable(to, -3); - luax_pop(to, 1); // Pop objects table. - // Set dependencies. - DepsMap& deps = *obj->dependencies; - DepsMap::iterator it = deps.begin(); - for (; it != deps.end(); ++it) - { - LuaObject* dep = it->second; - luax_addreference(to, src, dep); - } - return obj; - } - - int luax_getobject(lua_State* L, LuaObject* obj) - { - luax_getobjectstable(L); - luax_pushlightuserdata(L, obj->shared); - luax_gettable(L, -2); - luax_remove(L, -2); // Remove objects table on stack. - return 1; - } - - void luax_removeobject(lua_State* L, LuaObject* obj) - { - luax_getobjectstable(L); - luax_pushlightuserdata(L, obj->shared); - luax_pushnil(L); - luax_settable(L, -3); - luax_pop(L, 1); - } - - int luax_getobjectstable(lua_State* L) - { - luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Objects_Table); - // If no such table, add one. - if (luax_isnil(L, -1) || !luax_istable(L, -1)) - { - luax_pop(L, 1); - luax_newtable(L); - - // metatable - luax_newtable(L); - // weak table - luax_pushliteral(L, "v"); - luax_setfield(L, -2, "__mode"); - // setmetatable(newtable, metatable) - luax_setmetatable(L, -2); - - luax_pushvalue(L, -1); - luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Objects_Table); - } - return 1; - } - - int luax_getmodulestable(lua_State* L) - { - luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Modules_Table); - // If no such table, add one. - if (luax_isnil(L, -1) || !luax_istable(L, -1)) - { - luax_pop(L, 1); - luax_newtable(L); - - // metatable - luax_newtable(L); - // weak table - luax_pushliteral(L, "v"); - luax_setfield(L, -2, "__mode"); - // setmetatable(newtable, metatable) - luax_setmetatable(L, -2); - - luax_pushvalue(L, -1); - luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Modules_Table); - } - return 1; - } - - int luax_getreferencestable(lua_State* L) - { - luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Reference_Table); - // If no such table, add one. - if (luax_isnil(L, -1) || !luax_istable(L, -1)) - { - luax_pop(L, 1); - luax_newtable(L); - luax_pushvalue(L, -1); - luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Reference_Table); - } - return 1; - } - - void luax_getreference(lua_State* L, LuaObject* obj) - { - luax_getreferencestable(L); - luax_pushlightuserdata(L, obj->shared); - luax_gettable(L, -2); - luax_remove(L, -2); - } - - bool luax_addreference(lua_State* L, LuaObject* obj, LuaObject* dep) - { - luax_getreference(L, obj); - // If no dependencies table, add one. - if (luax_isnil(L, -1)) - { - luax_pop(L, 1); - luax_getreferencestable(L); - luax_newtable(L); - luax_pushlightuserdata(L, obj->shared); - luax_pushvalue(L, -2); - luax_settable(L, -4); - luax_remove(L, -2); // Remove references table. - } - luax_pushlightuserdata(L, dep->shared); - 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, LuaObject* obj) - { - luax_getreferencestable(L); - luax_pushlightuserdata(L, obj->shared); - luax_pushnil(L); - luax_settable(L, -3); - luax_pop(L, 1); - } - - void luax_removereference(lua_State* L, LuaObject* obj, LuaObject* dep) - { - luax_getreference(L, obj); - if (luax_isnil(L, -1)) - { - luax_pop(L, 1); - return; - } - luax_pushlightuserdata(L, dep->shared); - luax_pushnil(L); - luax_settable(L, -3); - luax_pop(L, 1); - } - - } + namespace Lua + { + + static const char* Jin_Lua_Objects_Table = "Jin_Objects_Table"; + + static const char* Jin_Lua_Reference_Table = "Jin_Reference_Table"; + + static const char* Jin_Lua_Modules_Table = "Jin_Modules_Table"; + + using DepsMap = LuaObject::DepsMap; + + LuaObject* luax_newinstance(lua_State* L, const char* type, Shared* shared) + { + LuaObject* obj = static_cast<LuaObject*>(luax_newinstance(L, type, sizeof(LuaObject))); + obj->state = L; + obj->type = type; + obj->bind(shared); + // Add to objects_table, objects_table[shared] = luaObj + luax_getobjectstable(L); + luax_pushlightuserdata(L, shared); + luax_pushvalue(L, -3); + luax_settable(L, -3); + luax_pop(L, 1); // Pop objects table. + return obj; + } + + LuaObject* luax_checkobject(lua_State* L, int idx, const char* type) + { + return (LuaObject*)luax_checktype(L, idx, type); + } + + LuaObject* luax_copyinstance(lua_State* to, LuaObject* src) + { + if (to == src->state) + return nullptr; + // Copy dependencies. + DepsMap& srcDeps = *src->dependencies; + for (DepsMap::iterator it = srcDeps.begin(); it != srcDeps.end(); ++it) + { + LuaObject* obj = it->second; + Shared* shr = obj->shared; + // Try get lua object. + luax_getobject(src->state, obj); + LuaObject* luaObj = (LuaObject*)luax_checktype(src->state, -1, obj->getObjectType()); + luax_pop(src->state, 1); // Pop lua object. + luax_copyinstance(to, luaObj); + luax_pop(to, 1); // Pop reference object. + } + Shared* shr = src->getShared(); + LuaObject* obj = luax_newinstance(to, src->getObjectType(), shr); + (*obj->dependencies) = (*src->dependencies); + // Add to objects_table. + luax_getobjectstable(to); + luax_pushlightuserdata(to, shr); + luax_pushvalue(to, -3); + luax_settable(to, -3); + luax_pop(to, 1); // Pop objects table. + // Set dependencies. + DepsMap& deps = *obj->dependencies; + DepsMap::iterator it = deps.begin(); + for (; it != deps.end(); ++it) + { + LuaObject* dep = it->second; + luax_addreference(to, src, dep); + } + return obj; + } + + int luax_getobject(lua_State* L, LuaObject* obj) + { + luax_getobjectstable(L); + luax_pushlightuserdata(L, obj->shared); + luax_gettable(L, -2); + luax_remove(L, -2); // Remove objects table on stack. + return 1; + } + + void luax_removeobject(lua_State* L, LuaObject* obj) + { + luax_getobjectstable(L); + luax_pushlightuserdata(L, obj->shared); + luax_pushnil(L); + luax_settable(L, -3); + luax_pop(L, 1); + } + + int luax_getobjectstable(lua_State* L) + { + luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Objects_Table); + // If no such table, add one. + if (luax_isnil(L, -1) || !luax_istable(L, -1)) + { + luax_pop(L, 1); + luax_newtable(L); + + // metatable + luax_newtable(L); + // weak table + luax_pushliteral(L, "v"); + luax_setfield(L, -2, "__mode"); + // setmetatable(newtable, metatable) + luax_setmetatable(L, -2); + + luax_pushvalue(L, -1); + luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Objects_Table); + } + return 1; + } + + int luax_getmodulestable(lua_State* L) + { + luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Modules_Table); + // If no such table, add one. + if (luax_isnil(L, -1) || !luax_istable(L, -1)) + { + luax_pop(L, 1); + luax_newtable(L); + + // metatable + luax_newtable(L); + // weak table + luax_pushliteral(L, "v"); + luax_setfield(L, -2, "__mode"); + // setmetatable(newtable, metatable) + luax_setmetatable(L, -2); + + luax_pushvalue(L, -1); + luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Modules_Table); + } + return 1; + } + + int luax_getreferencestable(lua_State* L) + { + luax_getfield(L, LUA_REGISTRYINDEX, Jin_Lua_Reference_Table); + // If no such table, add one. + if (luax_isnil(L, -1) || !luax_istable(L, -1)) + { + luax_pop(L, 1); + luax_newtable(L); + luax_pushvalue(L, -1); + luax_setfield(L, LUA_REGISTRYINDEX, Jin_Lua_Reference_Table); + } + return 1; + } + + void luax_getreference(lua_State* L, LuaObject* obj) + { + luax_getreferencestable(L); + luax_pushlightuserdata(L, obj->shared); + luax_gettable(L, -2); + luax_remove(L, -2); + } + + bool luax_addreference(lua_State* L, LuaObject* obj, LuaObject* dep) + { + luax_getreference(L, obj); + // If no dependencies table, add one. + if (luax_isnil(L, -1)) + { + luax_pop(L, 1); + luax_getreferencestable(L); + luax_newtable(L); + luax_pushlightuserdata(L, obj->shared); + luax_pushvalue(L, -2); + luax_settable(L, -4); + luax_remove(L, -2); // Remove references table. + } + luax_pushlightuserdata(L, dep->shared); + 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, LuaObject* obj) + { + luax_getreferencestable(L); + luax_pushlightuserdata(L, obj->shared); + luax_pushnil(L); + luax_settable(L, -3); + luax_pop(L, 1); + } + + void luax_removereference(lua_State* L, LuaObject* obj, LuaObject* dep) + { + luax_getreference(L, obj); + if (luax_isnil(L, -1)) + { + luax_pop(L, 1); + return; + } + luax_pushlightuserdata(L, dep->shared); + luax_pushnil(L); + luax_settable(L, -3); + luax_pop(L, 1); + } + + } }
\ No newline at end of file |