diff options
author | chai <chaifix@163.com> | 2019-09-13 15:08:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-09-13 15:08:43 +0800 |
commit | 20535cb86266d7a4828009f3ddca42e35269b9e2 (patch) | |
tree | 796e77e4a34ed52b6c92fb58ec432c69ed4232e1 /src/libjin-lua/common | |
parent | 695eadc9d2a6d2f499b24ee6858325f6e22da077 (diff) |
*格式化代码
Diffstat (limited to 'src/libjin-lua/common')
-rw-r--r-- | src/libjin-lua/common/je_lua.h | 132 | ||||
-rw-r--r-- | src/libjin-lua/common/l_callback.cpp | 64 | ||||
-rw-r--r-- | src/libjin-lua/common/l_callback.h | 86 | ||||
-rw-r--r-- | src/libjin-lua/common/l_constant.h | 6 | ||||
-rw-r--r-- | src/libjin-lua/common/l_object.cpp | 186 | ||||
-rw-r--r-- | src/libjin-lua/common/l_object.h | 70 | ||||
-rw-r--r-- | src/libjin-lua/common/l_reference.cpp | 60 | ||||
-rw-r--r-- | src/libjin-lua/common/l_reference.h | 88 | ||||
-rw-r--r-- | src/libjin-lua/common/l_runtime.cpp | 408 | ||||
-rw-r--r-- | src/libjin-lua/common/l_shared.cpp | 6 | ||||
-rw-r--r-- | src/libjin-lua/common/l_shared.hpp | 140 |
11 files changed, 623 insertions, 623 deletions
diff --git a/src/libjin-lua/common/je_lua.h b/src/libjin-lua/common/je_lua.h index 629874c..302fea6 100644 --- a/src/libjin-lua/common/je_lua.h +++ b/src/libjin-lua/common/je_lua.h @@ -12,72 +12,72 @@ namespace JinEngine { - namespace Lua - { - - // Extends luax.h library. - - /// - /// - /// - LuaObject* luax_newinstance(lua_State* L, const char* type, Shared* shared); - - /// - /// Copy instance to another lua state. - /// - LuaObject* luax_copyinstance(lua_State* to, LuaObject* src); - - /// - /// - /// - LuaObject* luax_checkobject(lua_State* L, int idx, const char* type); - - /// - /// Access lua object by object pointer. - /// - int luax_getobject(lua_State* L, LuaObject* obj); - - /// - /// Get object's reference table. - /// - void luax_getreference(lua_State* L, LuaObject* obj); - - /// - /// - /// - bool luax_addreference(lua_State* L, LuaObject* obj, LuaObject* dep); - - /// - /// - /// - void luax_removereference(lua_State* L, LuaObject* obj); - - /// - /// - /// - void luax_removereference(lua_State* L, LuaObject* obj, LuaObject* dep); - - /// - /// - /// - void luax_removeobject(lua_State* L, LuaObject* obj); - - /// - /// - /// - int luax_getobjectstable(lua_State* L); - - /// - /// - /// - int luax_getmodulestable(lua_State* L); - - /// - /// - /// - int luax_getreferencestable(lua_State* L); - - } + namespace Lua + { + + // Extends luax.h library. + + /// + /// + /// + LuaObject* luax_newinstance(lua_State* L, const char* type, Shared* shared); + + /// + /// Copy instance to another lua state. + /// + LuaObject* luax_copyinstance(lua_State* to, LuaObject* src); + + /// + /// + /// + LuaObject* luax_checkobject(lua_State* L, int idx, const char* type); + + /// + /// Access lua object by object pointer. + /// + int luax_getobject(lua_State* L, LuaObject* obj); + + /// + /// Get object's reference table. + /// + void luax_getreference(lua_State* L, LuaObject* obj); + + /// + /// + /// + bool luax_addreference(lua_State* L, LuaObject* obj, LuaObject* dep); + + /// + /// + /// + void luax_removereference(lua_State* L, LuaObject* obj); + + /// + /// + /// + void luax_removereference(lua_State* L, LuaObject* obj, LuaObject* dep); + + /// + /// + /// + void luax_removeobject(lua_State* L, LuaObject* obj); + + /// + /// + /// + int luax_getobjectstable(lua_State* L); + + /// + /// + /// + int luax_getmodulestable(lua_State* L); + + /// + /// + /// + int luax_getreferencestable(lua_State* L); + + } } #endif
\ No newline at end of file diff --git a/src/libjin-lua/common/l_callback.cpp b/src/libjin-lua/common/l_callback.cpp index a2adcea..4165ca6 100644 --- a/src/libjin-lua/common/l_callback.cpp +++ b/src/libjin-lua/common/l_callback.cpp @@ -2,42 +2,42 @@ namespace JinEngine { - namespace Lua - { + namespace Lua + { - LuaCallback::LuaCallback(lua_State* L) - : mLuaFunc(nullptr) - , mParams(0) - , mL(L) - { - } + LuaCallback::LuaCallback(lua_State* L) + : mLuaFunc(nullptr) + , mParams(0) + , mL(L) + { + } - LuaCallback::~LuaCallback() - { - delete mLuaFunc; - for (auto p : mParams) - delete p; - } + LuaCallback::~LuaCallback() + { + delete mLuaFunc; + for (auto p : mParams) + delete p; + } - void LuaCallback::setFunc(int i) - { - if (mLuaFunc != nullptr) - delete mLuaFunc; - mLuaFunc = new LuaRef(mL, i); - } + void LuaCallback::setFunc(int i) + { + if (mLuaFunc != nullptr) + delete mLuaFunc; + mLuaFunc = new LuaRef(mL, i); + } - void LuaCallback::pushParam(int i) - { - mParams.push_back(new LuaRef(mL, i)); - } + void LuaCallback::pushParam(int i) + { + mParams.push_back(new LuaRef(mL, i)); + } - void LuaCallback::call() - { - mLuaFunc->push(); - for (auto p : mParams) - p->push(); - luax_call(mL, mParams.size(), 0); - } + void LuaCallback::call() + { + mLuaFunc->push(); + for (auto p : mParams) + p->push(); + luax_call(mL, mParams.size(), 0); + } - } + } }
\ No newline at end of file diff --git a/src/libjin-lua/common/l_callback.h b/src/libjin-lua/common/l_callback.h index af7200c..c6ab5b4 100644 --- a/src/libjin-lua/common/l_callback.h +++ b/src/libjin-lua/common/l_callback.h @@ -8,59 +8,59 @@ namespace JinEngine { - namespace Lua - { + namespace Lua + { - /// - /// - /// - class LuaCallback - { - public: - /// - /// - /// - LuaCallback(lua_State* L); + /// + /// + /// + class LuaCallback + { + public: + /// + /// + /// + LuaCallback(lua_State* L); - /// - /// - /// - ~LuaCallback(); + /// + /// + /// + ~LuaCallback(); - /// - /// - /// - void setFunc(int i); + /// + /// + /// + void setFunc(int i); - /// - /// - /// - void pushParam(int i); + /// + /// + /// + void pushParam(int i); - /// - /// - /// - void call(); + /// + /// + /// + void call(); - private: - /// - /// - /// - LuaRef* mLuaFunc; + private: + /// + /// + /// + LuaRef* mLuaFunc; - /// - /// - /// - std::vector<LuaRef*> mParams; + /// + /// + /// + std::vector<LuaRef*> mParams; - /// - /// - /// - lua_State* const mL; + /// + /// + /// + lua_State* const mL; - }; + }; - } // namespace Lua + } // namespace Lua } // namespace JinEngine #endif // __JIN_COMMON_REFERENCE_H
\ No newline at end of file diff --git a/src/libjin-lua/common/l_constant.h b/src/libjin-lua/common/l_constant.h index c173b88..05d3b0c 100644 --- a/src/libjin-lua/common/l_constant.h +++ b/src/libjin-lua/common/l_constant.h @@ -3,12 +3,12 @@ namespace JinEngine { - namespace Lua - { + namespace Lua + { - } + } } #endif
\ No newline at end of file diff --git a/src/libjin-lua/common/l_object.cpp b/src/libjin-lua/common/l_object.cpp index e3ac158..8117079 100644 --- a/src/libjin-lua/common/l_object.cpp +++ b/src/libjin-lua/common/l_object.cpp @@ -3,110 +3,110 @@ namespace JinEngine { - namespace Lua - { + namespace Lua + { - void LuaObject::bind(Shared* obj) - { - if (obj) - { - shared = obj; - shared->retain(); - dependencies = new std::map<uint, LuaObject*>(); - } - } + void LuaObject::bind(Shared* obj) + { + if (obj) + { + shared = obj; + shared->retain(); + dependencies = new std::map<uint, LuaObject*>(); + } + } - void LuaObject::release() - { - // Delete lua shared table reference and references. - if (dependencies) - { - clearDependencies(); - delete dependencies; - dependencies = nullptr; - } - // Try delete engine side shared. - if (shared) - { - shared->release(); - shared = nullptr; - } - } + void LuaObject::release() + { + // Delete lua shared table reference and references. + if (dependencies) + { + clearDependencies(); + delete dependencies; + dependencies = nullptr; + } + // Try delete engine side shared. + if (shared) + { + shared->release(); + shared = nullptr; + } + } - const char* LuaObject::getObjectType() - { - return type; - } + const char* LuaObject::getObjectType() + { + return type; + } - Shared* LuaObject::getShared() - { - return shared; - } + Shared* LuaObject::getShared() + { + return shared; + } - void LuaObject::setDependency(uint key, LuaObject* dep) - { - removeDependency(key); - dependencies->insert(std::pair<uint, LuaObject*>(key, dep)); - luax_addreference(state, this, dep); - } + void LuaObject::setDependency(uint key, LuaObject* dep) + { + removeDependency(key); + dependencies->insert(std::pair<uint, LuaObject*>(key, dep)); + luax_addreference(state, this, dep); + } - void LuaObject::removeDependency(uint key) - { - if (!isDependOn(key)) - return; - DepsMap::iterator it = dependencies->find(key); - LuaObject* dep = it->second; - luax_removereference(state, this, dep); - dependencies->erase(it); - } + void LuaObject::removeDependency(uint key) + { + if (!isDependOn(key)) + return; + DepsMap::iterator it = dependencies->find(key); + LuaObject* dep = it->second; + luax_removereference(state, this, dep); + dependencies->erase(it); + } - void LuaObject::removeDependency(LuaObject* dependency) - { - for (DepsMap::iterator it = dependencies->begin(); it != dependencies->end();) - { - LuaObject* dep = it->second; - if (dep == dependency) - { - luax_removereference(state, this, dep); - dependencies->erase(it); - } - else - ++it; - } - } + void LuaObject::removeDependency(LuaObject* dependency) + { + for (DepsMap::iterator it = dependencies->begin(); it != dependencies->end();) + { + LuaObject* dep = it->second; + if (dep == dependency) + { + luax_removereference(state, this, dep); + dependencies->erase(it); + } + else + ++it; + } + } - bool LuaObject::isDependOn(uint key) - { - return dependencies->find(key) != dependencies->end(); - } + bool LuaObject::isDependOn(uint key) + { + return dependencies->find(key) != dependencies->end(); + } - bool LuaObject::isDependOn(LuaObject* shared) - { - for (std::pair<uint, LuaObject*> dep : (*dependencies)) - { - if (dep.second == shared) - return true; - } - return false; - } + bool LuaObject::isDependOn(LuaObject* shared) + { + for (std::pair<uint, LuaObject*> dep : (*dependencies)) + { + if (dep.second == shared) + return true; + } + return false; + } - void LuaObject::clearDependencies() - { - luax_removereference(state, this); - dependencies->clear(); - } + void LuaObject::clearDependencies() + { + luax_removereference(state, this); + dependencies->clear(); + } - LuaObject* LuaObject::getDependency(uint key) - { - if (!isDependOn(key)) - return nullptr; - return dependencies->find(key)->second; - } + LuaObject* LuaObject::getDependency(uint key) + { + if (!isDependOn(key)) + return nullptr; + return dependencies->find(key)->second; + } - int LuaObject::getDependenciesCount() - { - return dependencies->size(); - } + int LuaObject::getDependenciesCount() + { + return dependencies->size(); + } - } + } }
\ No newline at end of file diff --git a/src/libjin-lua/common/l_object.h b/src/libjin-lua/common/l_object.h index 15bd79d..84d16d7 100644 --- a/src/libjin-lua/common/l_object.h +++ b/src/libjin-lua/common/l_object.h @@ -8,62 +8,62 @@ struct lua_State; namespace JinEngine { - namespace Lua - { + namespace Lua + { - class LuaObject - { - public: - /// - /// Set lua_State and object it bind. - /// - void bind(Shared* obj); + class LuaObject + { + public: + /// + /// Set lua_State and object it bind. + /// + void bind(Shared* obj); - void release(); + void release(); - const char* getObjectType(); + const char* getObjectType(); - Shared* getShared(); + Shared* getShared(); - template<class T> - T* getObject() - { - return shared->getObject<T>(); - } + template<class T> + T* getObject() + { + return shared->getObject<T>(); + } - void setDependency(uint key, LuaObject* dep); + void setDependency(uint key, LuaObject* dep); - void removeDependency(uint key); + void removeDependency(uint key); - void removeDependency(LuaObject* dep); + void removeDependency(LuaObject* dep); - bool isDependOn(uint key); + bool isDependOn(uint key); - bool isDependOn(LuaObject* shared); + bool isDependOn(LuaObject* shared); - void clearDependencies(); + void clearDependencies(); - LuaObject* getDependency(uint key); + LuaObject* getDependency(uint key); - int getDependenciesCount(); + int getDependenciesCount(); - ////////////////////////////////////////////////////////////////////////////////////////////////////// - // Lua state object. - ////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////////////////////////////////////////// + // Lua state object. + ////////////////////////////////////////////////////////////////////////////////////////////////////// - using DepsMap = std::map<uint, LuaObject*>; + using DepsMap = std::map<uint, LuaObject*>; - lua_State* state; + lua_State* state; - Shared* shared; + Shared* shared; - const char* type; + const char* type; - DepsMap* dependencies; + DepsMap* dependencies; - }; + }; - } // namespace Lua + } // namespace Lua } // namespace JinEngine #endif // __JIN_COMMON_OBJECT_H__
\ No newline at end of file diff --git a/src/libjin-lua/common/l_reference.cpp b/src/libjin-lua/common/l_reference.cpp index 2fcf792..550ff73 100644 --- a/src/libjin-lua/common/l_reference.cpp +++ b/src/libjin-lua/common/l_reference.cpp @@ -4,39 +4,39 @@ namespace JinEngine { - namespace Lua - { + namespace Lua + { - LuaRef::LuaRef(lua_State* L, int i) - : mL(L) - { - // Get value. - luax_pushvalue(mL, i); - // Set reference. - luax_getreferencestable(L); - luax_pushvalue(mL, -2); - mIndex = luax_ref(mL, -2); - luax_pop(L, 3); - } + LuaRef::LuaRef(lua_State* L, int i) + : mL(L) + { + // Get value. + luax_pushvalue(mL, i); + // Set reference. + luax_getreferencestable(L); + luax_pushvalue(mL, -2); + mIndex = luax_ref(mL, -2); + luax_pop(L, 3); + } - LuaRef::~LuaRef() - { - unref(); - } + LuaRef::~LuaRef() + { + unref(); + } - void LuaRef::unref() - { - luax_getreferencestable(mL); - luax_unref(mL, -1, mIndex); - luax_pop(mL, 1); - } + void LuaRef::unref() + { + luax_getreferencestable(mL); + luax_unref(mL, -1, mIndex); + luax_pop(mL, 1); + } - void LuaRef::push() - { - luax_getreferencestable(mL); - luax_rawgeti(mL, -1, mIndex); - luax_remove(mL, -2); - } + void LuaRef::push() + { + luax_getreferencestable(mL); + luax_rawgeti(mL, -1, mIndex); + luax_remove(mL, -2); + } - } + } }
\ No newline at end of file diff --git a/src/libjin-lua/common/l_reference.h b/src/libjin-lua/common/l_reference.h index 1a62dba..4c4756f 100644 --- a/src/libjin-lua/common/l_reference.h +++ b/src/libjin-lua/common/l_reference.h @@ -5,50 +5,50 @@ namespace JinEngine { - namespace Lua - { - - /// - /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua - /// variables. - /// - class LuaRef - { - public: - /// - /// - /// - LuaRef(lua_State* L, int i); - - /// - /// - /// - ~LuaRef(); - - /// - /// - /// - void unref(); - - /// - /// Push value onto the stack. - /// - void push(); - - private: - /// - /// - /// - lua_State* const mL; - - /// - /// - /// - int mIndex; - - }; - - } // namespace Lua + namespace Lua + { + + /// + /// This class wraps the reference functionality built into Lua, which allows C++ code to refer to Lua + /// variables. + /// + class LuaRef + { + public: + /// + /// + /// + LuaRef(lua_State* L, int i); + + /// + /// + /// + ~LuaRef(); + + /// + /// + /// + void unref(); + + /// + /// Push value onto the stack. + /// + void push(); + + private: + /// + /// + /// + lua_State* const mL; + + /// + /// + /// + int mIndex; + + }; + + } // namespace Lua } // namespace JinEngine #endif // __JIN_COMMON_REFERENCE_H
\ No newline at end of file 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 diff --git a/src/libjin-lua/common/l_shared.cpp b/src/libjin-lua/common/l_shared.cpp index 03303da..047905e 100644 --- a/src/libjin-lua/common/l_shared.cpp +++ b/src/libjin-lua/common/l_shared.cpp @@ -4,8 +4,8 @@ namespace JinEngine { - namespace Lua - { + namespace Lua + { - } + } }
\ No newline at end of file diff --git a/src/libjin-lua/common/l_shared.hpp b/src/libjin-lua/common/l_shared.hpp index 6a58ab3..11f28b3 100644 --- a/src/libjin-lua/common/l_shared.hpp +++ b/src/libjin-lua/common/l_shared.hpp @@ -9,76 +9,76 @@ namespace JinEngine { - namespace Lua - { - - class LuaObject; - - /// - /// Thread shared object wrapper. - /// - class Shared - { - public: - Shared(Object* obj) - : mCount(0) - , mObject(obj) - { - } - - inline Object* operator->() - { - return static_cast<Object*>(mObject); - } - - inline Object* getObject() - { - return static_cast<Object*>(mObject); - } - - template<class T> - inline T* getObject() - { - return static_cast<T*>(mObject); - } - - private: - friend class LuaObject; - - // Disable copy. - Shared(const Shared& shared); - - /// - /// Sharedֻڶ - /// - ~Shared() - { - delete mObject; - } - - /// - /// ͬһ̵߳lua_StateУLuaObjectEngineObjectӦһһӦLuaObject(lua runtime)ü - /// ͻաEngine-sideüΪά̵ͬ߳lua_StateͬһEngineObjectÿLuaObjectһ - /// Sharedʱһüͬһ߳УһEngineObjectֻһLuaObjectnew - /// instanceУômCountͱEngineObjectĹ߳ - /// - inline void Shared::retain() - { - ++mCount; - } - - inline void Shared::release() - { - if (--mCount <= 0) - delete this; - } - - Object* mObject; - int mCount; - - }; - - } // namespace Lua + namespace Lua + { + + class LuaObject; + + /// + /// Thread shared object wrapper. + /// + class Shared + { + public: + Shared(Object* obj) + : mCount(0) + , mObject(obj) + { + } + + inline Object* operator->() + { + return static_cast<Object*>(mObject); + } + + inline Object* getObject() + { + return static_cast<Object*>(mObject); + } + + template<class T> + inline T* getObject() + { + return static_cast<T*>(mObject); + } + + private: + friend class LuaObject; + + // Disable copy. + Shared(const Shared& shared); + + /// + /// Sharedֻڶ + /// + ~Shared() + { + delete mObject; + } + + /// + /// ͬһ̵߳lua_StateУLuaObjectEngineObjectӦһһӦLuaObject(lua runtime)ü + /// ͻաEngine-sideüΪά̵ͬ߳lua_StateͬһEngineObjectÿLuaObjectһ + /// Sharedʱһüͬһ߳УһEngineObjectֻһLuaObjectnew + /// instanceУômCountͱEngineObjectĹ߳ + /// + inline void Shared::retain() + { + ++mCount; + } + + inline void Shared::release() + { + if (--mCount <= 0) + delete this; + } + + Object* mObject; + int mCount; + + }; + + } // namespace Lua } // namespace JinEngine #endif
\ No newline at end of file |