From 157530b8b6e11efc5573d5a0db8987a440197aa1 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 29 Mar 2019 22:28:40 +0800 Subject: *misc --- source/3rd-party/Lua51/loadlib.c | 2 +- source/3rd-party/Luax/luax_class.cpp | 6 +-- source/3rd-party/Luax/luax_class.hpp | 6 +-- source/3rd-party/Luax/luax_class.inl | 44 ++++++++++++++++------ source/3rd-party/Luax/luax_config.h | 6 ++- source/3rd-party/Luax/luax_globalstate.h | 7 ++++ source/3rd-party/Luax/luax_internal.h | 12 ++++++ source/3rd-party/Luax/luax_ref.cpp | 6 ++- source/3rd-party/Luax/luax_state.cpp | 16 ++++++-- source/3rd-party/Luax/luax_state.h | 8 ++-- source/3rd-party/Luax/luax_vm.cpp | 32 +++++++--------- source/3rd-party/Luax/luax_vm.h | 34 +++++++++-------- source/3rd-party/plotscript/plot.c | 5 +++ .../3rd-party/plotscript/test/first_meeting.plot | 9 +++++ 14 files changed, 132 insertions(+), 61 deletions(-) create mode 100644 source/3rd-party/Luax/luax_globalstate.h create mode 100644 source/3rd-party/Luax/luax_internal.h create mode 100644 source/3rd-party/plotscript/plot.c create mode 100644 source/3rd-party/plotscript/test/first_meeting.plot (limited to 'source/3rd-party') diff --git a/source/3rd-party/Lua51/loadlib.c b/source/3rd-party/Lua51/loadlib.c index 9455c53..6158c53 100644 --- a/source/3rd-party/Lua51/loadlib.c +++ b/source/3rd-party/Lua51/loadlib.c @@ -654,7 +654,7 @@ LUALIB_API int luaopen_package (lua_State *L) { lua_setfield(L, -2, "config"); /* set field `loaded' */ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); - lua_setfield(L, -2, "loaded"); // LUA_REGISTRYINDEX["_LOADED"]设置为package.loaded\LUA_ENVIRONINDEX["loaded"] + lua_setfield(L, -2, "loaded"); /* set field `preload' */ lua_newtable(L); lua_setfield(L, -2, "preload"); diff --git a/source/3rd-party/Luax/luax_class.cpp b/source/3rd-party/Luax/luax_class.cpp index 960fd47..bb1609d 100644 --- a/source/3rd-party/Luax/luax_class.cpp +++ b/source/3rd-party/Luax/luax_class.cpp @@ -46,13 +46,13 @@ namespace Luax lua_setfield(L, -2, "__index"); lua_pushstring(L, type); - lua_pushcclosure(L, l___tostring, 1); + lua_pushcclosure(L, _Tostring, 1); lua_setfield(L, -2, "__tostring"); return 1; } - int LuaxPlainClass::l___tostring(lua_State* L) + int LuaxPlainClass::_Tostring(lua_State* L) { // upvalues: // 1: class name @@ -184,7 +184,7 @@ namespace Luax lua_setfield(L, -2, "__index"); lua_pushstring(L, type); - lua_pushcclosure(L, l___tostring, 1); + lua_pushcclosure(L, _Tostring, 1); lua_setfield(L, -2, "__tostring"); // class的metatable设置为baseClass diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp index 99ac381..257e1ca 100644 --- a/source/3rd-party/Luax/luax_class.hpp +++ b/source/3rd-party/Luax/luax_class.hpp @@ -180,12 +180,12 @@ namespace Luax public: // 公共内容 - LUAX_DECL_METHOD( l___tostring ); + LUAX_DECL_METHOD( _Tostring ); LUAX_DECL_METHOD( l_GetClass ); LUAX_DECL_METHOD( l_GetClassName ); // 工厂类相关 - LUAX_DECL_METHOD( l___gc ); + LUAX_DECL_METHOD( _GC ); #if LUAX_ENABLE_NATIVE_EXTEND LUAX_DECL_METHOD( l_ExtendFactory ); #endif @@ -213,7 +213,7 @@ namespace Luax /// static int registry(lua_State* L); - LUAX_DECL_METHOD( l___tostring ); + LUAX_DECL_METHOD( _Tostring ); LUAX_DECL_METHOD( l_Extend ); LUAX_DECL_METHOD( l_New ); LUAX_DECL_METHOD( l_TypeOf ); diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl index d1c8c4b..45e6552 100644 --- a/source/3rd-party/Luax/luax_class.inl +++ b/source/3rd-party/Luax/luax_class.inl @@ -116,7 +116,12 @@ namespace Luax u32 count = state.GetValue(-1, 0); // get the count (or 0) lua_pop(state, 1); // pop the old count - if (count == 0) return; // nothing to do + // no such reference + if (count == 0) + { + state.Pop(2); // userdata, reftable + return; // nothing to do + } if (count > 1) { lua_pushnumber(state, count - 1); // push the new count @@ -125,7 +130,12 @@ namespace Luax lua_pushnil(state); // maybe cause gc } lua_settable(state, -3); // save it in the table + + state.Pop(1); // reftable + return; } + state.Pop(2); // nil, reftable + return; } } @@ -194,9 +204,10 @@ namespace Luax /// /// 创建userdata,并以此添加ref table,member table和class table。 - /// ref table 是kv强引用table,保存对其他userdata的引用计数(通过userdata作为key,计数为value),以及成员引用 + /// ref table 是kv强引用table,保存对其他userdata的引用计数(通过userdata作为key, + /// 计数为value),以及成员引用 /// member table 保存lua创建的实例的成员 - /// class table 所有本类型的实例共有的函数表 + /// class table 所有本类型的实例共有的函数表 /// template void LuaxNativeClass::BindToLua(LuaxState& state) @@ -205,8 +216,13 @@ namespace Luax assert(!T::IsLuaxClassSingleton()); assert(!mUserdata); - // 创建userdata并留在栈顶,注意地址要转换为T*,直接用this可能会导致多重继承的类丧失多态。 - state.PushPtrUserdata(static_cast(this)); + /// + /// 创建userdata并留在栈顶,注意地址要转换为T*,直接用this可能会导致多重继承的类丧失多态。 + /// 如果直接传this进去,在多重继承情况下,是拿不到另一头的虚函数表的。所以这里需要将this + /// 转换为整个对象的低地址,这样可以拿到另一个基类的虚函数表,通过另一个基类实现多态。 + /// + T* p = static_cast(this); + state.PushPtrUserdata(p); lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期 lua_newtable(state); // member table,lua中创建的对象成员都保存在这里 @@ -222,10 +238,13 @@ namespace Luax int memberTable = top - 1; int refTable = top - 2; - // ref table 注册 __tostring - lua_pushcfunction(state, l___tostring); + // ref table 注册 __tostring 和 __gc + lua_pushcfunction(state, _Tostring); lua_setfield(state, refTable, "__tostring"); + lua_pushcfunction(state, _GC); + lua_setfield(state, refTable, "__gc"); + // ref table 的 __index 和 __newindex 设为 member table lua_pushvalue(state, memberTable); lua_setfield(state, refTable, "__index"); @@ -233,9 +252,6 @@ namespace Luax lua_pushvalue(state, memberTable); lua_setfield(state, refTable, "__newindex"); - lua_pushcfunction(state, l___gc); - lua_setfield(state, refTable, "__gc"); - // 设置元表 lua_setmetatable(state, -2); // class is meta of member lua_setmetatable(state, -2); // member is meta of ref @@ -321,8 +337,12 @@ namespace Luax /// 释放工厂创建的实例 /// template - int LuaxNativeClass::l___gc(lua_State* L) + int LuaxNativeClass::_GC(lua_State* L) { +#if LUAX_PROFILER + std::cout << "Luax: GC<" << T::GetLuaxClassName() << ">\n"; +#endif + LUAX_SETUP(L, "U"); T* self = state.GetUserdata(1); delete self; @@ -334,7 +354,7 @@ namespace Luax /// 地址 类名 /// template - int LuaxNativeClass::l___tostring(lua_State* L) + int LuaxNativeClass::_Tostring(lua_State* L) { // params: // 1: userdata diff --git a/source/3rd-party/Luax/luax_config.h b/source/3rd-party/Luax/luax_config.h index c251c6f..2a8ed3e 100644 --- a/source/3rd-party/Luax/luax_config.h +++ b/source/3rd-party/Luax/luax_config.h @@ -55,7 +55,11 @@ namespace Luax #define LUAX_ENABLE_PLAIN_CLASS 0 #define LUAX_ENABLE_PLAIN_ENUM 0 -#define LUAX_PROFILER 0 +#define LUAX_PROFILER 1 + +#if LUAX_PROFILER +#include +#endif } diff --git a/source/3rd-party/Luax/luax_globalstate.h b/source/3rd-party/Luax/luax_globalstate.h new file mode 100644 index 0000000..91be51f --- /dev/null +++ b/source/3rd-party/Luax/luax_globalstate.h @@ -0,0 +1,7 @@ +#ifndef __LUAX_GLOBAL_STATE_H__ +#define __LUAX_GLOBAL_STATE_H__ + +// 索引到lua里的global_State +typedef struct global_State global_State; + +#endif \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_internal.h b/source/3rd-party/Luax/luax_internal.h new file mode 100644 index 0000000..5904008 --- /dev/null +++ b/source/3rd-party/Luax/luax_internal.h @@ -0,0 +1,12 @@ +#ifndef __LUAX_INTERNAL_H__ +#define __LUAX_INTERNAL_H__ + +/// +/// 对lua源代码的深度使用 +/// +extern "C" +{ +#include "lua51/lstate.h" +} + +#endif \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_ref.cpp b/source/3rd-party/Luax/luax_ref.cpp index 78596e3..d4be775 100644 --- a/source/3rd-party/Luax/luax_ref.cpp +++ b/source/3rd-party/Luax/luax_ref.cpp @@ -23,7 +23,7 @@ namespace Luax { assert(mRefID != LUA_NOREF); - LuaxVM* vm = LuaxVM::TryGetVM(state); + LuaxVM* vm = state.GetVM(); if (!vm) return false; if (mMode == STRONG_REF) { @@ -37,13 +37,15 @@ namespace Luax } else { + state.PushNil(); return false; } + return true; } void LuaxRef::SetRef(LuaxState& state, int idx) { - LuaxVM* vm = LuaxVM::TryGetVM(state); + LuaxVM* vm = state.GetVM(); if (!vm) return; if (mMode == STRONG_REF) { diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp index b4d989a..3eae1df 100644 --- a/source/3rd-party/Luax/luax_state.cpp +++ b/source/3rd-party/Luax/luax_state.cpp @@ -1,8 +1,8 @@ -//#include "luax_class.h" #include "luax_enum.h" #include "luax_state.h" #include "luax_vm.h" #include "luax_class.hpp" +#include "luax_internal.h" namespace Luax { @@ -28,6 +28,16 @@ namespace Luax luaL_openlibs(mState); } + global_State* LuaxState::GetGlobalState() + { + return G(mState); + } + + LuaxVM* LuaxState::GetVM() + { + return LuaxVM::TryGetVM(G(mState)); + } + void LuaxState::PushGlobalNamespace() { int top = GetTop(); @@ -642,8 +652,8 @@ namespace Luax return value; } - void LuaxState::PushPtrUserdata(void* ptr) { - + void LuaxState::PushPtrUserdata(void* ptr) + { void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*)); assert(handle); (*handle) = ptr; diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h index dc3b79f..7c7d813 100644 --- a/source/3rd-party/Luax/luax_state.h +++ b/source/3rd-party/Luax/luax_state.h @@ -5,6 +5,7 @@ #include "luax_config.h" #include "luax_reftable.h" +#include "luax_globalstate.h" namespace Luax { @@ -41,6 +42,10 @@ namespace Luax /// inline lua_State* GetHandle() { return mState; }; + global_State* GetGlobalState(); + + LuaxVM* GetVM(); + //------------------------------------------------------------------------------// void OpenLibs(); @@ -206,10 +211,7 @@ namespace Luax void* operator &(); void* operator new(size_t size); - //------------------------------------------------------------------------------// - lua_State* const mState; - }; //--------------------------------------------------------------------------------// diff --git a/source/3rd-party/Luax/luax_vm.cpp b/source/3rd-party/Luax/luax_vm.cpp index 549e20b..5e9d1f5 100644 --- a/source/3rd-party/Luax/luax_vm.cpp +++ b/source/3rd-party/Luax/luax_vm.cpp @@ -1,32 +1,39 @@ +#include "luax_internal.h" #include "luax_vm.h" namespace Luax { - LuaxVM::ThreadMap threadMap; // 通过线程查找虚拟机,为了方便 + LuaxVM::VMap LuaxVM::VMs; // 通过线程查找虚拟机,为了方便 - LuaxVM* LuaxVM::TryGetVM(lua_State* L) + LuaxVM* LuaxVM::TryGetVM(global_State* gState) { - auto it = threadMap.find(L); - if (it != threadMap.end()) + 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); - mThreads.insert(mMainThread); + mGlobalState = G(mMainThread); - threadMap.insert(std::pair(mMainThread, this)); + VMs.insert(std::pair(mGlobalState, this)); } LuaxVM::~LuaxVM() { + VMs.erase(mGlobalState); lua_close(mMainThread); } @@ -34,7 +41,7 @@ namespace Luax void LuaxVM::Setup() { LUAX_STATE(mMainThread); - // 设置全局引用表 + mStrongRefTable.Init(state, "_LUAX_STRONGREF_TABLE"); mWeakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v"); } @@ -43,7 +50,6 @@ namespace Luax { lua_State* thread = lua_newthread(mMainThread); assert(thread); - mThreads.insert(thread); return thread; } @@ -62,14 +68,4 @@ namespace Luax return mWeakRefTable; } - bool LuaxVM::HasThread(lua_State* L) - { - return mThreads.find(L) != mThreads.end(); - } - - int LuaxVM::GetThreadCount() - { - return mThreads.size(); - } - } \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_vm.h b/source/3rd-party/Luax/luax_vm.h index 9039a04..1bac71e 100644 --- a/source/3rd-party/Luax/luax_vm.h +++ b/source/3rd-party/Luax/luax_vm.h @@ -7,6 +7,7 @@ #include "luax_ref.h" #include "luax_config.h" #include "luax_state.h" +#include "luax_globalstate.h" namespace Luax { @@ -17,38 +18,41 @@ namespace Luax class LuaxVM { public: + + /// + /// 根据global_State拿到虚拟机。 + /// + static LuaxVM* TryGetVM(global_State* gState); + static LuaxVM* TryGetVM(lua_State* state); + LuaxVM(); ~LuaxVM(); + /// + /// 创建虚拟机后,需要手动调用Setup函数,初始化一些虚拟机状态。 + /// void Setup(); - lua_State* CreateThread(); lua_State* GetMainThread(); - - int GetThreadCount(); + lua_State* CreateThread(); LuaxRefTable& GetStrongRefTable(); LuaxRefTable& GetWeakRefTable(); - bool HasThread(lua_State* L); - - static LuaxVM* TryGetVM(lua_State* L); - - typedef std::map ThreadMap; - private: - static ThreadMap threadMap; // 通过线程查找虚拟机,为了方便 + typedef std::map VMap; - LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE - LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE + static VMap VMs; // 通过global_State索引虚拟机,为了方便 - lua_State* mMainThread; // 主线程 + LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE + LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE - std::unordered_set mThreads; // 所有线程 + global_State* mGlobalState; // 虚拟机的global_State,由当前虚拟机的所有线程共享 + lua_State* mMainThread; // 主线程 #if LUAX_PROFILER - size_t mObjectCount; // 统计所有在此虚拟机中创建的实例 + size_t mObjectCount; // 统计所有在此虚拟机中创建的实例 #endif }; diff --git a/source/3rd-party/plotscript/plot.c b/source/3rd-party/plotscript/plot.c new file mode 100644 index 0000000..6028671 --- /dev/null +++ b/source/3rd-party/plotscript/plot.c @@ -0,0 +1,5 @@ +/* + * 情节脚本,导出流程符号。情节 环境 人物 +*/ + + diff --git a/source/3rd-party/plotscript/test/first_meeting.plot b/source/3rd-party/plotscript/test/first_meeting.plot new file mode 100644 index 0000000..5cf69d6 --- /dev/null +++ b/source/3rd-party/plotscript/test/first_meeting.plot @@ -0,0 +1,9 @@ + +#但是这个时候 + +@(李白): + +@camera: + move_to(1, 2, 4s) & move_to() + + \ No newline at end of file -- cgit v1.1-26-g67d0