aboutsummaryrefslogtreecommitdiff
path: root/src/lua/common/je_lua.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-12-05 15:47:41 +0800
committerchai <chaifix@163.com>2018-12-05 15:47:41 +0800
commitd14d3de517e3bea5470ba42a4d1646cca77c2d25 (patch)
treefcc9e18b37b0e3cbddd8715fe093dc6e0a5945fe /src/lua/common/je_lua.cpp
parentf6a902ebd5743d54321e1232dae40f15041be5a6 (diff)
*lua reference
Diffstat (limited to 'src/lua/common/je_lua.cpp')
-rw-r--r--src/lua/common/je_lua.cpp47
1 files changed, 26 insertions, 21 deletions
diff --git a/src/lua/common/je_lua.cpp b/src/lua/common/je_lua.cpp
index 1c1f5aa..8605e62 100644
--- a/src/lua/common/je_lua.cpp
+++ b/src/lua/common/je_lua.cpp
@@ -1,6 +1,7 @@
#include "libjin/jin.h"
#include "je_lua.h"
+#include "je_lua_object.h"
using namespace std;
using namespace JinEngine::Math;
@@ -16,6 +17,8 @@ namespace JinEngine
static const char* Jin_Lua_Reference_Table = "Jin_Reference_Table";
+ using DepsMap = LuaObject::DepsMap;
+
LuaObject* luax_newinstance(lua_State* L, const char* type, SharedBase* shared)
{
LuaObject* obj = static_cast<LuaObject*>(luax_newinstance(L, type, sizeof(LuaObject)));
@@ -30,36 +33,38 @@ namespace JinEngine
return obj;
}
- LuaObject* luax_newinstance(lua_State* L, LuaObject* src)
+ LuaObject* luax_copyinstance(lua_State* to, LuaObject* src)
{
- if(L != src->state)
+ if (to == src->state)
+ return nullptr;
+
+ // Copy dependencies.
+ DepsMap& srcDeps = *src->dependencies;
+ for (DepsMap::iterator it = srcDeps.begin(); it != srcDeps.end(); ++it)
{
- // 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 = it->second;
+ // Try get lua object.
+ luax_getobject(src->state, shr);
+ LuaObject* luaObj = (LuaObject*)luax_checktype(src->state, -1, shr->getType());
+ luax_copyinstance(to, luaObj);
+ luax_pop(to, 1); // Pop reference object.
}
SharedBase * shr = src->getSharedBase();
- LuaObject* obj = luax_newinstance(L, src->getObjectType(), shr);
+ LuaObject* obj = luax_newinstance(to, 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.
+ luax_getobjectstable(to);
+ luax_pushlightuserdata(to, shr);
+ luax_pushvalue(to, -3);
+ luax_settable(to, -3);
+ luax_pop(to, 1); // Pop objects table.
// Set dependencies.
- map<uint, SharedBase*>::iterator it = (*obj->dependencies).begin();
- for (; it != (*obj->dependencies).end(); ++it)
+ DepsMap& deps = *obj->dependencies;
+ DepsMap::iterator it = deps.begin();
+ for (; it != deps.end(); ++it)
{
SharedBase* dep = it->second;
- luax_addreference(L, shr, dep);
+ luax_addreference(to, shr, dep);
}
return obj;
}