From 45328cbadd8a946c19a77301f218efbf650e2f28 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 18 Oct 2021 19:56:41 +0800 Subject: *misc --- Runtime/Lua/LuaBind/LuaBindVM.cpp | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Runtime/Lua/LuaBind/LuaBindVM.cpp (limited to 'Runtime/Lua/LuaBind/LuaBindVM.cpp') 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(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 -- cgit v1.1-26-g67d0