diff options
author | chai <chaifix@163.com> | 2018-12-05 15:47:41 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-12-05 15:47:41 +0800 |
commit | d14d3de517e3bea5470ba42a4d1646cca77c2d25 (patch) | |
tree | fcc9e18b37b0e3cbddd8715fe093dc6e0a5945fe | |
parent | f6a902ebd5743d54321e1232dae40f15041be5a6 (diff) |
*lua reference
-rw-r--r-- | bin/game/main.lua | 4 | ||||
-rw-r--r-- | bin/jin.exe | bin | 1596416 -> 1596416 bytes | |||
-rw-r--r-- | src/lua/common/je_lua.cpp | 47 | ||||
-rw-r--r-- | src/lua/common/je_lua.h | 4 | ||||
-rw-r--r-- | src/lua/modules/thread/je_lua_thread.cpp | 8 |
5 files changed, 32 insertions, 31 deletions
diff --git a/bin/game/main.lua b/bin/game/main.lua index 97079f2..9070d2c 100644 --- a/bin/game/main.lua +++ b/bin/game/main.lua @@ -100,8 +100,8 @@ end function jin.core.onUpdate() tb.x = t t = t + jin.time.getDelta() - animator:update(jin.time.getDelta()) - timer:update(jin.time.getDelta()) + animator:update(jin.time.getDelta()) + timer:update(jin.time.getDelta()) end function jin.core.onDraw() diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex 10def3d..5241d02 100644 --- a/bin/jin.exe +++ b/bin/jin.exe 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; } diff --git a/src/lua/common/je_lua.h b/src/lua/common/je_lua.h index 5f9ebb3..24f89fe 100644 --- a/src/lua/common/je_lua.h +++ b/src/lua/common/je_lua.h @@ -23,9 +23,9 @@ namespace JinEngine LuaObject* luax_newinstance(lua_State* L, const char* type, SharedBase* shared); /// + /// Copy instance to another lua state. /// - /// - LuaObject* luax_newinstance(lua_State* L, LuaObject* src); + LuaObject* luax_copyinstance(lua_State* to, LuaObject* src); /// /// Access lua object by object pointer. diff --git a/src/lua/modules/thread/je_lua_thread.cpp b/src/lua/modules/thread/je_lua_thread.cpp index d99161f..dc6a27a 100644 --- a/src/lua/modules/thread/je_lua_thread.cpp +++ b/src/lua/modules/thread/je_lua_thread.cpp @@ -126,7 +126,7 @@ namespace JinEngine case Thread::Variant::POINTER: LuaObject* p = (LuaObject*)v.pointer; // Create lua object from other lua_State. - LuaObject* luaObj = luax_newinstance(L, p); + LuaObject* luaObj = luax_copyinstance(L, p); break; } @@ -159,7 +159,7 @@ namespace JinEngine case Thread::Variant::POINTER: LuaObject* p = (LuaObject*)v.pointer; // Create lua object from other lua_State. - LuaObject* luaObj = luax_newinstance(L, p->getObjectType(), p->getSharedBase()); + LuaObject* luaObj = luax_copyinstance(L, p); break; } @@ -205,9 +205,7 @@ namespace JinEngine { "isRunning", l_isRunning }, { 0, 0 } }; - luax_newtype(L, Jin_Lua_Thread, thread_function); - return 0; } @@ -230,14 +228,12 @@ namespace JinEngine LUA_EXPORT int luaopen_thread(lua_State* L) { luaopen_Thread(L); - luaL_Reg methods[] = { { "newThread", l_newThread }, { "getThread", l_getThread }, { 0, 0 } }; luax_newlib(L, methods); - return 1; } |