From d2e4b2839bc7ce874370ff4c52dcfdadf666ff52 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 15 Nov 2020 09:59:32 +0800 Subject: +lua binding --- Runtime/Scripting/LuaBindVM.cpp | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Runtime/Scripting/LuaBindVM.cpp (limited to 'Runtime/Scripting/LuaBindVM.cpp') 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(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 -- cgit v1.1-26-g67d0