diff options
Diffstat (limited to 'Source')
20 files changed, 590 insertions, 337 deletions
diff --git a/Source/3rdParty/Luax/lua.hpp b/Source/3rdParty/Luax/lua.hpp deleted file mode 100644 index ab14a19..0000000 --- a/Source/3rdParty/Luax/lua.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __LUAX_LUA_HPP__ -#define __LUAX_LUA_HPP__ - -// Include lua first - -extern "C" { -#include "lua51/lua.h" -#include "lua51/lualib.h" -#include "lua51/lauxlib.h" -} -#endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax.h b/Source/3rdParty/Luax/luax.h index f43fb26..58ea2c8 100644 --- a/Source/3rdParty/Luax/luax.h +++ b/Source/3rdParty/Luax/luax.h @@ -1,52 +1,13 @@ #ifndef __LUAX_H__ #define __LUAX_H__ -// moai-coreг - -// include lua.hpp before this #include "luax_state.h" -#include "luax_ref.h" -#include "luax_class.h" -#include "luax_module.h" #include "luax_runtime.h" +#include "luax_namespace.h" +#include "luax_ref.h" #include "luax_reftable.h" +#include "luax_class.hpp" +#include "luax_class.inl" +#include "luax_state.inl" -// luax չluaҪ -// * modules -// * class -// * -/* - -GetClassName -GetClass -New -Extend -GetInterfaceTable - - - -__index -__newIndex -__gc -__mode -__tostring - - - -ݳԱҪʣֵ͵õֵͨʵ֣__indexΪԪ - -ƿռͨʵ - -ͨupvalueʵֵߵķʣҪͨselfͨBaseClass.Extend("SubClass")BaseClassֲͬbase class -BaseClassΪExtendupvaluecfunctionͨlua_upvalueindexupvalue - -֮ͨ.úΪԪԪԸuserdatatableΪԪuserdataԵãʵϲҪ - -interface table -> member table -member table -> ref table -ref table -> userdata ˶ÿ͵úühLuaUserdataΪkey)Ϊdebug - -class table ͵ı壨NewExtendGetClassName - -*/ #endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_class.cpp b/Source/3rdParty/Luax/luax_class.cpp index 52cf6b7..3cb1f8a 100644 --- a/Source/3rdParty/Luax/luax_class.cpp +++ b/Source/3rdParty/Luax/luax_class.cpp @@ -1,72 +1,6 @@ -#include "luax_state.h" -#include "luax_class.h" -#include "luax_runtime.h" + namespace Luax { - LuaxClass::LuaxClass() - : mRC(1) // ʱĬһ - , mSafer(false) - { - } - - LuaxClass::~LuaxClass() - { - assert(mSafer); - } - - void LuaxClass::Retain() - { - ++mRC; - } - - void LuaxClass::Release() - { - if (--mRC <= 0) - { - mSafer = true; // safer - delete this; - } - } - - bool LuaxClass::PushLuaUserdata(LuaxState& state) - { - return true; - } - - void LuaxClass::BindFactoryToLua(LuaxState& state) - { - //assert(!mUserdata); - - // - state.PushPtrUserData(this); - - lua_newtable(state); // ref table - lua_newtable(state); // member table - - } - - void LuaxClass::BindSingletonToLua(LuaxState& state) - { - - } - - //-------------------------------------------------------------------------------------------------------------- - - /// - /// ͷŹʵ - /// - int LuaxClass::l_GC(lua_State* L) - { - LUAX_SETUP(L, "U"); - - return 0; - } - - int LuaxClass::l_ToString(lua_State* L) - { - return 0; - } - }
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_class.h b/Source/3rdParty/Luax/luax_class.hpp index bde9990..6ce8d19 100644 --- a/Source/3rdParty/Luax/luax_class.h +++ b/Source/3rdParty/Luax/luax_class.hpp @@ -3,15 +3,13 @@ #include <vector> -#include "lua.hpp" #include "luax_config.h" -#include "luax_state.h" -#include "luax_runtime.h" +#include "luax_ref.h" namespace Luax { -#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State*) +#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State* L) /// /// RegisterLuaxClass עķͳԱö١ȵclass table @@ -24,8 +22,7 @@ namespace Luax static const char* GetLuaxFactoryName() { return #type; };\ static const char* GetLuaxClassName() { return #type; };\ static bool IsLuaxClassSingleton() { return false; }; - - + /// /// RegisterLuaxClass עķͳԱö١ȵclass table /// LuaxGetSingletonName õ @@ -36,10 +33,10 @@ namespace Luax static const char* GetLuaxClassName() { return #type; }; \ static bool IsLuaxClassSingleton() { return true; }; - /// /// Ҫ¶luaclassҪ̳дࡣͨluaʵҪȷüȷԣڶ߳Ҫȷͷš /// + template<class T> class LuaxClass { public: @@ -53,7 +50,7 @@ namespace Luax virtual ~LuaxClass(); /// - /// userdata pushջûгʼmUserdataʼúԪѳʼõuserdataջ + /// userdata pushջûгʼmUserdataʼúԪѳʼõuserdataջһá /// bool PushLuaUserdata(LuaxState& state); @@ -68,10 +65,17 @@ namespace Luax friend class LuaxState; - template<class T> static void RegisterLuaxClass(LuaxState& state); - template<class T> static void RegisterLuaxFactoryClass(LuaxState& state); - template<class T> static void RegisterLuaxInterface(LuaxState& state); - template<class T> static void RegisterLuaxSingletonClass(LuaxState& state); + 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 PushInterfaceTable(LuaxState& state); + static void PushClassTable(LuaxState& state); + static void PushRefTable(LuaxState& state); /// /// ȡַҪַֻͨڶϴʵõջϺ;̬ıȡַ֤üȷ @@ -80,19 +84,23 @@ namespace Luax void* operator &(); /// - /// stateǹ + /// userdataʵstateǹ /// - void BindFactoryToLua(LuaxState& state); - - /// - /// stateǵ - /// - void BindSingletonToLua(LuaxState& state); + void BindToLua(LuaxState& state); //------------------------------------------------------------------------------------------------------------ - LuaxStrongRef mInterfaceTable; // interface table - LuaxStrongRef mClassTable; // class table + /// + /// LuaxClass<T>͵ʵ + /// + static LuaxStrongRef mInterfaceTable; // interface table + static LuaxStrongRef mClassTable; // class table + static LuaxStrongRef mRefTable; // + + /// + /// ͨuserdataõref table\member table\interface table + /// + LuaxWeakRef mUserdata; /// /// ü̼߳乲 @@ -109,19 +117,21 @@ namespace Luax //------------------------------------------------------------------------------------------------------------ // - template<class T> LUAX_DECL_METHOD(l_GetClassName); - template<class T> LUAX_DECL_METHOD(l_GetClassName2); - - LUAX_DECL_METHOD(l_ToString); // __tostring + LUAX_DECL_METHOD( l_GetClassName ); + LUAX_DECL_METHOD( l_GetInterfaceTable ); + LUAX_DECL_METHOD(l_ToString); //------------------------------------------------------------------------------------------------------------ // - LUAX_DECL_METHOD(l_GC); // __gc + LUAX_DECL_METHOD( l_ExtendFactory ); + LUAX_DECL_METHOD( l_GC ); //------------------------------------------------------------------------------------------------------------ // - + + LUAX_DECL_METHOD( l_ExtendSingleton ); + }; /// @@ -135,11 +145,6 @@ namespace Luax #define LUAX_STATE(L) \ LuaxState& state = LuaxRuntime::Get().GetLuaxState(L) -#define LUAX_RUNTIME() \ - LuaxRuntime& runtime = LuaxRuntime::Get() - -#include "luax_class.inl" - } #endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_class.inl b/Source/3rdParty/Luax/luax_class.inl index fd5c1d9..895152e 100644 --- a/Source/3rdParty/Luax/luax_class.inl +++ b/Source/3rdParty/Luax/luax_class.inl @@ -1,75 +1,249 @@ +namespace Luax +{ -//---------------------------------------------------------------------------------------------------------------- -// ӿ + //---------------------------------------------------------------------------------------------------------------- + // ӿ -/// -/// ԲͬͣͨGetLuaClassName -/// -template<class T> -int LuaxClass::l_GetClassName(lua_State* L) -{ - LUAX_SETUP(L, "*"); + /// + /// ԲͬͣͨGetLuaClassName + /// + template<typename T> + int LuaxClass<T>::l_GetClassName(lua_State* L) + { + LUAX_SETUP(L, "*"); - cc8* type = T::GetLuaxClassName(); - state.Push(type); - return 1; -} + cc8* type = T::GetLuaxClassName(); + state.Push(type); + return 1; + } -//---------------------------------------------------------------------------------------------------------------- + //---------------------------------------------------------------------------------------------------------------- -/// -/// עṤ͵еԱ -/// -template<class T> -void LuaxClass::RegisterLuaxClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - { "GetClassName", l_GetClassName<T> }, - { NULL, NULL } - }; + /// + /// עṤ͵еԱ + /// + template<typename T> + void LuaxClass<T>::RegisterLuaxClass(LuaxState& state) + { + luaL_Reg regTable[] = { + { "GetClassName", l_GetClassName }, + { NULL, NULL } + }; - state.Register(regTable); -} + state.Register(regTable); + } -/// -/// ijԱעclass table -/// -template<class T> -void LuaxClass::RegisterLuaxFactoryClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - { NULL, NULL } - }; + /// + /// ijԱעclass table + /// + template<typename T> + void LuaxClass<T>::RegisterLuaxFactoryClass(LuaxState& state) + { + luaL_Reg regTable[] = { + { "GetInterfaceTable", l_GetInterfaceTable }, + { NULL, NULL } + }; - state.Register(regTable); -} + state.Register(regTable); + } -/// -/// ʵijԱעinterface table -/// -template<class T> -void LuaxClass::RegisterLuaxInterface(LuaxState& state) -{ - luaL_Reg regTable[] = { - { "__gc", l_GC }, - { NULL, NULL } - }; + /// + /// ʵijԱעinterface table + /// + template<typename T> + void LuaxClass<T>::RegisterLuaxInterface(LuaxState& state) + { + luaL_Reg regTable[] = { + { "__gc", l_GC }, + { NULL, NULL } + }; - state.Register(regTable); -} + state.Register(regTable); + } -/// -/// ijԱעclass table -/// -template<class T> -void LuaxClass::RegisterLuaxSingletonClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - {NULL, NULL} - }; + /// + /// ijԱעclass table + /// + template<typename T> + void LuaxClass<T>::RegisterLuaxSingletonClass(LuaxState& state) + { + luaL_Reg regTable[] = { + { NULL, NULL } + }; + + state.Register(regTable); + } + + template<typename T> + void LuaxClass<T>::PushInterfaceTable(LuaxState& state) + { + assert(mInterfaceTable); + + mInterfaceTable.PushRef(state); + } + + template<typename T> + void LuaxClass<T>::PushClassTable(LuaxState& state) + { + assert(mClassTable); + + mClassTable.PushRef(state); + } + + 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) + { + mInterfaceTable.SetRef(state, idx); + } + + template<typename T> + void LuaxClass<T>::SetClassTableRef(LuaxState& state, int idx) + { + mClassTable.SetRef(state, idx); + } + + template<typename T> + LuaxClass<T>::LuaxClass() + : mRC(1) // ʱĬһ + , mSafer(false) + { + } + + template<typename T> + LuaxClass<T>::~LuaxClass() + { + assert(mSafer); + } + + template<typename T> + void LuaxClass<T>::Retain() + { + ++mRC; + } + + template<typename T> + void LuaxClass<T>::Release() + { + if (--mRC <= 0) + { + mSafer = true; // safer + delete this; + } + } + + template<typename T> + bool LuaxClass<T>::PushLuaUserdata(LuaxState& state) + { + if (!mUserdata) + { + BindToLua(state); + return true; + } + return mUserdata.PushRef(state); + } + + template<typename T> + void LuaxClass<T>::BindToLua(LuaxState& state) + { + 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 + } + + // һuserdataãͨnativeָ뷵lua + mUserdata.SetRef(state, -1); + assert(mUserdata); + + if (T::IsLuaxClassSingleton()) + { + + } + } + + //-------------------------------------------------------------------------------------------------------------- + + /// + /// ͷŹʵ + /// + template<typename T> + int LuaxClass<T>::l_GC(lua_State* L) + { + LUAX_SETUP(L, "U"); + + return 0; + } + + template<typename T> + int LuaxClass<T>::l_ToString(lua_State* L) + { + return 0; + } + + template<typename T> + int LuaxClass<T>::l_ExtendFactory(lua_State* L) + { + return 0; + } + + template<typename T> + int LuaxClass<T>::l_GetInterfaceTable(lua_State* L) + { + LUAX_STATE(L); + assert(mInterfaceTable); + mInterfaceTable.PushRef(state); + 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; // - state.Register(regTable); }
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_config.h b/Source/3rdParty/Luax/luax_config.h index ea623a9..3401336 100644 --- a/Source/3rdParty/Luax/luax_config.h +++ b/Source/3rdParty/Luax/luax_config.h @@ -2,7 +2,12 @@ #define __LUAX_TYPE_H__ #include <iostream> -#include "lua.hpp" + +extern "C" { +#include "lua51/lua.h" +#include "lua51/lualib.h" +#include "lua51/lauxlib.h" +} #include <assert.h> diff --git a/Source/3rdParty/Luax/luax_memberref.h b/Source/3rdParty/Luax/luax_memberref.h index 052c53e..baebd06 100644 --- a/Source/3rdParty/Luax/luax_memberref.h +++ b/Source/3rdParty/Luax/luax_memberref.h @@ -5,7 +5,7 @@ namespace Luax { /// - /// LuaxClassijԱã֤ȷͷţǿá + /// LuaxClassijԱãinterface table֤ȷͷţǿá /// class LuaxMemberRef { diff --git a/Source/3rdParty/Luax/luax_module.cpp b/Source/3rdParty/Luax/luax_module.cpp deleted file mode 100644 index 94de1a6..0000000 --- a/Source/3rdParty/Luax/luax_module.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "luax_module.h" - -namespace Luax -{ - - - -}
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_module.h b/Source/3rdParty/Luax/luax_module.h deleted file mode 100644 index 96d954c..0000000 --- a/Source/3rdParty/Luax/luax_module.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef __LUAX_MODULE_H__ -#define __LUAX_MODULE_H__ - -namespace Luax -{ - -#define LUAX_BEGIN_MODULE(MDL) -#define LUAX_END_MODULE(MDL) - -} - -#endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_namespace.cpp b/Source/3rdParty/Luax/luax_namespace.cpp new file mode 100644 index 0000000..a27d44a --- /dev/null +++ b/Source/3rdParty/Luax/luax_namespace.cpp @@ -0,0 +1,8 @@ +#include "luax_namespace.h" + +namespace Luax +{ + + + +}
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_namespace.h b/Source/3rdParty/Luax/luax_namespace.h new file mode 100644 index 0000000..c9b9a23 --- /dev/null +++ b/Source/3rdParty/Luax/luax_namespace.h @@ -0,0 +1,11 @@ +#ifndef __LUAX_NAMESPACE_H__ +#define __LUAX_NAMESPACE_H__ + +namespace Luax +{ + + + +} + +#endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_ref.cpp b/Source/3rdParty/Luax/luax_ref.cpp index e69de29..544861d 100644 --- a/Source/3rdParty/Luax/luax_ref.cpp +++ b/Source/3rdParty/Luax/luax_ref.cpp @@ -0,0 +1,69 @@ +#include "luax_runtime.h" +#include "luax_ref.h" + +namespace Luax +{ + + LuaxRef::LuaxRef(int mode) + : mRefID(LUA_NOREF) + , mMode(mode) + { + } + + LuaxRef::~LuaxRef() + { + } + + LuaxRef::operator bool() + { + return (mRefID != LUA_NOREF); + } + + bool LuaxRef::PushRef(LuaxState& state) + { + assert(mRefID != LUA_NOREF); + + LuaxRuntime& runtime = LuaxRuntime::Get(); + + if (mMode == STRONG_REF) + { + LuaxRefTable& table = runtime[state.GetHandle()].strongRefTable; + table.PushRef(state, mRefID); + } + else if (mMode == WEAK_REF) + { + LuaxRefTable& table = runtime[state.GetHandle()].weakRefTable; + table.PushRef(state, mRefID); + } + else + { + return false; + } + } + + void LuaxRef::SetRef(LuaxState& state, int idx) + { + LuaxRuntime& runtime = LuaxRuntime::Get(); + if (mMode == STRONG_REF) + { + LuaxRefTable& table = runtime[state.GetHandle()].strongRefTable; + mRefID = table.Ref(state, idx); + } + else if (mMode == WEAK_REF) + { + LuaxRefTable& table = runtime[state.GetHandle()].weakRefTable; + mRefID = table.Ref(state, idx); + } + } + + LuaxStrongRef::LuaxStrongRef() + : LuaxRef(STRONG_REF) + { + } + + LuaxWeakRef::LuaxWeakRef() + : LuaxRef(WEAK_REF) + { + } + +} diff --git a/Source/3rdParty/Luax/luax_ref.h b/Source/3rdParty/Luax/luax_ref.h index 759a314..7b484e9 100644 --- a/Source/3rdParty/Luax/luax_ref.h +++ b/Source/3rdParty/Luax/luax_ref.h @@ -1,6 +1,9 @@ #ifndef __LUAX_REF_H__ #define __LUAX_REF_H__ +#include "luax_config.h" +#include "luax_state.h" + namespace Luax { @@ -13,13 +16,24 @@ namespace Luax enum { - STRONG, - WEAK + STRONG_REF, + WEAK_REF }; + + LuaxRef(int mode = STRONG_REF); + virtual ~LuaxRef(); + + operator bool(); + + void SetRef(LuaxState& state, int idx); + bool PushRef(LuaxState& state); + + int GetRefID(); private: - int mRefID; // = luaL_ref + int mRefID; // luaL_ref + int mMode; // strong or weak }; @@ -28,6 +42,8 @@ namespace Luax /// class LuaxStrongRef: public LuaxRef { + public: + LuaxStrongRef(); }; @@ -36,6 +52,8 @@ namespace Luax /// class LuaxWeakRef : public LuaxRef { + public: + LuaxWeakRef(); }; diff --git a/Source/3rdParty/Luax/luax_runtime.h b/Source/3rdParty/Luax/luax_runtime.h index 2414c2e..fe70580 100644 --- a/Source/3rdParty/Luax/luax_runtime.h +++ b/Source/3rdParty/Luax/luax_runtime.h @@ -60,12 +60,15 @@ namespace Luax static LuaxRuntime* mRuntime; /// - /// lua_stateҵcontext + /// lua_State handlecontextӳ /// std::map<lua_State*, Context> mContexts; }; +#define LUAX_RUNTIME() \ + LuaxRuntime& runtime = LuaxRuntime::Get() + } #endif
\ No newline at end of file diff --git a/Source/3rdParty/Luax/luax_state.h b/Source/3rdParty/Luax/luax_state.h index 6a688b6..2b143f8 100644 --- a/Source/3rdParty/Luax/luax_state.h +++ b/Source/3rdParty/Luax/luax_state.h @@ -3,15 +3,13 @@ #include <string> -#include "lua.hpp" -#include "luax_reftable.h" #include "luax_config.h" +#include "luax_reftable.h" namespace Luax { class Context; - class LuaxClass; /// /// lua_StateĴ˱һlua_Stateòݡһʵmetatable£ @@ -109,6 +107,8 @@ namespace Luax void Settop(int idx); + template<typename T> T* GetLuaUserdata(int idx); + //------------------------------------------------------------------------------------------------------------ // õĹregister[LUAX_STRONG_REFTABLE]register[LUAX_WEAK_REFTABLE] @@ -157,8 +157,6 @@ namespace Luax }; -#include "luax_state.inl" - //-------------------------------------------------------------------------------------------------------------- // GetValue()ģػ diff --git a/Source/3rdParty/Luax/luax_state.inl b/Source/3rdParty/Luax/luax_state.inl index 1515123..2c9f7a8 100644 --- a/Source/3rdParty/Luax/luax_state.inl +++ b/Source/3rdParty/Luax/luax_state.inl @@ -1,110 +1,140 @@ +namespace Luax +{ -// ͨ͵вͬע̣ͨLuaxStateRegister_ʵ + // ͨ͵вͬע̣ͨLuaxStateRegister_ʵ -// עṤΪעinterface tableclass tabletype nameΪƿռϡעβԪȵNewõʱŻᡣ + // עṤΪעinterface tableclass tabletype nameΪƿռϡעβԪȵNewõʱŻᡣ -template<typename T> -void LuaxState::RegisterFactory() -{ - lua_State* L = mState; + template<typename T> + void LuaxState::RegisterFactory() + { + lua_State* L = mState; + LuaxState& state = *this; - int top = lua_gettop(L); // namespace table + int top = lua_gettop(L); // namespace table - const char* type = T::GetLuaxFactoryName(); + assert(lua_istable(L, top)); - // interface table. - lua_newtable(L); + const char* type = T::GetLuaxFactoryName(); - int idx = AbsIndex(-1); + // interface table + lua_newtable(L); - LuaxClass::RegisterLuaxInterface<T>(*this); - T::RegisterLuaxInterface(*this); + // interface table[__index] = interface table + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); - // TǷûעķ -#define assertMethods(I, NAME)\ - GetField(I, NAME);\ - assert(IsType(-1, LUA_TFUNCTION));\ - Pop(); + LuaxClass<T>::RegisterLuaxInterface(state); + T::RegisterLuaxInterface(state); - assertMethods(idx, "New"); + LuaxClass<T>::SetInterfaceTableRef(state, -1); -#undef assertMethods + lua_settop(L, top); - lua_settop(L, top); + // class table + lua_newtable(L); - // class table. - lua_newtable(L); + LuaxClass<T>::RegisterLuaxClass(state); + LuaxClass<T>::RegisterLuaxFactoryClass(state); + T::RegisterLuaxClass(state); - assert(lua_istable(L, -1)); + // TǷûעķ +#define _assertmethod(I, NAME) \ + GetField(I, NAME); \ + assert(IsType(-1, LUA_TFUNCTION)); \ + Pop(); - lua_pushvalue(L, -1); + // NewûУûеĻʾһ + //_assertmethod(-1, "New"); - LuaxClass::RegisterLuaxClass<T>(*this); - LuaxClass::RegisterLuaxFactoryClass<T>(*this); - T::RegisterLuaxClass(*this); +#undef _assertmethod - SetField(top, type); + // .Extend() + lua_pushvalue(state, -1); // class table + LuaxClass<T>::PushInterfaceTable(state); // interface table + lua_pushcclosure(state, LuaxClass<T>::l_ExtendFactory, 2); + lua_setfield(state, -2, "Extend"); - // reset top - lua_settop(L, top); + // .GetInterfaceTable() + LuaxClass<T>::PushInterfaceTable(state); // interface table + lua_pushcclosure(state, LuaxClass<T>::l_GetInterfaceTable, 1); + lua_setfield(state, -2, "GetInterfaceTable"); -} + LuaxClass<T>::SetClassTableRef(state, -1); -// עᵥ -template<typename T> -void LuaxState::RegisterSingleton() -{ - lua_State* L = mState; + SetField(top, type); - int top = lua_gettop(L); // namespace table + // reset top + lua_settop(L, top); + } - const char* type = T::GetLuaxSingletonName(); + // עᵥ + template<typename T> + void LuaxState::RegisterSingleton() + { + lua_State* L = mState; + LuaxState& state = *this; - // class table. - lua_newtable(L); + int top = lua_gettop(L); // namespace table + assert(lua_istable(L, top)); - assert(lua_istable(L, -1)); + const char* type = T::GetLuaxSingletonName(); - lua_pushvalue(L, -1); + // class table. + lua_newtable(L); - LuaxClass::RegisterLuaxClass<T>(*this); - LuaxClass::RegisterLuaxFactoryClass<T>(*this); - T::RegisterLuaxClass(*this); + LuaxClass<T>::RegisterLuaxClass(state); + LuaxClass<T>::RegisterLuaxFactoryClass(state); + T::RegisterLuaxClass(state); - SetField(top, type); + SetField(top, type); - // reset top - lua_settop(L, top); + // reset top + lua_settop(L, top); + } -} + template<typename T> + void LuaxState::SetField(int idx, cc8* key, T value) + { + if (IsTableOrUserdata(idx)) + { + idx = AbsIndex(idx); + this->Push(value); + lua_setfield(mState, idx, key); + } + } -template<typename T> -void LuaxState::SetField(int idx, cc8* key, T value) -{ - if (IsTableOrUserdata(idx)) + template<typename T> + T LuaxState::GetField(int idx, cc8* key, T value) { - idx = AbsIndex(idx); - this->Push(value); - lua_setfield(mState, idx, key); + GetField(idx, key); + T result = GetValue < T >(-1, value); + this->Pop(); + + return result; } -} -template<typename T> -T LuaxState::GetField(int idx, cc8* key, T value) -{ - GetField(idx, key); - T result = GetValue < T >(-1, value); - this->Pop(); - - return result; -} - -template<typename T> -T LuaxState::GetField(int idx, int key, T value) -{ - GetField(idx, key); - T result = GetValue < T >(-1, value); - Pop(); + template<typename T> + T LuaxState::GetField(int idx, int key, T value) + { + GetField(idx, key); + T result = GetValue < T >(-1, value); + Pop(); + + return result; + } + + template<typename T> + T* LuaxState::GetLuaUserdata(int idx) + { + void* p = nullptr; + + if (IsType(idx, LUA_TUSERDATA)) + { + p = *(void**)lua_touserdata(mState, idx); + } + + return static_cast<T*>(p); + } - return result; -} +}
\ No newline at end of file diff --git a/Source/Asura.Engine/Graphics/Port/Shader.cpp b/Source/Asura.Engine/Graphics/Port/Shader.cpp index 35fa86d..ba355f5 100644 --- a/Source/Asura.Engine/Graphics/Port/Shader.cpp +++ b/Source/Asura.Engine/Graphics/Port/Shader.cpp @@ -22,7 +22,7 @@ namespace AsuraEngine int Shader::l_Unuse(lua_State* L) { LUAX_STATE(L); - + } /// diff --git a/Source/Asura.Engine/Scripting/Object.h b/Source/Asura.Engine/Scripting/Object.h new file mode 100644 index 0000000..58b7e1a --- /dev/null +++ b/Source/Asura.Engine/Scripting/Object.h @@ -0,0 +1,23 @@ +#ifndef __ASURA_ENGINE_OBJECT_H__ +#define __ASURA_ENGINE_OBJECT_H__ + +#include "Config.h" +#include "Portable.h" + +namespace AsuraEngine +{ + namespace Scripting + { + + /// + /// AsuraEngineűij࣬еĶ + /// + class Object : public Portable<Object> + { + + }; + + } +} + +#endif
\ No newline at end of file diff --git a/Source/Asura.Engine/Scripting/Portable.h b/Source/Asura.Engine/Scripting/Portable.h index c188378..e3c5bef 100644 --- a/Source/Asura.Engine/Scripting/Portable.h +++ b/Source/Asura.Engine/Scripting/Portable.h @@ -10,8 +10,12 @@ namespace AsuraEngine namespace Scripting { - using Portable = Luax::LuaxClass; - + /// + /// Ҫluaļ̳д + /// + template<typename T> + using Portable = Luax::LuaxClass<T>; + } } diff --git a/Source/Samples/LuaxTest/main.cpp b/Source/Samples/LuaxTest/main.cpp index 8dc7281..fdcf29a 100644 --- a/Source/Samples/LuaxTest/main.cpp +++ b/Source/Samples/LuaxTest/main.cpp @@ -59,25 +59,28 @@ void School::RegisterLuaxClass(LuaxState& state) //---------------------------------------------------------------------------------------------------------------- -class Boy : public LuaxClass +class Boy : public LuaxClass<Boy> { public: - Boy(int age) : mAge(age) {} + Boy(int age, const char* name) : mAge(age), mName(name){} -private: + int mAge; + + const char* mName; - int mAge; +private: public: LUAX_DECL_FACTORY(SimBoy); // member methods - LUAX_DECL_METHOD(l_New); LUAX_DECL_METHOD(l_GetAge); + LUAX_DECL_METHOD(l_GetName); // class method + LUAX_DECL_METHOD(l_New); LUAX_DECL_METHOD(l_GetGender); }; @@ -87,8 +90,9 @@ int Boy::l_New(lua_State* L) LUAX_STATE(L); int age = state.GetValue(1, 0); + const char* name = state.GetValue(2, ""); - Boy* boy = new Boy(age); + Boy* boy = new Boy(age, name); boy->PushLuaUserdata(state); return 1; @@ -97,6 +101,19 @@ int Boy::l_New(lua_State* L) int Boy::l_GetAge(lua_State* L) { LUAX_SETUP(L, "U"); + + Boy* self = state.GetLuaUserdata<Boy>(1); + + state.Push(self->mAge); + + return 1; +} + +int Boy::l_GetName(lua_State* L) +{ + LUAX_SETUP(L, "U"); + Boy* self = state.GetLuaUserdata<Boy>(1); + state.Push(self->mName); return 1; } @@ -113,8 +130,9 @@ void Boy::RegisterLuaxClass(LuaxState& state) state.SetField(-1, "Gender", "Male"); // 101 luaL_Reg regTable[] = { - {"GetGender", l_GetGender}, - {NULL, NULL} + { "New", l_New }, + { "GetGender", l_GetGender }, + {NULL, NULL} }; state.Register(regTable); @@ -123,8 +141,9 @@ void Boy::RegisterLuaxClass(LuaxState& state) void Boy::RegisterLuaxInterface(LuaxState& state) { luaL_Reg regTable[] = { - {"New", l_New}, - {NULL, NULL} + { "GetAge", l_GetAge }, + { "GetName", l_GetName }, + {NULL, NULL} }; state.Register(regTable); @@ -132,6 +151,11 @@ void Boy::RegisterLuaxInterface(LuaxState& state) //---------------------------------------------------------------------------------------------------------------- +class Girl : public LuaxClass<Girl> +{ +}; + +//---------------------------------------------------------------------------------------------------------------- string script = R"( function main() local a = 19 @@ -144,7 +168,6 @@ function main() -- print(Asura.SimSchool.GetName()) print(Asura.SimBoy.Class) print(Asura.SimBoy.Gender) - print(Asura.SimBoy.GetGender()) print(Asura.SimBoy.GetClassName()) print(Asura.School.GetName()) print(Asura.School.Region) @@ -160,8 +183,27 @@ function main() end local kid = Kid.New(110, 12) kid:GetHeight() -]] -end + ]] + local kid = Asura.SimBoy.New(23, "Chai") + print(kid:GetAge()) + print(kid:GetName()) + kid.fruit = function() + return "apple" + end + print(kid.fruit()) + print(Asura.SimBoy.GetGender()) + Asura.SimBoy.Havefun = function() + return "Boys have some fun!" + end + print(Asura.SimBoy.Havefun()) + +-- + Asura.SimBoy.Foo = function() + return "SimBoy.Foo" + end + print(Asura.SimBoy.Foo()) + +end function err(msg) print(msg) end @@ -183,7 +225,6 @@ int main() lua_State* L = runtime.Open(); Luax::LuaxState& state = runtime[L].state; - LuaxRefTable& strong = runtime[L].strongRefTable; state.OpenLibs(); state.PushNamespace("Asura"); state.PushNamespace("author"); |