summaryrefslogtreecommitdiff
path: root/Source/external/Luax/luax_vm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/external/Luax/luax_vm.cpp')
-rw-r--r--Source/external/Luax/luax_vm.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/Source/external/Luax/luax_vm.cpp b/Source/external/Luax/luax_vm.cpp
new file mode 100644
index 0000000..4dc7e0c
--- /dev/null
+++ b/Source/external/Luax/luax_vm.cpp
@@ -0,0 +1,76 @@
+#include "luax_internal.h"
+#include "luax_vm.h"
+
+namespace Luax
+{
+
+ LuaxVM::VMap LuaxVM::VMs; // ̲ͨ߳Ϊ˷
+
+ LuaxVM* LuaxVM::TryGetVM(global_State* gState)
+ {
+ auto it = VMs.find(gState);
+ if (it != VMs.end())
+ return it->second;
+ else
+ return nullptr;
+ }
+
+ LuaxVM* LuaxVM::TryGetVM(lua_State* state)
+ {
+ return TryGetVM(G(state));
+ }
+
+ LuaxVM::LuaxVM()
+ : mStrongRefTable()
+ , mWeakRefTable()
+ {
+ mMainThread = luaL_newstate();
+ assert(mMainThread);
+ mGlobalState = G(mMainThread);
+
+ VMs.insert(std::pair<global_State*, LuaxVM*>(mGlobalState, this));
+ }
+
+ LuaxVM::~LuaxVM()
+ {
+ VMs.erase(mGlobalState);
+ lua_close(mMainThread);
+ }
+
+ // ʼcontext
+ void LuaxVM::Setup()
+ {
+ LUAX_STATE(mMainThread);
+
+ mStrongRefTable.Init(state, "_LUAX_STRONGREF_TABLE");
+ mWeakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v");
+ }
+
+ lua_State* LuaxVM::CreateThread()
+ {
+ lua_State* thread = lua_newthread(mMainThread);
+ assert(thread);
+ return thread;
+ }
+
+ lua_State* LuaxVM::GetMainThread()
+ {
+ return mMainThread;
+ }
+
+ LuaxState LuaxVM::GetMainState()
+ {
+ return mMainThread;
+ }
+
+ LuaxRefTable& LuaxVM::GetStrongRefTable()
+ {
+ return mStrongRefTable;
+ }
+
+ LuaxRefTable& LuaxVM::GetWeakRefTable()
+ {
+ return mWeakRefTable;
+ }
+
+} \ No newline at end of file