From 8d8c4ff1664625e7428d0d31cd798d9321680cb2 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 14 Mar 2019 09:08:07 +0800 Subject: *luax --- Bin/win64/LuaxTest.exe | Bin 531968 -> 538624 bytes Build/3rdParty/Luax/Luax.vcxproj | 8 +- Build/3rdParty/Luax/Luax.vcxproj.filters | 8 +- Build/Asura.Engine/Asura.Engine.vcxproj | 2 + Build/Asura.Engine/Asura.Engine.vcxproj.filters | 4 + Source/3rdParty/Luax/lua.hpp | 11 - Source/3rdParty/Luax/luax.h | 49 +--- Source/3rdParty/Luax/luax_class.cpp | 68 +----- Source/3rdParty/Luax/luax_class.h | 145 ------------ Source/3rdParty/Luax/luax_class.hpp | 150 ++++++++++++ Source/3rdParty/Luax/luax_class.inl | 292 +++++++++++++++++++----- Source/3rdParty/Luax/luax_config.h | 7 +- Source/3rdParty/Luax/luax_memberref.h | 2 +- Source/3rdParty/Luax/luax_module.cpp | 8 - Source/3rdParty/Luax/luax_module.h | 12 - Source/3rdParty/Luax/luax_namespace.cpp | 8 + Source/3rdParty/Luax/luax_namespace.h | 11 + Source/3rdParty/Luax/luax_ref.cpp | 69 ++++++ Source/3rdParty/Luax/luax_ref.h | 24 +- Source/3rdParty/Luax/luax_runtime.h | 5 +- Source/3rdParty/Luax/luax_state.h | 8 +- Source/3rdParty/Luax/luax_state.inl | 182 +++++++++------ Source/Asura.Engine/Graphics/Port/Shader.cpp | 2 +- Source/Asura.Engine/Scripting/Object.h | 23 ++ Source/Asura.Engine/Scripting/Portable.h | 8 +- Source/Samples/LuaxTest/main.cpp | 69 ++++-- git.exe.stackdump | 19 -- 27 files changed, 715 insertions(+), 479 deletions(-) delete mode 100644 Source/3rdParty/Luax/lua.hpp delete mode 100644 Source/3rdParty/Luax/luax_class.h create mode 100644 Source/3rdParty/Luax/luax_class.hpp delete mode 100644 Source/3rdParty/Luax/luax_module.cpp delete mode 100644 Source/3rdParty/Luax/luax_module.h create mode 100644 Source/3rdParty/Luax/luax_namespace.cpp create mode 100644 Source/3rdParty/Luax/luax_namespace.h create mode 100644 Source/Asura.Engine/Scripting/Object.h delete mode 100644 git.exe.stackdump diff --git a/Bin/win64/LuaxTest.exe b/Bin/win64/LuaxTest.exe index 3ae17e5..b1f896b 100644 Binary files a/Bin/win64/LuaxTest.exe and b/Bin/win64/LuaxTest.exe differ diff --git a/Build/3rdParty/Luax/Luax.vcxproj b/Build/3rdParty/Luax/Luax.vcxproj index 8761b03..3373149 100644 --- a/Build/3rdParty/Luax/Luax.vcxproj +++ b/Build/3rdParty/Luax/Luax.vcxproj @@ -122,13 +122,12 @@ - - + - + @@ -137,10 +136,9 @@ - - + diff --git a/Build/3rdParty/Luax/Luax.vcxproj.filters b/Build/3rdParty/Luax/Luax.vcxproj.filters index 758a745..6476684 100644 --- a/Build/3rdParty/Luax/Luax.vcxproj.filters +++ b/Build/3rdParty/Luax/Luax.vcxproj.filters @@ -1,11 +1,8 @@  - - - @@ -13,18 +10,19 @@ + + - - + diff --git a/Build/Asura.Engine/Asura.Engine.vcxproj b/Build/Asura.Engine/Asura.Engine.vcxproj index bb736dc..8ca3444 100644 --- a/Build/Asura.Engine/Asura.Engine.vcxproj +++ b/Build/Asura.Engine/Asura.Engine.vcxproj @@ -169,6 +169,7 @@ + @@ -188,6 +189,7 @@ + diff --git a/Build/Asura.Engine/Asura.Engine.vcxproj.filters b/Build/Asura.Engine/Asura.Engine.vcxproj.filters index f1c72a7..a1c6ac8 100644 --- a/Build/Asura.Engine/Asura.Engine.vcxproj.filters +++ b/Build/Asura.Engine/Asura.Engine.vcxproj.filters @@ -195,6 +195,10 @@ Scripting + + + Scripting + 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ÉèΪExtendµÄupvalue£¬ÔÚcfunctionͨ¹ýlua_upvalueindexºêÀï·ÃÎÊupvalue¡£ - -Ö®ËùÒÔÄÜͨ¹ý.µ÷Óú¯ÊýÊÇÒòΪԪ±í£¬Ôª±í¿ÉÒÔ¸øuserdataºÍtable×÷ΪԪ±í£¬ËùÒÔuserdata¿ÉÒÔµ÷Ó㬵«Êµ¼ÊÉϲ»ÐèÒªÕâÑù¡£ - -interface table -> member table -member table -> ref table -ref table -> userdata ±£´æÁ˶Ôÿ¸öÆäËûÀàÐ͵ÄÒýÓúÍÒýÓüÆÊý£¨ÓÃhLuaUserdata×÷Ϊkey)£¬¼ÆÊýÊÇΪÁËdebug - -class table ÊÇÀàÐ͵ı¾Ì壨¹¤³§£©£¬ÓÐNew¡¢Extend¡¢GetClassName·½·¨ - -*/ #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.h deleted file mode 100644 index bde9990..0000000 --- a/Source/3rdParty/Luax/luax_class.h +++ /dev/null @@ -1,145 +0,0 @@ -#ifndef __LUAX_CLASS_H__ -#define __LUAX_CLASS_H__ - -#include - -#include "lua.hpp" -#include "luax_config.h" -#include "luax_state.h" -#include "luax_runtime.h" - -namespace Luax -{ - -#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State*) - - /// - /// RegisterLuaxClass ×¢²áÀàµÄ·½·¨ºÍ³ÉÔ±£¬±ÈÈçö¾Ù¡¢³£Á¿µÈµ½class table - /// LuaxRegisterInterface ×¢²á½Ó¿Ú·½·¨µ½interface table - /// LuaxGetFactoryName »ñµÃ¹¤³§µÄÀàÃû£¬Í¬Ê±ÓÃÀ´±ÜÃâ×¢²áʱ´íÎó×¢²áΪÁËsingleton£¬Í¨¹ý±àÒëʱ±¨´í±ÜÃâ - /// -#define LUAX_DECL_FACTORY(type) \ - static void RegisterLuaxClass(LuaxState&);\ - static void RegisterLuaxInterface(LuaxState&);\ - static const char* GetLuaxFactoryName() { return #type; };\ - static const char* GetLuaxClassName() { return #type; };\ - static bool IsLuaxClassSingleton() { return false; }; - - - /// - /// RegisterLuaxClass ×¢²áÀàµÄ·½·¨ºÍ³ÉÔ±£¬±ÈÈçö¾Ù¡¢³£Á¿µÈµ½class table - /// LuaxGetSingletonName »ñµÃµ¥ÀýµÄÀàÃû - /// -#define LUAX_DECL_SINGLETON(type) \ - static void RegisterLuaxClass(LuaxState&); \ - static const char* GetLuaxSingletonName() { return #type; }; \ - static const char* GetLuaxClassName() { return #type; }; \ - static bool IsLuaxClassSingleton() { return true; }; - - - /// - /// ÐèÒª±©Â¶¸øluaµÄclassÐèÒª¼Ì³Ð´ËÀࡣͨ¹ýlua¹ÜÀíµÄʵÀýҪȷ±£ÒýÓüÆÊýµÄÕýÈ·ÐÔ£¬ÔÚ¶à¸öÏß³ÌÖÐÐèҪȷ¶¨²»»áÎóÊÍ·Å¡£ - /// - class LuaxClass - { - public: - - void Retain(); - void Release(); - - protected: - - LuaxClass(); - virtual ~LuaxClass(); - - /// - /// ½«userdata pushµ½Õ»¶¥£¬Èç¹ûûÓгõʼ»¯mUserdata£¬³õʼ»¯ÉèÖúÃÔª±í²¢°Ñ³õʼ»¯ºÃµÄuserdataÁôÔÚÕ»¶¥¡£ - /// - bool PushLuaUserdata(LuaxState& state); - - //------------------------------------------------------------------------------------------------------------ - - /// - /// ¶ÔÀàµÄreftableµÄ¹ÜÀí - /// - void Ref(); - - private: - - friend class LuaxState; - - template static void RegisterLuaxClass(LuaxState& state); - template static void RegisterLuaxFactoryClass(LuaxState& state); - template static void RegisterLuaxInterface(LuaxState& state); - template static void RegisterLuaxSingletonClass(LuaxState& state); - - /// - /// ÆÁ±ÎÈ¡µØÖ·ÔËËã·û£¬Èç¹ûÐèÒªµØÖ·£¬Ö»ÄÜͨ¹ýÔÚ¶ÑÉÏ´´½¨ÊµÀýµÃµ½¡£ÔÚÕ»ÉϺ;²Ì¬ÇøµÄ±äÁ¿²»ÄÜÈ¡µØÖ·¡£±£Ö¤ÒýÓüÆÊýµÄ׼ȷ¡£Èç - /// ¹ûÐèÒª´©ÒýÓã¬Ê¹ÓÃÒýÓô«µÝ¶ø²»ÊÇ´«µÝµØÖ·¡£ - /// - void* operator &(); - - /// - /// °ó¶¨µ½state£¬Èç¹û±¾ÀàÊǹ¤³§ - /// - void BindFactoryToLua(LuaxState& state); - - /// - /// °ó¶¨µ½state£¬Èç¹û±¾ÀàÊǵ¥Àý - /// - void BindSingletonToLua(LuaxState& state); - - //------------------------------------------------------------------------------------------------------------ - - LuaxStrongRef mInterfaceTable; // interface table - LuaxStrongRef mClassTable; // class table - - /// - /// ÒýÓüÆÊý£¬ÓÃÀ´´¦ÀíÏ̼߳乲Ïí - /// - int mRC; - - /// - /// È·±£Îö¹¹º¯ÊýÖ»ÄÜͨ¹ýReleaseµ÷ÓõÄsafer£¬ÕâÑùÖ»Òª¼Ì³ÐÁËLuaxClassµÄÀ࣬Èç¹ûʹÓÃdeleteÖ±½ÓÎö¹¹£¬¾Í»á±¨´í - /// - bool mSafer; - - public: - - //------------------------------------------------------------------------------------------------------------ - // ¹«¹²ÄÚÈÝ - - template LUAX_DECL_METHOD(l_GetClassName); - template LUAX_DECL_METHOD(l_GetClassName2); - - LUAX_DECL_METHOD(l_ToString); // __tostring - - //------------------------------------------------------------------------------------------------------------ - // ¹¤³§ÀàÏà¹Ø - - LUAX_DECL_METHOD(l_GC); // __gc - - //------------------------------------------------------------------------------------------------------------ - // µ¥ÀýÀàÏà¹Ø - - }; - - /// - /// ÔÚ³ÉÔ±·½·¨Àï´´½¨LuaxState²¢¶Ô²ÎÊý½øÐмì²é¡£ - /// -#define LUAX_SETUP(L, params) \ - LuaxRuntime& runtime = LuaxRuntime::Get(); \ - LuaxState& state = runtime[L].state; \ - if(!state.CheckParams(1, params)) return 0 - -#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.hpp b/Source/3rdParty/Luax/luax_class.hpp new file mode 100644 index 0000000..6ce8d19 --- /dev/null +++ b/Source/3rdParty/Luax/luax_class.hpp @@ -0,0 +1,150 @@ +#ifndef __LUAX_CLASS_H__ +#define __LUAX_CLASS_H__ + +#include + +#include "luax_config.h" +#include "luax_ref.h" + +namespace Luax +{ + +#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State* L) + + /// + /// RegisterLuaxClass ×¢²áÀàµÄ·½·¨ºÍ³ÉÔ±£¬±ÈÈçö¾Ù¡¢³£Á¿µÈµ½class table + /// LuaxRegisterInterface ×¢²á½Ó¿Ú·½·¨µ½interface table + /// LuaxGetFactoryName »ñµÃ¹¤³§µÄÀàÃû£¬Í¬Ê±ÓÃÀ´±ÜÃâ×¢²áʱ´íÎó×¢²áΪÁËsingleton£¬Í¨¹ý±àÒëʱ±¨´í±ÜÃâ + /// +#define LUAX_DECL_FACTORY(type) \ + static void RegisterLuaxClass(LuaxState&);\ + static void RegisterLuaxInterface(LuaxState&);\ + static const char* GetLuaxFactoryName() { return #type; };\ + static const char* GetLuaxClassName() { return #type; };\ + static bool IsLuaxClassSingleton() { return false; }; + + /// + /// RegisterLuaxClass ×¢²áÀàµÄ·½·¨ºÍ³ÉÔ±£¬±ÈÈçö¾Ù¡¢³£Á¿µÈµ½class table + /// LuaxGetSingletonName »ñµÃµ¥ÀýµÄÀàÃû + /// +#define LUAX_DECL_SINGLETON(type) \ + static void RegisterLuaxClass(LuaxState&); \ + static const char* GetLuaxSingletonName() { return #type; }; \ + static const char* GetLuaxClassName() { return #type; }; \ + static bool IsLuaxClassSingleton() { return true; }; + + /// + /// ÐèÒª±©Â¶¸øluaµÄclassÐèÒª¼Ì³Ð´ËÀࡣͨ¹ýlua¹ÜÀíµÄʵÀýҪȷ±£ÒýÓüÆÊýµÄÕýÈ·ÐÔ£¬ÔÚ¶à¸öÏß³ÌÖÐÐèҪȷ¶¨²»»áÎóÊÍ·Å¡£ + /// + template + class LuaxClass + { + public: + + void Retain(); + void Release(); + + protected: + + LuaxClass(); + virtual ~LuaxClass(); + + /// + /// ½«userdata pushµ½Õ»¶¥£¬Èç¹ûûÓгõʼ»¯mUserdata£¬³õʼ»¯ÉèÖúÃÔª±í²¢°Ñ³õʼ»¯ºÃµÄuserdataÁôÔÚÕ»¶¥¡£²¢Ìí¼ÓÒ»¸öÒýÓᣠ+ /// + bool PushLuaUserdata(LuaxState& state); + + //------------------------------------------------------------------------------------------------------------ + + /// + /// ¶ÔÀàµÄreftableµÄ¹ÜÀí + /// + void Ref(); + + private: + + friend class LuaxState; + + 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); + + /// + /// ÆÁ±ÎÈ¡µØÖ·ÔËËã·û£¬Èç¹ûÐèÒªµØÖ·£¬Ö»ÄÜͨ¹ýÔÚ¶ÑÉÏ´´½¨ÊµÀýµÃµ½¡£ÔÚÕ»ÉϺ;²Ì¬ÇøµÄ±äÁ¿²»ÄÜÈ¡µØÖ·¡£±£Ö¤ÒýÓüÆÊýµÄ׼ȷ¡£Èç + /// ¹ûÐèÒª´©ÒýÓã¬Ê¹ÓÃÒýÓô«µÝ¶ø²»ÊÇ´«µÝµØÖ·¡£ + /// + void* operator &(); + + /// + /// ´´½¨userdata£¬°ó¶¨ÊµÀýµ½state£¬Èç¹û±¾ÀàÊǹ¤³§ + /// + void BindToLua(LuaxState& state); + + //------------------------------------------------------------------------------------------------------------ + + /// + /// ËùÓÐLuaxClassÀàÐ͵ÄʵÀý¹²ÏíµÄÄÚÈÝ + /// + static LuaxStrongRef mInterfaceTable; // interface table + static LuaxStrongRef mClassTable; // class table + static LuaxStrongRef mRefTable; // µ¥Àý + + /// + /// ͨ¹ýuserdata¿ÉÒÔÄõ½ref table\member table\interface table + /// + LuaxWeakRef mUserdata; + + /// + /// ÒýÓüÆÊý£¬ÓÃÀ´´¦ÀíÏ̼߳乲Ïí + /// + int mRC; + + /// + /// È·±£Îö¹¹º¯ÊýÖ»ÄÜͨ¹ýReleaseµ÷ÓõÄsafer£¬ÕâÑùÖ»Òª¼Ì³ÐÁËLuaxClassµÄÀ࣬Èç¹ûʹÓÃdeleteÖ±½ÓÎö¹¹£¬¾Í»á±¨´í + /// + bool mSafer; + + public: + + //------------------------------------------------------------------------------------------------------------ + // ¹«¹²ÄÚÈÝ + + LUAX_DECL_METHOD( l_GetClassName ); + LUAX_DECL_METHOD( l_GetInterfaceTable ); + LUAX_DECL_METHOD(l_ToString); + + //------------------------------------------------------------------------------------------------------------ + // ¹¤³§ÀàÏà¹Ø + + LUAX_DECL_METHOD( l_ExtendFactory ); + LUAX_DECL_METHOD( l_GC ); + + //------------------------------------------------------------------------------------------------------------ + // µ¥ÀýÀàÏà¹Ø + + LUAX_DECL_METHOD( l_ExtendSingleton ); + + }; + + /// + /// ÔÚ³ÉÔ±·½·¨Àï´´½¨LuaxState²¢¶Ô²ÎÊý½øÐмì²é¡£ + /// +#define LUAX_SETUP(L, params) \ + LuaxRuntime& runtime = LuaxRuntime::Get(); \ + LuaxState& state = runtime[L].state; \ + if(!state.CheckParams(1, params)) return 0 + +#define LUAX_STATE(L) \ + LuaxState& state = LuaxRuntime::Get().GetLuaxState(L) + +} + +#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 -int LuaxClass::l_GetClassName(lua_State* L) -{ - LUAX_SETUP(L, "*"); + /// + /// ¶Ô²»Í¬ÀàÐÍ£¬Í¨¹ýµ÷ÓÃGetLuaClassName»ñµÃÀàÐÍÃû + /// + template + int LuaxClass::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 -void LuaxClass::RegisterLuaxClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - { "GetClassName", l_GetClassName }, - { NULL, NULL } - }; + /// + /// ×¢²á¹¤³§ºÍµ¥Àý¹²ÓеÄÀà³ÉÔ± + /// + template + void LuaxClass::RegisterLuaxClass(LuaxState& state) + { + luaL_Reg regTable[] = { + { "GetClassName", l_GetClassName }, + { NULL, NULL } + }; - state.Register(regTable); -} + state.Register(regTable); + } -/// -/// ¹¤³§ÀàµÄ³ÉÔ±£¬×¢²áÔÚclass table -/// -template -void LuaxClass::RegisterLuaxFactoryClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - { NULL, NULL } - }; + /// + /// ¹¤³§ÀàµÄ³ÉÔ±£¬×¢²áÔÚclass table + /// + template + void LuaxClass::RegisterLuaxFactoryClass(LuaxState& state) + { + luaL_Reg regTable[] = { + { "GetInterfaceTable", l_GetInterfaceTable }, + { NULL, NULL } + }; - state.Register(regTable); -} + state.Register(regTable); + } -/// -/// ¹¤³§ÊµÀýµÄ³ÉÔ±£¬×¢²áÔÚinterface table -/// -template -void LuaxClass::RegisterLuaxInterface(LuaxState& state) -{ - luaL_Reg regTable[] = { - { "__gc", l_GC }, - { NULL, NULL } - }; + /// + /// ¹¤³§ÊµÀýµÄ³ÉÔ±£¬×¢²áÔÚinterface table + /// + template + void LuaxClass::RegisterLuaxInterface(LuaxState& state) + { + luaL_Reg regTable[] = { + { "__gc", l_GC }, + { NULL, NULL } + }; - state.Register(regTable); -} + state.Register(regTable); + } -/// -/// µ¥ÀýÀàµÄ³ÉÔ±£¬×¢²áÔÚclass table -/// -template -void LuaxClass::RegisterLuaxSingletonClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - {NULL, NULL} - }; + /// + /// µ¥ÀýÀàµÄ³ÉÔ±£¬×¢²áÔÚclass table + /// + template + void LuaxClass::RegisterLuaxSingletonClass(LuaxState& state) + { + luaL_Reg regTable[] = { + { NULL, NULL } + }; + + state.Register(regTable); + } + + template + void LuaxClass::PushInterfaceTable(LuaxState& state) + { + assert(mInterfaceTable); + + mInterfaceTable.PushRef(state); + } + + template + void LuaxClass::PushClassTable(LuaxState& state) + { + assert(mClassTable); + + mClassTable.PushRef(state); + } + + template + void LuaxClass::PushRefTable(LuaxState& state) + { + assert(mRefTable); + + mRefTable.Push(state); + } + + template + void LuaxClass::SetInterfaceTableRef(LuaxState& state, int idx) + { + mInterfaceTable.SetRef(state, idx); + } + + template + void LuaxClass::SetClassTableRef(LuaxState& state, int idx) + { + mClassTable.SetRef(state, idx); + } + + template + LuaxClass::LuaxClass() + : mRC(1) // ¹¹ÔìʱĬÈÏÓÐÒ»¸öÒýÓà + , mSafer(false) + { + } + + template + LuaxClass::~LuaxClass() + { + assert(mSafer); + } + + template + void LuaxClass::Retain() + { + ++mRC; + } + + template + void LuaxClass::Release() + { + if (--mRC <= 0) + { + mSafer = true; // ´ò¿ªÎö¹¹safer + delete this; + } + } + + template + bool LuaxClass::PushLuaUserdata(LuaxState& state) + { + if (!mUserdata) + { + BindToLua(state); + return true; + } + return mUserdata.PushRef(state); + } + + template + void LuaxClass::BindToLua(LuaxState& state) + { + assert(!mUserdata); + + // ´´½¨userdata²¢ÁôÔÚÕ»¶¥ + state.PushPtrUserData(this); + + // ¹¤³§ + if (!T::IsLuaxClassSingleton()) + { + lua_newtable(state); // ref table + lua_newtable(state); // member table + LuaxClass::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::l_GC); + lua_setfield(state, refTable, "__gc"); + + lua_pushcfunction(state, LuaxClass::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 + int LuaxClass::l_GC(lua_State* L) + { + LUAX_SETUP(L, "U"); + + return 0; + } + + template + int LuaxClass::l_ToString(lua_State* L) + { + return 0; + } + + template + int LuaxClass::l_ExtendFactory(lua_State* L) + { + return 0; + } + + template + int LuaxClass::l_GetInterfaceTable(lua_State* L) + { + LUAX_STATE(L); + assert(mInterfaceTable); + mInterfaceTable.PushRef(state); + return 0; + } + + template LuaxStrongRef LuaxClass::mInterfaceTable; // interface table + template LuaxStrongRef LuaxClass::mClassTable; // class table + template LuaxStrongRef LuaxClass::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 -#include "lua.hpp" + +extern "C" { +#include "lua51/lua.h" +#include "lua51/lualib.h" +#include "lua51/lauxlib.h" +} #include 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 { /// - /// LuaxClassµÄ³ÉÔ±ÒýÓ㬱£Ö¤ÕýÈ·ÊÍ·Å£¬ÊÇÇ¿ÒýÓᣠ+ /// LuaxClassµÄ³ÉÔ±ÒýÓ㬴æÔÚ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 handleµ½contextµÄÓ³Éä /// std::map 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 -#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 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 +{ -// Õë¶ÔÆÕͨÀàºÍµ¥ÀýÀàÓв»Í¬µÄ×¢²á¹ý³Ì£¬Í¨¹ýLuaxStateµÄÁ½¸ö¹¤³§·½·¨Register_ʵÏÖ + // Õë¶ÔÆÕͨÀàºÍµ¥ÀýÀàÓв»Í¬µÄ×¢²á¹ý³Ì£¬Í¨¹ýLuaxStateµÄÁ½¸ö¹¤³§·½·¨Register_ʵÏÖ -// ×¢²á¹¤³§£¬·ÖΪע²áinterface tableºÍclass table£¬ÒÔtype nameΪ¼üÉèÖÃÔÚÃû³Æ¿Õ¼äÉÏ¡£ÔÚ×¢²á½×¶Î²»»áÉèÖÃÔª±í£¬µÈµ½New·½·¨µ÷ÓõÄʱºò²Å»á¡£ + // ×¢²á¹¤³§£¬·ÖΪע²áinterface tableºÍclass table£¬ÒÔtype nameΪ¼üÉèÖÃÔÚÃû³Æ¿Õ¼äÉÏ¡£ÔÚ×¢²á½×¶Î²»»áÉèÖÃÔª±í£¬µÈµ½New·½·¨µ÷ÓõÄʱºò²Å»á¡£ -template -void LuaxState::RegisterFactory() -{ - lua_State* L = mState; + template + 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(*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::RegisterLuaxInterface(state); + T::RegisterLuaxInterface(state); - assertMethods(idx, "New"); + LuaxClass::SetInterfaceTableRef(state, -1); -#undef assertMethods + lua_settop(L, top); - lua_settop(L, top); + // class table + lua_newtable(L); - // class table. - lua_newtable(L); + LuaxClass::RegisterLuaxClass(state); + LuaxClass::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(*this); - LuaxClass::RegisterLuaxFactoryClass(*this); - T::RegisterLuaxClass(*this); +#undef _assertmethod - SetField(top, type); + // .Extend() + lua_pushvalue(state, -1); // class table + LuaxClass::PushInterfaceTable(state); // interface table + lua_pushcclosure(state, LuaxClass::l_ExtendFactory, 2); + lua_setfield(state, -2, "Extend"); - // reset top - lua_settop(L, top); + // .GetInterfaceTable() + LuaxClass::PushInterfaceTable(state); // interface table + lua_pushcclosure(state, LuaxClass::l_GetInterfaceTable, 1); + lua_setfield(state, -2, "GetInterfaceTable"); -} + LuaxClass::SetClassTableRef(state, -1); -// ×¢²áµ¥Àý -template -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 + 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(*this); - LuaxClass::RegisterLuaxFactoryClass(*this); - T::RegisterLuaxClass(*this); + LuaxClass::RegisterLuaxClass(state); + LuaxClass::RegisterLuaxFactoryClass(state); + T::RegisterLuaxClass(state); - SetField(top, type); + SetField(top, type); - // reset top - lua_settop(L, top); + // reset top + lua_settop(L, top); + } -} + template + void LuaxState::SetField(int idx, cc8* key, T value) + { + if (IsTableOrUserdata(idx)) + { + idx = AbsIndex(idx); + this->Push(value); + lua_setfield(mState, idx, key); + } + } -template -void LuaxState::SetField(int idx, cc8* key, T value) -{ - if (IsTableOrUserdata(idx)) + template + 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 -T LuaxState::GetField(int idx, cc8* key, T value) -{ - GetField(idx, key); - T result = GetValue < T >(-1, value); - this->Pop(); - - return result; -} - -template -T LuaxState::GetField(int idx, int key, T value) -{ - GetField(idx, key); - T result = GetValue < T >(-1, value); - Pop(); + template + T LuaxState::GetField(int idx, int key, T value) + { + GetField(idx, key); + T result = GetValue < T >(-1, value); + Pop(); + + return result; + } + + template + T* LuaxState::GetLuaUserdata(int idx) + { + void* p = nullptr; + + if (IsType(idx, LUA_TUSERDATA)) + { + p = *(void**)lua_touserdata(mState, idx); + } + + return static_cast(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½Å±¾µÄ³éÏó»ùÀ࣬ËùÓеĶ«Î÷¶¼´ÓÕâÀïÅÉÉú + /// + class Object : public Portable + { + + }; + + } +} + +#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 + using Portable = Luax::LuaxClass; + } } 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 { 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(1); + + state.Push(self->mAge); + + return 1; +} + +int Boy::l_GetName(lua_State* L) +{ + LUAX_SETUP(L, "U"); + Boy* self = state.GetLuaUserdata(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 +{ +}; + +//---------------------------------------------------------------------------------------------------------------- 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"); diff --git a/git.exe.stackdump b/git.exe.stackdump deleted file mode 100644 index 3628384..0000000 --- a/git.exe.stackdump +++ /dev/null @@ -1,19 +0,0 @@ -Stack trace: -Frame Function Args -000FFFFB4C0 0018005FDA9 (00000000000, 00600076310, 0007E13C269, 000FFFFDE50) -00000000002 00180061C1A (000FFFFB7A0, 00000000000, 0000000035C, 00000000000) -000FFFFB7A0 0018012860B (0000000000B, 00000000000, 0000000000B, 00000000000) -000FFFFBA80 00180124BEE (000FFFFB9F0, 00100000000, 00000000000, 7FFF00000003) -00600075510 001801250E4 (0018019A537, 0018032C518, 000FFFFBAA0, 00000000006) -00600075510 00180125329 (00000000018, 00000001028, 000FFFFBBA0, 0010058939C) -00600075510 0018012560F (001801613CE, 00600075510, 00100608ED5, 000000001F4) -00600075510 00100589517 (0018012078B, 000FFFFBCE8, 000FFFFBD90, 000FFFFBD90) -00600075510 00100589858 (00600086998, 00000000002, 001006090B7, 00180242080) -00600075510 0010054A27C (00100605FF0, 006000745A0, 001800BF9A2, 00000000000) -001005EEE8A 001004B9394 (000FFFFC028, 00000000006, 00600000000, 00000000001) -00000000001 00100577E8E (000000000A8, 000000000A8, 00000000000, 00600073C48) -00000000100 00100579E12 (00000000000, 001005D9959, 000FFFFCC6A, 001005D9959) -001005B2D60 0010045EC6E (00000000000, 00000000000, 000FFFFC2B0, 001005DA0A0) -001005B2D60 00100460036 (001800BF8FE, 000FFFFCA50, 00000000004, 00100648160) -000FFFFCC38 00100401265 (00180124994, 00000000000, 001006481B8, 006000725E0) -End of stack trace (more stack frames may be present) -- cgit v1.1-26-g67d0