diff options
author | chai <chaifix@163.com> | 2018-12-04 15:38:19 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-12-04 15:38:19 +0800 |
commit | adbc170d850a23b219f1718c2cf3e40fadd66ce0 (patch) | |
tree | f26f3d68e3938b2bf147efbe467ed8767e6db5eb /src/lua/common/je_lua.cpp | |
parent | 85d456d30810347257a33d9443d4ad158d6d5fb1 (diff) |
*拷贝lua object到另一个lua state
Diffstat (limited to 'src/lua/common/je_lua.cpp')
-rw-r--r-- | src/lua/common/je_lua.cpp | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/src/lua/common/je_lua.cpp b/src/lua/common/je_lua.cpp index b09004f..21d3fa6 100644 --- a/src/lua/common/je_lua.cpp +++ b/src/lua/common/je_lua.cpp @@ -2,6 +2,7 @@ #include "je_lua.h" +using namespace std; using namespace JinEngine::Math; namespace JinEngine @@ -17,16 +18,54 @@ namespace JinEngine LuaObject* luax_newinstance(lua_State* L, const char* type, SharedBase* shared) { - LuaObject* luaObj = static_cast<LuaObject*>(luax_newinstance(L, type, sizeof(LuaObject))); - luaObj->state = L; - luaObj->bind(shared); + LuaObject* obj = static_cast<LuaObject*>(luax_newinstance(L, type, sizeof(LuaObject))); + obj->state = L; + 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 luaObj; + return obj; + } + + LuaObject* luax_newinstance(lua_State* L, LuaObject* src) + { + bool another = L != src->state; + if(another) + { + // Copy dependencies. + map<uint, SharedBase*>::iterator it = (*src->dependencies).begin(); + for (; it != (*src->dependencies).end(); ++it) + { + SharedBase* shr = it->second; + // Try get lua object. + luax_getobject(src->state, shr); + LuaObject* luaObj = (LuaObject*)luax_checktype(src->state, -1, shr->getType()); + luax_newinstance(L, luaObj); + } + } + SharedBase * shr = src->getSharedBase(); + LuaObject* obj = luax_newinstance(L, src->getObjectType(), shr); + (*obj->dependencies) = (*src->dependencies); + // Add to objects_table + luax_getobjectstable(L); + luax_pushlightuserdata(L, shr); + luax_pushvalue(L, -3); + luax_settable(L, -3); + luax_pop(L, 1); // Pop objects table. + // Set dependencies. + if (another) + { + map<uint, SharedBase*>::iterator it = (*obj->dependencies).begin(); + for (; it != (*obj->dependencies).end(); ++it) + { + SharedBase* dep = it->second; + luax_addreference(L, shr, dep); + } + } + return obj; } int luax_getobject(lua_State* L, SharedBase* shared) |