diff options
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindVM.cpp')
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindVM.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindVM.cpp b/Runtime/Lua/LuaBind/LuaBindVM.cpp new file mode 100644 index 0000000..268a5ed --- /dev/null +++ b/Runtime/Lua/LuaBind/LuaBindVM.cpp @@ -0,0 +1,82 @@ +#include "LuaBindInternal.h" +#include "LuaBindVM.h" + +namespace LuaBind +{ + + VM::VMap VM::VMs; // 通过线程查找虚拟机,为了方便 + + VM* VM::TryGetVM(global_State* gState) + { + auto it = VMs.find(gState); + if (it != VMs.end()) + return it->second; + else + return nullptr; + } + + VM* VM::TryGetVM(lua_State* state) + { + return TryGetVM(G(state)); + } + + VM::VM() + : mStrongRefTable() + , mWeakRefTable() + { + mMainThread = luaL_newstate(); + assert(mMainThread); + mGlobalState = G(mMainThread); + + VMs.insert(std::pair<global_State*, VM*>(mGlobalState, this)); + } + + VM::~VM() + { + VMs.erase(mGlobalState); + lua_close(mMainThread); + } + + // 初始化context + void VM::Setup() + { + LUA_BIND_STATE(mMainThread); + + mStrongRefTable.Init(state, "GAMELAB_UNIVERSAL_STRONG_REFERENCE_TABLE"); + mWeakRefTable.Init(state, "GAMELAB_UNIVERSAL_WEAK_REFERENCE_TABLE", "v"); + } + + lua_State* VM::CreateThread() + { + lua_State* thread = lua_newthread(mMainThread); + assert(thread); + return thread; + } + + lua_State* VM::GetMainThread() + { + return mMainThread; + } + + State VM::GetMainState() + { + return mMainThread; + } + + RefTable& VM::GetStrongRefTable() + { + return mStrongRefTable; + } + + RefTable& VM::GetWeakRefTable() + { + return mWeakRefTable; + } + + void VM::OpenLibs() + { + assert(mMainThread); + luaL_openlibs(mMainThread); + } + +}
\ No newline at end of file |