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