diff options
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindVM.cpp')
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindVM.cpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindVM.cpp b/Runtime/Lua/LuaBind/LuaBindVM.cpp index 268a5ed..6b39288 100644 --- a/Runtime/Lua/LuaBind/LuaBindVM.cpp +++ b/Runtime/Lua/LuaBind/LuaBindVM.cpp @@ -3,8 +3,10 @@ namespace LuaBind { + static cc8* kStrongTableName = "GAMELAB_UNIVERSAL_STRONG_REFERENCE_TABLE"; + static cc8* kWeakTableName = "GAMELAB_UNIVERSAL_WEAK_REFERENCE_TABLE"; - VM::VMap VM::VMs; // 通过线程查找虚拟机,为了方便 + std::map<global_State*, VM*> VM::VMs; // 通过线程查找虚拟机,为了方便 VM* VM::TryGetVM(global_State* gState) { @@ -12,7 +14,7 @@ namespace LuaBind if (it != VMs.end()) return it->second; else - return nullptr; + return NULL; } VM* VM::TryGetVM(lua_State* state) @@ -21,11 +23,11 @@ namespace LuaBind } VM::VM() - : mStrongRefTable() - , mWeakRefTable() + : mStrongRefTable(this) + , mWeakRefTable(this) + , mClasses() { mMainThread = luaL_newstate(); - assert(mMainThread); mGlobalState = G(mMainThread); VMs.insert(std::pair<global_State*, VM*>(mGlobalState, this)); @@ -42,8 +44,8 @@ namespace LuaBind { LUA_BIND_STATE(mMainThread); - mStrongRefTable.Init(state, "GAMELAB_UNIVERSAL_STRONG_REFERENCE_TABLE"); - mWeakRefTable.Init(state, "GAMELAB_UNIVERSAL_WEAK_REFERENCE_TABLE", "v"); + mStrongRefTable.Init(kStrongTableName); + mWeakRefTable.Init(kWeakTableName, "v"); } lua_State* VM::CreateThread() @@ -79,4 +81,28 @@ namespace LuaBind luaL_openlibs(mMainThread); } + int VM::RegisterClassI(State& state, int classID, int index) + { + assert(state.GetVM() == this); + assert(mClasses.count(classID) == 0); + StrongRef ref = StrongRef(this); + ref.SetRef(state, index); + auto cls = std::pair<int, StrongRef>(classID, ref); + mClasses.insert(cls); + return 1; + } + + void VM::PushClassTable(State& state, int classID) + { + assert(state.GetVM() == this); + auto ref = mClasses.find(classID); + if (ref == mClasses.end()) + { + state.PushNil(); + return; + } + StrongRef cls = ref->second; + cls.PushRef(state); + } + }
\ No newline at end of file |