From 250e30d73f09e9da2b5a81d0fbae63744ae12a73 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 2 Apr 2019 08:47:15 +0800 Subject: *misc --- source/libs/asura-lib-utils/scripting/lua_env.h | 72 +++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 source/libs/asura-lib-utils/scripting/lua_env.h (limited to 'source/libs/asura-lib-utils/scripting/lua_env.h') diff --git a/source/libs/asura-lib-utils/scripting/lua_env.h b/source/libs/asura-lib-utils/scripting/lua_env.h new file mode 100644 index 0000000..e2fc4fc --- /dev/null +++ b/source/libs/asura-lib-utils/scripting/lua_env.h @@ -0,0 +1,72 @@ +#ifndef __ASURA_LUA_ENV_H__ +#define __ASURA_LUA_ENV_H__ + +extern "C" +{ +#include +#include +} +#include + +#include "../singleton.hpp" + +namespace AsuraEngine +{ + namespace Scripting + { + + /// + /// 程序通过这个接口访问主lua state。Asura程序的思想是,进程主线程维护一个lua虚拟机,应用 + /// 的主要逻辑跑在此虚拟机的主lua线程里(或者叫主栈里)。 + /// + class LuaEnv ASURA_FINAL : public Singleton + { + public: + + LuaEnv() : mVM(0) {}; + ~LuaEnv() {}; + + /// + /// 设置主虚拟机的主执行栈。 + /// + inline void Init() + { + ASSERT(!mVM); + mVM = new Luax::LuaxVM(); + ASSERT(mVM); + mVM->Setup(); + }; + + inline lua_State* GetMainThread() + { + return mVM->GetMainThread(); + }; + + inline void Exit() + { + delete mVM; + mVM = nullptr; + } + + private: + + /// + /// 主线程保存一个主lua虚拟机。一个Asura程序永远只有一个主虚拟机,大部分代码都在这个虚 + /// 拟机中运行,且只能设置一次。主线程\主执行栈对应的是global_State里面的: + /// + /// struct lua_State *mainthread; + /// + /// 使用lua_newstate(),会创建一个虚拟机,包含一个global_State结构,并创建一个这个虚 + /// 拟机的主线程lua_State并返回。global_State由这个虚拟机的所有线程共享,除了第一个主 + /// 线程由lua_newstate()创建外,其余线程由lua_newthread()创建。 + /// + Luax::LuaxVM* mVM; + + }; + + } +} + +namespace AEScripting = AsuraEngine::Scripting; + +#endif \ No newline at end of file -- cgit v1.1-26-g67d0