diff options
Diffstat (limited to 'Source/3rdParty')
-rw-r--r-- | Source/3rdParty/Lua51/loadlib.c | 2 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax.h | 1 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_class.hpp | 34 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_class.inl | 161 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_context.cpp | 19 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_context.h | 8 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_enum.cpp | 8 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_memberref.h | 2 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_ref.h | 4 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_runtime.cpp | 20 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_runtime.h | 4 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_state.cpp | 87 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_state.h | 39 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_state.inl | 56 |
14 files changed, 281 insertions, 164 deletions
diff --git a/Source/3rdParty/Lua51/loadlib.c b/Source/3rdParty/Lua51/loadlib.c index 6158c53..9455c53 100644 --- a/Source/3rdParty/Lua51/loadlib.c +++ b/Source/3rdParty/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_setfield(L, -2, "loaded"); // LUA_REGISTRYINDEX["_LOADED"]Ϊpackage.loaded\LUA_ENVIRONINDEX["loaded"] /* set field `preload' */ lua_newtable(L); lua_setfield(L, -2, "preload"); diff --git a/Source/3rdParty/Luax/luax.h b/Source/3rdParty/Luax/luax.h index ed67bdc..9a6b0b9 100644 --- a/Source/3rdParty/Luax/luax.h +++ b/Source/3rdParty/Luax/luax.h @@ -6,6 +6,7 @@ #include "luax_namespace.h" #include "luax_ref.h" #include "luax_reftable.h" +#include "luax_memberref.h" #include "luax_enum.h" #include "luax_class.hpp" #include "luax_class.inl" diff --git a/Source/3rdParty/Luax/luax_class.hpp b/Source/3rdParty/Luax/luax_class.hpp index 0d3dc5b..a76d501 100644 --- a/Source/3rdParty/Luax/luax_class.hpp +++ b/Source/3rdParty/Luax/luax_class.hpp @@ -53,7 +53,9 @@ namespace Luax /// /// userdata pushջûгʼmUserdataʼúԪѳʼõuserdataջһá /// - bool PushLuaUserdata(LuaxState& state); + bool PushLuaxUserdata(LuaxState& state); + bool PushLuaxMemberTable(LuaxState& state); + bool PushLuaxRefTable(LuaxState& state); private: @@ -62,15 +64,15 @@ namespace Luax static void RegisterLuaxClass(LuaxState& state); static void RegisterLuaxFactoryClass(LuaxState& state); static void RegisterLuaxSingletonClass(LuaxState& state); - static void RegisterLuaxInterface(LuaxState& state); - static void SetInterfaceTableRef(LuaxState& state, int idx); - static void SetClassTableRef(LuaxState& state, int idx); + static void RegisterLuaxInterface(LuaxState& state); - static void PushInterfaceTable(LuaxState& state); - static void PushClassTable(LuaxState& state); - static void PushRefTable(LuaxState& state); + static void SetLuaxInterfaceTableRef(LuaxState& state, int idx); + static void SetLuaxClassTableRef(LuaxState& state, int idx); + static void PushLuaxInterfaceTable(LuaxState& state); + static void PushLuaxClassTable(LuaxState& state); + /// /// ȡַҪַֻͨڶϴʵõջϺ;̬ıȡַ֤üȷ /// ҪãʹôݶǴݵַ @@ -87,12 +89,16 @@ namespace Luax /// /// LuaxClass<T>͵ʵ /// - static LuaxStrongRef mInterfaceTable; // interface table - static LuaxStrongRef mClassTable; // class table - static LuaxStrongRef mRefTable; // - + static LuaxStrongRef mClassTable; // class table͵ + static LuaxStrongRef mInterfaceTable; // ǹinterface tableʵĶĹз + static LuaxStrongRef mSingletonRefTable; // ǵsingletonijԱԱ֤ᱻͨ + // ref tableijԱȫڵģֱ_LUAX_STRONGREF_TABLE + /// - /// ͨuserdataõref table\member table\interface table + /// ͨuserdataõ: + /// 1: ref table + /// 2: member table + /// 3: interface table /// LuaxWeakRef mUserdata; @@ -113,13 +119,13 @@ namespace Luax LUAX_DECL_METHOD( l_GetClassName ); LUAX_DECL_METHOD( l_GetInterfaceTable ); - LUAX_DECL_METHOD( l_ToString ); + LUAX_DECL_METHOD( l___tostring ); //------------------------------------------------------------------------------------------------------------ // LUAX_DECL_METHOD( l_ExtendFactory ); - LUAX_DECL_METHOD( l_GC ); + LUAX_DECL_METHOD( l___gc ); //------------------------------------------------------------------------------------------------------------ // diff --git a/Source/3rdParty/Luax/luax_class.inl b/Source/3rdParty/Luax/luax_class.inl index 2d28d68..8cf0f55 100644 --- a/Source/3rdParty/Luax/luax_class.inl +++ b/Source/3rdParty/Luax/luax_class.inl @@ -56,8 +56,8 @@ namespace Luax void LuaxClass<T>::RegisterLuaxInterface(LuaxState& state) { luaL_Reg regTable[] = { - { "__gc", l_GC }, - { NULL, NULL } + { "__gc", l___gc }, + { NULL, NULL } }; state.RegisterMethods(regTable); @@ -78,7 +78,7 @@ namespace Luax } template<typename T> - void LuaxClass<T>::PushInterfaceTable(LuaxState& state) + void LuaxClass<T>::PushLuaxInterfaceTable(LuaxState& state) { assert(mInterfaceTable); @@ -86,7 +86,7 @@ namespace Luax } template<typename T> - void LuaxClass<T>::PushClassTable(LuaxState& state) + void LuaxClass<T>::PushLuaxClassTable(LuaxState& state) { assert(mClassTable); @@ -94,21 +94,13 @@ namespace Luax } template<typename T> - void LuaxClass<T>::PushRefTable(LuaxState& state) - { - assert(mRefTable); - - mRefTable.Push(state); - } - - template<typename T> - void LuaxClass<T>::SetInterfaceTableRef(LuaxState& state, int idx) + void LuaxClass<T>::SetLuaxInterfaceTableRef(LuaxState& state, int idx) { mInterfaceTable.SetRef(state, idx); } template<typename T> - void LuaxClass<T>::SetClassTableRef(LuaxState& state, int idx) + void LuaxClass<T>::SetLuaxClassTableRef(LuaxState& state, int idx) { mClassTable.SetRef(state, idx); } @@ -143,8 +135,9 @@ namespace Luax } template<typename T> - bool LuaxClass<T>::PushLuaUserdata(LuaxState& state) + bool LuaxClass<T>::PushLuaxUserdata(LuaxState& state) { + assert(!T::IsLuaxClassSingleton()); if (!mUserdata) { BindToLua(state); @@ -154,58 +147,102 @@ namespace Luax } template<typename T> + bool LuaxClass<T>::PushLuaxMemberTable(LuaxState& state) + { + int top = state.GetTop(); + if (this->PushLuaxUserdata(state)) + { + if (lua_getmetatable(state, -1)) // ref table + { + lua_replace(state, -2); + if (lua_getmetatable(state, -1)) // member table + { + lua_replace(state, -2); + return true; + } + } + } + state.SetTop(top); + lua_pushnil(state); + return false; + } + + template<typename T> + bool LuaxClass<T>::PushLuaxRefTable(LuaxState& state) + { + // Singleton + if (T::IsLuaxClassSingleton()) + { + if (!this->mSingletonRefTable) { + lua_newtable(state); + this->mSingletonRefTable.SetRef(state, -1); // strong ref to member table won't be garbage collected + } + else { + this->mSingletonRefTable.PushRef(state); + } + return true; + } + // Factory + else + { + if (this->PushLuaxUserdata(state)) + { + if (lua_getmetatable(state, -1)) + { + lua_replace(state, -2); + return true; + } + } + } + return false; + } + + template<typename T> void LuaxClass<T>::BindToLua(LuaxState& state) { + // ܰuserdata + assert(!T::IsLuaxClassSingleton()); assert(!mUserdata); // userdataջ state.PushPtrUserData(this); - // - if (!T::IsLuaxClassSingleton()) - { - lua_newtable(state); // ref table - lua_newtable(state); // member table - LuaxClass<T>::PushInterfaceTable(state); // interface table - - // stack: - // -1: interface table - // -2: member table - // -3: ref table - // -4: userdata - - int top = state.GetTop(); - int memberTable = top - 1; - int refTable = top - 2; - - // ref table ע __gc__tostring - lua_pushcfunction(state, LuaxClass<T>::l_GC); - lua_setfield(state, refTable, "__gc"); - - lua_pushcfunction(state, LuaxClass<T>::l_ToString); - lua_setfield(state, refTable, "__tostring"); - - // member table Ϊ ref table __index __newindex - lua_pushvalue(state, memberTable); - lua_setfield(state, refTable, "__index"); - - lua_pushvalue(state, memberTable); - lua_setfield(state, refTable, "__newindex"); - - // Ԫ - lua_setmetatable(state, -2); // interface is meta of member - lua_setmetatable(state, -2); // member is meta of ref - lua_setmetatable(state, -2); // ref is meta of userdata - } + lua_newtable(state); // ref tableluaʣC + lua_newtable(state); // member tableluaдĶԱ + PushLuaxInterfaceTable(state); // interface tablemember tableûԱinterface tableҡ - // һuserdataãͨnativeָ뷵lua - mUserdata.SetRef(state, -1); - assert(mUserdata); + // stack: + // -1: interface table + // -2: member table + // -3: ref table + // -4: userdata - if (T::IsLuaxClassSingleton()) - { + int top = state.GetTop(); + int memberTable = top - 1; + int refTable = top - 2; - } + // ref table ע __gc__tostring + lua_pushcfunction(state, l___gc); + lua_setfield(state, refTable, "__gc"); + + lua_pushcfunction(state, l___tostring); + lua_setfield(state, refTable, "__tostring"); + + // member table Ϊ ref table __index __newindex + lua_pushvalue(state, memberTable); + lua_setfield(state, refTable, "__index"); + + lua_pushvalue(state, memberTable); + lua_setfield(state, refTable, "__newindex"); + + // Ԫ + lua_setmetatable(state, -2); // interface is meta of member + lua_setmetatable(state, -2); // member is meta of ref + lua_setmetatable(state, -2); // ref is meta of userdata + + // һuserdataãͨPushLuaUserdatalua + mUserdata.SetRef(state, -1); + assert(mUserdata); } //-------------------------------------------------------------------------------------------------------------- @@ -214,7 +251,7 @@ namespace Luax /// ͷŹʵ /// template<typename T> - int LuaxClass<T>::l_GC(lua_State* L) + int LuaxClass<T>::l___gc(lua_State* L) { LUAX_SETUP(L, "U"); @@ -222,7 +259,7 @@ namespace Luax } template<typename T> - int LuaxClass<T>::l_ToString(lua_State* L) + int LuaxClass<T>::l___tostring(lua_State* L) { return 0; } @@ -242,8 +279,8 @@ namespace Luax return 0; } - template<typename T> LuaxStrongRef LuaxClass<T>::mInterfaceTable; // interface table - template<typename T> LuaxStrongRef LuaxClass<T>::mClassTable; // class table - template<typename T> LuaxStrongRef LuaxClass<T>::mRefTable; // + template<typename T> LuaxStrongRef LuaxClass<T>::mInterfaceTable; // interface table + template<typename T> LuaxStrongRef LuaxClass<T>::mClassTable; // class table + template<typename T> LuaxStrongRef LuaxClass<T>::mSingletonRefTable; // }
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_context.cpp b/Source/3rdParty/Luax/luax_context.cpp index c0fb2f5..d41c24f 100644 --- a/Source/3rdParty/Luax/luax_context.cpp +++ b/Source/3rdParty/Luax/luax_context.cpp @@ -3,34 +3,27 @@ namespace Luax { - // ˴˺ - int l_Errfunc(lua_State* L) - { - cc8* err = lua_tostring(L, lua_upvalueindex(1)); - return luaL_error(L, err); - } - - Context::Context(lua_State* L) + LuaxContext::LuaxContext(lua_State* L) : state(L) { assert(state); } - Context::~Context() + LuaxContext::~LuaxContext() { } // ʼcontext - void Context::Setup() + void LuaxContext::Setup() { SetupRefTables(); } - void Context::SetupRefTables() + void LuaxContext::SetupRefTables() { // strong ref weak ref - strongRefTable.Init(state, "LUAX_STRONGREF_TABLE"); - weakRefTable.Init(state, "LUAX_WEAKREF_TABLE", "v"); + strongRefTable.Init(state, "_LUAX_STRONGREF_TABLE"); + weakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v"); } }
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_context.h b/Source/3rdParty/Luax/luax_context.h index 5746cf1..01c5937 100644 --- a/Source/3rdParty/Luax/luax_context.h +++ b/Source/3rdParty/Luax/luax_context.h @@ -11,11 +11,11 @@ namespace Luax /// /// lua_stateصcontextһϵдļϣҲûϵҪΪ˽Լڴ档 /// - class Context + class LuaxContext { public: - Context(lua_State* L); - ~Context(); + LuaxContext(lua_State* L); + ~LuaxContext(); void Setup(); @@ -23,7 +23,7 @@ namespace Luax LuaxRefTable strongRefTable; // strong ref table LuaxRefTable weakRefTable; // weak ref table - size_t objectCount; // ͳڴstateдʵ + size_t objectCount; // ͳڴstateдʵ private: diff --git a/Source/3rdParty/Luax/luax_enum.cpp b/Source/3rdParty/Luax/luax_enum.cpp index 1ccc900..a58593a 100644 --- a/Source/3rdParty/Luax/luax_enum.cpp +++ b/Source/3rdParty/Luax/luax_enum.cpp @@ -11,11 +11,11 @@ namespace Luax int l_rmt__index(lua_State* L) { // params: - // 1. enum table - // 2. key + // 1: enum table + // 2: key // upvalues: - // 1. metatable + // 1: metatable int mt = lua_upvalueindex(1); lua_pushvalue(L, 2); @@ -27,7 +27,7 @@ namespace Luax int l_rmt__newindex(lua_State* L) { // upvalue: - // 1. enum table name + // 1: enum table name cc8* name = lua_tostring(L, lua_upvalueindex(1)); diff --git a/Source/3rdParty/Luax/luax_memberref.h b/Source/3rdParty/Luax/luax_memberref.h index baebd06..a7ff1f6 100644 --- a/Source/3rdParty/Luax/luax_memberref.h +++ b/Source/3rdParty/Luax/luax_memberref.h @@ -5,7 +5,7 @@ namespace Luax { /// - /// LuaxClassijԱãinterface table֤ȷͷţǿá + /// LuaxClassijԱãref table֤ȷͷţǿá /// class LuaxMemberRef { diff --git a/Source/3rdParty/Luax/luax_ref.h b/Source/3rdParty/Luax/luax_ref.h index 7b484e9..b7aabe5 100644 --- a/Source/3rdParty/Luax/luax_ref.h +++ b/Source/3rdParty/Luax/luax_ref.h @@ -38,7 +38,7 @@ namespace Luax }; /// - /// ǿãLUA_REGISTRYINDEX["LUAX_STRONGREF_TABLE"] + /// ǿãLUA_REGISTRYINDEX["_LUAX_STRONGREF_TABLE"]֤lua objectᱻա /// class LuaxStrongRef: public LuaxRef { @@ -48,7 +48,7 @@ namespace Luax }; /// - /// ãLUA_REGISTRYINDEX["LUAX_WEAKREF_TABLE"] + /// ãLUA_REGISTRYINDEX["_LUAX_WEAKREF_TABLE"]Ӱlua objectĻգֻΪһȡlua objectӳ䡣 /// class LuaxWeakRef : public LuaxRef { diff --git a/Source/3rdParty/Luax/luax_runtime.cpp b/Source/3rdParty/Luax/luax_runtime.cpp index 47b20cb..b45d36a 100644 --- a/Source/3rdParty/Luax/luax_runtime.cpp +++ b/Source/3rdParty/Luax/luax_runtime.cpp @@ -26,9 +26,9 @@ namespace Luax lua_State* L = lua_open(); assert(L); - // 1. - mContexts.insert(pair<lua_State*, Context>(L, Context(L))); - // 2. ʼcontext + // 1) + mContexts.insert(pair<lua_State*, LuaxContext>(L, LuaxContext(L))); + // 2) ʼcontext (*this)[L].Setup(); return L; @@ -36,7 +36,7 @@ namespace Luax void LuaxRuntime::Close(lua_State* L) { - map<lua_State*, Context>::iterator it = mContexts.find(L); + map<lua_State*, LuaxContext>::iterator it = mContexts.find(L); if (it != mContexts.end()) { lua_close(it->second.state); @@ -46,13 +46,13 @@ namespace Luax bool LuaxRuntime::HasLuaxState(lua_State* L) { - map<lua_State*, Context>::iterator it = mContexts.find(L); + map<lua_State*, LuaxContext>::iterator it = mContexts.find(L); return it != mContexts.end(); } LuaxState& LuaxRuntime::GetLuaxState(lua_State* L) { - map<lua_State*, Context>::iterator it = mContexts.find(L); + map<lua_State*, LuaxContext>::iterator it = mContexts.find(L); if (it != mContexts.end()) { return it->second.state; @@ -61,7 +61,7 @@ namespace Luax LuaxRefTable& LuaxRuntime::GetStrongRefTable(lua_State* L) { - map<lua_State*, Context>::iterator it = mContexts.find(L); + map<lua_State*, LuaxContext>::iterator it = mContexts.find(L); if (it != mContexts.end()) { return it->second.strongRefTable; @@ -70,16 +70,16 @@ namespace Luax LuaxRefTable& LuaxRuntime::GetWeaksRefTable(lua_State* L) { - map<lua_State*, Context>::iterator it = mContexts.find(L); + map<lua_State*, LuaxContext>::iterator it = mContexts.find(L); if (it != mContexts.end()) { return it->second.weakRefTable; } } - Context& LuaxRuntime::operator[](lua_State* L) + LuaxContext& LuaxRuntime::operator[](lua_State* L) { - map<lua_State*, Context>::iterator it = mContexts.find(L); + map<lua_State*, LuaxContext>::iterator it = mContexts.find(L); assert(it != mContexts.end()); return it->second; } diff --git a/Source/3rdParty/Luax/luax_runtime.h b/Source/3rdParty/Luax/luax_runtime.h index cb8b66b..1234627 100644 --- a/Source/3rdParty/Luax/luax_runtime.h +++ b/Source/3rdParty/Luax/luax_runtime.h @@ -35,7 +35,7 @@ namespace Luax LuaxRefTable& GetStrongRefTable(lua_State* L); LuaxRefTable& GetWeaksRefTable(lua_State* L); - Context& operator[](lua_State* L); + LuaxContext& operator[](lua_State* L); private: @@ -47,7 +47,7 @@ namespace Luax /// /// lua_State handlecontextӳ /// - std::map<lua_State*, Context> mContexts; + std::map<lua_State*, LuaxContext> mContexts; }; diff --git a/Source/3rdParty/Luax/luax_state.cpp b/Source/3rdParty/Luax/luax_state.cpp index 5253a2c..1414591 100644 --- a/Source/3rdParty/Luax/luax_state.cpp +++ b/Source/3rdParty/Luax/luax_state.cpp @@ -27,23 +27,47 @@ namespace Luax luaL_openlibs(mState); } + void LuaxState::PushGlobalNamespace() + { + int top = GetTop(); + + lua_newtable(mState); // pseudo namespace table + int pnt = GetTop(); + + lua_newtable(mState); // metatable + int mt = GetTop(); + + // __index = _G + // __newindex = _G + lua_pushvalue(mState, LUA_GLOBALSINDEX); + lua_pushvalue(mState, LUA_GLOBALSINDEX); + lua_setfield(mState, mt, "__index"); + lua_setfield(mState, mt, "__newindex"); + + lua_setmetatable(mState, pnt); + + // stack: + // -1 pseudo global namespace + } + void LuaxState::PushNamespace(cc8* name) { - bool isG = !lua_istable(mState, -1); - int idx = isG ? LUA_GLOBALSINDEX : -1; - lua_getfield(mState, idx, name); + assert(IsNamespace(-1)); + + int top = GetTop(); + + lua_getfield(mState, -1, name); if (lua_isnil(mState, -1)) { lua_pop(mState, 1); + lua_newtable(mState); - assert(lua_istable(mState, -1)); lua_pushvalue(mState, -1); - int t = isG ? LUA_GLOBALSINDEX : -3; - lua_setfield(mState, t, name); + lua_setfield(mState, top, name); } // stack: - // -1 namespace + // -1 namespace } void LuaxState::PopNamespace() @@ -52,6 +76,11 @@ namespace Luax lua_pop(mState, 1); } + bool LuaxState::IsNamespace(int idx) + { + return lua_istable(mState, idx); + } + void LuaxState::DoString(const std::string& code) { luaL_dostring(mState, code.c_str()); @@ -146,11 +175,6 @@ namespace Luax lua_pop(mState, n); } - void LuaxState::Settop(int idx) - { - lua_settop(mState, idx); - } - bool LuaxState::IsNil(int idx) { return lua_isnil(mState, idx); @@ -191,6 +215,11 @@ namespace Luax return (mState != 0); } + void LuaxState::Settop(int idx) + { + lua_settop(mState, idx); + } + int LuaxState::GetTop() { return lua_gettop(mState); @@ -244,11 +273,6 @@ namespace Luax return false; } - void LuaxState::RegisterMethods(const luaL_Reg *l) - { - luaL_register(mState, 0, l); - } - void LuaxState::GetField(int idx, cc8* name) { lua_getfield(mState, idx, name); @@ -646,4 +670,33 @@ namespace Luax lua_setfield(L, top, name); } + + void LuaxState::RegisterMethods(const luaL_Reg *l) + { + assert(lua_istable(mState, -1)); + // luaL_registerڶΪգ-1λעluaL_RegЩ + luaL_register(mState, 0, l); + } + + void LuaxState::RegisterMethod(cc8* fname, lua_CFunction func) + { + assert(lua_istable(mState, -1)); + lua_pushcfunction(mState, func); + lua_setfield(mState, -1, fname); + } + + void LuaxState::RegisterPreloader(cc8* libname, lua_CFunction preloader) + { + lua_getglobal(mState, "package"); + lua_getfield(mState, -1, "preload"); + lua_pushcfunction(mState, preloader); + lua_setfield(mState, -2, libname); + lua_pop(mState, 2); + } + + void LuaxState::RegisterLib(cc8* libname, const luaL_Reg* l) + { + luaL_register(mState, libname, l); + } + }
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_state.h b/Source/3rdParty/Luax/luax_state.h index 166ac26..a936ea6 100644 --- a/Source/3rdParty/Luax/luax_state.h +++ b/Source/3rdParty/Luax/luax_state.h @@ -9,7 +9,7 @@ namespace Luax { - class Context; + class LuaxContext; class LuaxEnum; class LuaxStrongRef; class LuaxWeakRef; @@ -43,11 +43,14 @@ namespace Luax //------------------------------------------------------------------------------------------------------------ // ƿռƿռһ_Gı + void PushGlobalNamespace(); void PushNamespace(cc8* name); void PopNamespace(); - + bool IsNamespace(int idx); + //------------------------------------------------------------------------------------------------------------ + void SetTop(int top); int GetTop(); bool CheckParams(int idx, cc8* format); int AbsIndex(int idx); @@ -55,11 +58,6 @@ namespace Luax //------------------------------------------------------------------------------------------------------------ - /// - /// עCעһ{0 0} - /// - void RegisterMethods(const luaL_Reg *l); - void GetField(int idx, cc8* name); void GetField(int idx, int key); std::string GetField(int idx, cc8* key, cc8* value); @@ -126,6 +124,7 @@ namespace Luax void DoFile(const std::string& file); //------------------------------------------------------------------------------------------------------------ + // ע᷽ /// /// עṤͨ࣬New @@ -142,9 +141,33 @@ namespace Luax /// void RegisterEnum(cc8* name, LuaxEnum* enums); + /// + /// עCעһ{0 0} + /// + void RegisterMethods(const luaL_Reg *l); + + /// + /// עᵥC + /// + void RegisterMethod(cc8* fname, lua_CFunction func); + + /// + /// preloaderӵpackage.preloadrequire"libname"ʱlualoader_preloadlibnameҵpreloaderֱӼء + /// ʵҪrequireʱżأҼعһκpackage.loaded¼´βټءͨrequire + /// preloader + /// + void RegisterPreloader(cc8* libname, lua_CFunction preloader); + + /// + /// luaL_Reglib table_Gpackage.loadedlibnameָlib table + /// + void RegisterLib(cc8* libname, const luaL_Reg* l); + + //------------------------------------------------------------------------------------------------------------ + private: - friend class Context; + friend class LuaxContext; LuaxState(lua_State* state); LuaxState(const LuaxState& state); diff --git a/Source/3rdParty/Luax/luax_state.inl b/Source/3rdParty/Luax/luax_state.inl index b5ee5aa..bd7ca9a 100644 --- a/Source/3rdParty/Luax/luax_state.inl +++ b/Source/3rdParty/Luax/luax_state.inl @@ -1,10 +1,10 @@ namespace Luax { - // ͨ͵вͬע̣ͨLuaxStateRegister_ʵ - - // עṤΪעinterface tableclass tabletype nameΪƿռϡעβԪȵNewõʱŻᡣ - + /// + /// עṤΪעinterface tableclass tabletype nameΪƿռϡעβԪȵNew + /// õʱŻᡣ + /// template<typename T> void LuaxState::RegisterFactory() { @@ -12,56 +12,51 @@ namespace Luax LuaxState& state = *this; int top = lua_gettop(L); // namespace table - assert(lua_istable(L, top)); - const char* type = T::GetLuaxFactoryName(); - - // interface table + // 1) interface table lua_newtable(L); + LuaxClass<T>::RegisterLuaxInterface(state); + T::RegisterLuaxInterface(state); // interface table[__index] = interface table lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - LuaxClass<T>::RegisterLuaxInterface(state); - T::RegisterLuaxInterface(state); - - LuaxClass<T>::SetInterfaceTableRef(state, -1); + LuaxClass<T>::SetLuaxInterfaceTableRef(state, -1); lua_settop(L, top); - // class table + // 2) class table lua_newtable(L); - LuaxClass<T>::RegisterLuaxClass(state); LuaxClass<T>::RegisterLuaxFactoryClass(state); T::RegisterLuaxClass(state); // TǷûעķ -#define _assertmethod(I, NAME) \ - GetField(I, NAME); \ - assert(IsType(-1, LUA_TFUNCTION)); \ - Pop(); + #define _assertmethod(I, NAME) \ + GetField(I, NAME); \ + assert(IsType(-1, LUA_TFUNCTION)); \ + Pop(); - // NewûУûеĻʾһ - //_assertmethod(-1, "New"); + _assertmethod(-1, "New"); -#undef _assertmethod + #undef _assertmethod // .Extend() lua_pushvalue(state, -1); // class table - LuaxClass<T>::PushInterfaceTable(state); // interface table + LuaxClass<T>::PushLuaxInterfaceTable(state); // interface table lua_pushcclosure(state, LuaxClass<T>::l_ExtendFactory, 2); lua_setfield(state, -2, "Extend"); // .GetInterfaceTable() - LuaxClass<T>::PushInterfaceTable(state); // interface table + LuaxClass<T>::PushLuaxInterfaceTable(state); // interface table lua_pushcclosure(state, LuaxClass<T>::l_GetInterfaceTable, 1); lua_setfield(state, -2, "GetInterfaceTable"); - LuaxClass<T>::SetClassTableRef(state, -1); + LuaxClass<T>::SetLuaxClassTableRef(state, -1); + const char* type = T::GetLuaxFactoryName(); SetField(top, type); // reset top @@ -71,7 +66,9 @@ namespace Luax T::RegisterLuaxPostprocess(state); } - // עᵥ + /// + /// Singletonֻһclass tableûinterface table + /// template<typename T> void LuaxState::RegisterSingleton() { @@ -85,11 +82,18 @@ namespace Luax // class table. lua_newtable(L); - LuaxClass<T>::RegisterLuaxClass(state); LuaxClass<T>::RegisterLuaxFactoryClass(state); T::RegisterLuaxClass(state); + LuaxClass<T>::SetLuaxClassTableRef(state, -1); + + // class table__index__newindexָ + lua_pushvalue(state, -1); + lua_setfield(state, -2, "__index"); + lua_pushvalue(state, -1); + lua_setfield(state, -2, "__newindex"); + SetField(top, type); // reset top |