diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/3rdParty/Luax/luax_class.cpp | 104 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_class.hpp | 31 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_class.inl | 50 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_enum.h | 10 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_state.cpp | 8 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_state.h | 12 | ||||
-rw-r--r-- | Source/3rdParty/Luax/luax_state.inl | 30 | ||||
-rw-r--r-- | Source/Samples/LuaxTest/main.cpp | 31 | ||||
-rw-r--r-- | Source/Samples/LuaxTest/script.lua | 19 |
9 files changed, 196 insertions, 99 deletions
diff --git a/Source/3rdParty/Luax/luax_class.cpp b/Source/3rdParty/Luax/luax_class.cpp new file mode 100644 index 0000000..bdff377 --- /dev/null +++ b/Source/3rdParty/Luax/luax_class.cpp @@ -0,0 +1,104 @@ +#include "luax_class.hpp" +#include "luax_runtime.h" +#include "luax_cfunctions.h" + +namespace Luax +{ + + int LuaxPlainClass::registry(lua_State* L) + { + LUAX_STATE(L); + + // params: + // 1: class name + + cc8* type = state.GetValue<cc8*>(1, ""); + + lua_newtable(L); // class table + + // GetClassName() + lua_pushstring(L, type); + lua_pushcclosure(L, luax_c_getupvalue, 1); + lua_setfield(L, -2, "GetClassName"); + + // New() + lua_pushvalue(L, -1); // class table + lua_pushcclosure(L, l_New, 1); + lua_setfield(L, -2, "New"); + + // Extend() + + lua_pushvalue(L, -1); // class table + lua_setfield(L, -2, "__index"); + + return 1; + } + + /// + /// NewnԻȡCtorCtorʼʵ + /// + int LuaxPlainClass::l_New(lua_State* L) + { + LUAX_STATE(L); + + // upvalues: + // 1: class table + + // params: + // n: params + int n = lua_gettop(L); + + int classTable = lua_upvalueindex(1); + + lua_newtable(L); // instance table + + // instance metatable Ϊ class + lua_pushvalue(L, classTable); + lua_setmetatable(L, -2); + + lua_getfield(L, classTable, "Ctor"); + if (!lua_isnil(L, -1)) + { + // stack: + // -1: Ctor() + // -2: instance + // -3~-n-2: params + + lua_insert(L, -2 - n); + // stack: + // -1: instance + // -2~-n-1: params + // -n-2: Ctor() + + lua_pushvalue(L, -1); + // stack: + // -1: instance + // -2: instance + // -3~-n-2: params + // -n-3: Ctor + + lua_insert(L, -3 - n); + // stack: + // -1: instance + // -2~-n-1: params + // -n-2: Ctor() + // -n-3: instance + + lua_insert(L, -1 - n); + // stack: + // -1~-n: params + // -n-1: instance + // -n-2: Ctor() + // -n-3: instance + + lua_pcall(L, n + 1, 0, 0); + } + else + { + state.Pop(); + } + + return 1; + } + +}
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_class.hpp b/Source/3rdParty/Luax/luax_class.hpp index 539093c..6557c0b 100644 --- a/Source/3rdParty/Luax/luax_class.hpp +++ b/Source/3rdParty/Luax/luax_class.hpp @@ -14,12 +14,10 @@ namespace Luax /// /// RegisterLuaxClass עķͳԱö١ȵclass table - /// LuaxRegisterInterface עӿڷinterface table /// LuaxGetFactoryName ùͬʱעʱעΪsingletonͨʱ /// #define LUAX_DECL_FACTORY(type) \ static void RegisterLuaxClass(Luax::LuaxState&);\ - static void RegisterLuaxInterface(Luax::LuaxState&);\ static void RegisterLuaxPostprocess(Luax::LuaxState&); \ static const char* GetLuaxFactoryName() { return #type; };\ static const char* GetLuaxClassName() { return #type; };\ @@ -37,7 +35,7 @@ namespace Luax static bool IsLuaxClassSingleton() { return true; }; /// - /// Ҫ¶luaclassҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷͷš + /// Ҫ¶luanative classҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷͷš /// template<class T> class LuaxClass @@ -81,12 +79,8 @@ namespace Luax static void RegisterLuaxFactoryClass(LuaxState& state); static void RegisterLuaxSingletonClass(LuaxState& state); - static void RegisterLuaxInterface(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); /// @@ -106,7 +100,6 @@ namespace Luax /// LuaxClass<T>͵ʵ /// static LuaxStrongRef mClassTable; // class table͵ - static LuaxStrongRef mInterfaceTable; // ǹinterface tableʵĶĹз static LuaxStrongRef mSingletonRefTable; // ǵsingletonijԱԱ֤ᱻͨ // ref tableijԱȫڵģֱ_LUAX_STRONGREF_TABLE @@ -114,7 +107,7 @@ namespace Luax /// ͨuserdataõ: /// 1: ref table /// 2: member table - /// 3: interface table + /// 3: class table /// LuaxWeakRef mUserdata; @@ -132,7 +125,6 @@ namespace Luax LUAX_DECL_METHOD( l___gc ); LUAX_DECL_METHOD( l_ExtendFactory ); LUAX_DECL_METHOD( l_GetClass ); - LUAX_DECL_METHOD( l_GetInterfaceTable ); LUAX_DECL_METHOD( l_GetRefTable ); //------------------------------------------------------------------------------------------------------------ @@ -142,6 +134,25 @@ namespace Luax }; + //-------------------------------------------------------------------------------------------------------------- + + /// + /// lua + /// + class LuaxPlainClass + { + public: + + /// + /// עںͨregistry()עࡣ + /// + static int registry(lua_State* L); + + LUAX_DECL_METHOD( l_Extend ); + LUAX_DECL_METHOD( l_New ); + + }; + } #endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_class.inl b/Source/3rdParty/Luax/luax_class.inl index bbda6de..6086b3d 100644 --- a/Source/3rdParty/Luax/luax_class.inl +++ b/Source/3rdParty/Luax/luax_class.inl @@ -40,20 +40,6 @@ namespace Luax void LuaxClass<T>::RegisterLuaxFactoryClass(LuaxState& state) { luaL_Reg regTable[] = { - { "GetInterfaceTable", l_GetInterfaceTable }, - { NULL, NULL } - }; - - state.RegisterMethods(regTable); - } - - /// - /// ʵijԱעinterface table - /// - template<typename T> - void LuaxClass<T>::RegisterLuaxInterface(LuaxState& state) - { - luaL_Reg regTable[] = { { "GetClass", l_GetClass }, { "GetRefTable", l_GetRefTable }, { NULL, NULL } @@ -76,14 +62,6 @@ namespace Luax } template<typename T> - void LuaxClass<T>::PushLuaxInterfaceTable(LuaxState& state) - { - assert(mInterfaceTable); - - mInterfaceTable.PushRef(state); - } - - template<typename T> void LuaxClass<T>::PushLuaxClassTable(LuaxState& state) { assert(mClassTable); @@ -92,12 +70,6 @@ namespace Luax } template<typename T> - void LuaxClass<T>::SetLuaxInterfaceTableRef(LuaxState& state, int idx) - { - mInterfaceTable.SetRef(state, idx); - } - - template<typename T> void LuaxClass<T>::SetLuaxClassTableRef(LuaxState& state, int idx) { mClassTable.SetRef(state, idx); @@ -221,10 +193,10 @@ namespace Luax } /// - /// userdataԴref tablemember tableinterface table + /// userdataԴref tablemember tableclass table /// ref table kvǿtableuserdataüͨuserdataΪkeyΪvalueԼԱ /// member table luaʵijԱ - /// interface table б͵ʵеijԱ + /// class table б͵ʵеĺ /// template<typename T> void LuaxClass<T>::BindToLua(LuaxState& state) @@ -238,10 +210,10 @@ namespace Luax lua_newtable(state); // ref tableluaʣC lua_newtable(state); // member tableluaдĶԱ - PushLuaxInterfaceTable(state); // interface tablemember tableûԱinterface tableҡ + PushLuaxClassTable(state); // class table // stack: - // -1: interface table + // -1: class table // -2: member table // -3: ref table // -4: userdata @@ -265,7 +237,7 @@ namespace Luax lua_setfield(state, refTable, "__newindex"); // Ԫ - lua_setmetatable(state, -2); // interface is meta of member + lua_setmetatable(state, -2); // class is meta of member lua_setmetatable(state, -2); // member is meta of ref lua_setmetatable(state, -2); // ref is meta of userdata @@ -388,17 +360,6 @@ namespace Luax return 0; } - template<typename T> - int LuaxClass<T>::l_GetInterfaceTable(lua_State* L) - { - LUAX_STATE(L); - if (!mInterfaceTable) - lua_pushnil(L); - else - mInterfaceTable.PushRef(state); - return 1; - } - template<typename T> int LuaxClass<T>::l_GetClass(lua_State* L) { @@ -421,7 +382,6 @@ namespace Luax return 1; } - 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; // diff --git a/Source/3rdParty/Luax/luax_enum.h b/Source/3rdParty/Luax/luax_enum.h index bb96102..17fcda4 100644 --- a/Source/3rdParty/Luax/luax_enum.h +++ b/Source/3rdParty/Luax/luax_enum.h @@ -19,6 +19,16 @@ namespace Luax extern int l_rmt__newindex(lua_State* L); + //-------------------------------------------------------------------------------------------------------------- + + /// + /// luaö٣ĵtable + /// + class LuaxPlainEnum + { + + }; + } #endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_state.cpp b/Source/3rdParty/Luax/luax_state.cpp index a60a021..0913ee3 100644 --- a/Source/3rdParty/Luax/luax_state.cpp +++ b/Source/3rdParty/Luax/luax_state.cpp @@ -2,6 +2,7 @@ #include "luax_enum.h" #include "luax_state.h" #include "luax_runtime.h" +#include "luax_class.hpp" namespace Luax { @@ -699,4 +700,11 @@ namespace Luax luaL_register(mState, libname, l); } + void LuaxState::RegisterPlainClassRegistry(cc8* name) + { + assert(lua_istable(mState, -1)); + lua_pushcfunction(mState, LuaxPlainClass::registry); + lua_setfield(mState, -2, name); + } + }
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_state.h b/Source/3rdParty/Luax/luax_state.h index 2bcfd9b..fee9d8a 100644 --- a/Source/3rdParty/Luax/luax_state.h +++ b/Source/3rdParty/Luax/luax_state.h @@ -16,7 +16,7 @@ namespace Luax /// /// lua_StateĴ˱һlua_Stateòݡһʵmetatable£ - /// interface table + /// class table /// member table /// ref table /// userdata @@ -163,6 +163,16 @@ namespace Luax /// void RegisterLib(cc8* libname, const luaL_Reg* l); + /// + /// עᴿluaעắluaࡣ + /// + void RegisterPlainClassRegistry(cc8* name); + + /// + /// עᴿluaö٣Էֹöֵ + /// + void RegisterPlainEnumRegistry(cc8* name); + //------------------------------------------------------------------------------------------------------------ private: diff --git a/Source/3rdParty/Luax/luax_state.inl b/Source/3rdParty/Luax/luax_state.inl index c9a95f6..63f849f 100644 --- a/Source/3rdParty/Luax/luax_state.inl +++ b/Source/3rdParty/Luax/luax_state.inl @@ -2,8 +2,7 @@ namespace Luax { /// - /// עṤΪעinterface tableclass tabletype nameΪƿռϡעβԪȵNew - /// õʱŻᡣ + /// עṤעclass tabletype nameΪƿռϡעβԪȵNewõʱŻᡣ /// template<typename T> void LuaxState::RegisterFactory() @@ -14,20 +13,7 @@ namespace Luax int top = lua_gettop(L); // namespace table assert(lua_istable(L, top)); - // 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>::SetLuaxInterfaceTableRef(state, -1); - - lua_settop(L, top); - - // 2) class table + // class table lua_newtable(L); LuaxClass<T>::RegisterLuaxClass(state); LuaxClass<T>::RegisterLuaxFactoryClass(state); @@ -45,14 +31,12 @@ namespace Luax // .Extend() lua_pushvalue(state, -1); // class table - LuaxClass<T>::PushLuaxInterfaceTable(state); // interface table - lua_pushcclosure(state, LuaxClass<T>::l_ExtendFactory, 2); + lua_pushcclosure(state, LuaxClass<T>::l_ExtendFactory, 1); lua_setfield(state, -2, "Extend"); - // .GetInterfaceTable() - LuaxClass<T>::PushLuaxInterfaceTable(state); // interface table - lua_pushcclosure(state, LuaxClass<T>::l_GetInterfaceTable, 1); - lua_setfield(state, -2, "GetInterfaceTable"); + // class["__index"] = class + lua_pushvalue(state, -1); // class table + lua_setfield(state, -2, "__index"); LuaxClass<T>::SetLuaxClassTableRef(state, -1); @@ -67,7 +51,7 @@ namespace Luax } /// - /// Singletonֻһclass tableûinterface table + /// Singleton /// template<typename T> void LuaxState::RegisterSingleton() diff --git a/Source/Samples/LuaxTest/main.cpp b/Source/Samples/LuaxTest/main.cpp index d9bd315..65cdaa1 100644 --- a/Source/Samples/LuaxTest/main.cpp +++ b/Source/Samples/LuaxTest/main.cpp @@ -16,7 +16,9 @@ using namespace Luax; //---------------------------------------------------------------------------------------------------------------- -class School : Singleton<School> +class School + : public Singleton<School> + , public LuaxClass<School> { public: @@ -153,13 +155,16 @@ int Boy::l_Write(lua_State* L) void Boy::RegisterLuaxClass(LuaxState& state) { - state.SetField(-1, "Class", 101); // 101 - state.SetField(-1, "Gender", "Male"); // 101 - luaL_Reg regTable[] = { + // class functions { "New", l_New }, { "GetGender", l_GetGender }, - {NULL, NULL} + // members + { "GetAge", l_GetAge }, + { "GetName", l_GetName }, + { "Write", l_Write }, + { "Speak", l_Speak }, + { 0, 0} }; state.RegisterMethods(regTable); @@ -176,19 +181,6 @@ void Boy::RegisterLuaxClass(LuaxState& state) state.RegisterEnum("EHabits", EHabits); } -void Boy::RegisterLuaxInterface(LuaxState& state) -{ - luaL_Reg regTable[] = { - { "GetAge", l_GetAge }, - { "GetName", l_GetName }, - { "Write", l_Write }, - { "Speak", l_Speak }, - {NULL, NULL} - }; - - state.RegisterMethods(regTable); -} - void Boy::RegisterLuaxPostprocess(LuaxState& state) { // boyİ @@ -231,7 +223,8 @@ int main() state.OpenLibs(); state.PushGlobalNamespace(); state.PushNamespace("Asura"); - + state.RegisterPlainClassRegistry("Class"); // Asura.Class("Foo") + state.RegisterPlainEnumRegistry("Enum"); // Asura.Enum("EFoo", { ... }); //עenum LuaxEnum EGender[] = { { "BOY", 7 }, diff --git a/Source/Samples/LuaxTest/script.lua b/Source/Samples/LuaxTest/script.lua index 32bd8e8..97890c0 100644 --- a/Source/Samples/LuaxTest/script.lua +++ b/Source/Samples/LuaxTest/script.lua @@ -63,7 +63,24 @@ function main() return "kid:Write() 2" end ) print(kid:Speak()) - + +------------------- plain class test + local Foo = Asura.Class("Foo") + Foo.Ctor = function(self, age, name, boy) + self.age = age + self.name = boy:GetName() + self.boy = boy + end + Foo.GetAge = function(self) + return self.age + end + Foo.GetName = function(self) + return self.name + end + local foo = Foo.New(10, "lee", kid) + print(foo:GetName()) + print(Foo.GetClassName()) + print(foo.boy:GetAge()) end function err(msg) print(msg) |