summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/luax_vm.h
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Scripting/luax_vm.h')
-rw-r--r--Runtime/Scripting/luax_vm.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/Runtime/Scripting/luax_vm.h b/Runtime/Scripting/luax_vm.h
new file mode 100644
index 0000000..3eda3f8
--- /dev/null
+++ b/Runtime/Scripting/luax_vm.h
@@ -0,0 +1,63 @@
+#ifndef __LUAX_CONTEXT_H__
+#define __LUAX_CONTEXT_H__
+
+#include <map>
+#include <unordered_set>
+
+#include "luax_ref.h"
+#include "luax_config.h"
+#include "luax_state.h"
+#include "luax_globalstate.h"
+
+namespace Luax
+{
+
+ ///
+ /// 单个lua_state相关的context。是一系列代理的集合,拷贝也没关系,主要是为了节约内存。
+ ///
+ class LuaxVM
+ {
+ public:
+
+ ///
+ /// 根据global_State拿到虚拟机。
+ ///
+ static LuaxVM* TryGetVM(global_State* gState);
+ static LuaxVM* TryGetVM(lua_State* state);
+
+ LuaxVM();
+ ~LuaxVM();
+
+ ///
+ /// 创建虚拟机后,需要手动调用Setup函数,初始化一些虚拟机状态。
+ ///
+ void Setup();
+
+ lua_State* GetMainThread();
+ lua_State* CreateThread();
+ LuaxState GetMainState();
+
+ LuaxRefTable& GetStrongRefTable();
+ LuaxRefTable& GetWeakRefTable();
+
+ private:
+
+ typedef std::map<global_State*, LuaxVM*> VMap;
+
+ static VMap VMs; // 通过global_State索引虚拟机,为了方便
+
+ LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE
+ LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE
+
+ global_State* mGlobalState; // 虚拟机的global_State,由当前虚拟机的所有线程共享
+ lua_State* mMainThread; // 主线程
+
+#if LUAX_PROFILER
+ size_t mObjectCount; // 统计所有在此虚拟机中创建的实例
+#endif
+
+ };
+
+}
+
+#endif \ No newline at end of file