summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/LuaBindVM.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-11-15 09:59:32 +0800
committerchai <chaifix@163.com>2020-11-15 09:59:32 +0800
commitd2e4b2839bc7ce874370ff4c52dcfdadf666ff52 (patch)
treeb83e34c2d66cf80b94af5c9a420b6c9c206c138c /Runtime/Scripting/LuaBindVM.cpp
parent64f89347883d519b202711a11d2ea175bd80be37 (diff)
+lua binding
Diffstat (limited to 'Runtime/Scripting/LuaBindVM.cpp')
-rw-r--r--Runtime/Scripting/LuaBindVM.cpp76
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