summaryrefslogtreecommitdiff
path: root/source/3rd-party
diff options
context:
space:
mode:
Diffstat (limited to 'source/3rd-party')
-rw-r--r--source/3rd-party/Luax/luax_class.cpp24
-rw-r--r--source/3rd-party/Luax/luax_class.hpp168
-rw-r--r--source/3rd-party/Luax/luax_class.inl196
-rw-r--r--source/3rd-party/Luax/luax_config.h6
-rw-r--r--source/3rd-party/Luax/luax_dog.cpp0
-rw-r--r--source/3rd-party/Luax/luax_dog.h33
-rw-r--r--source/3rd-party/Luax/luax_enum.cpp6
-rw-r--r--source/3rd-party/Luax/luax_enum.h4
-rw-r--r--source/3rd-party/Luax/luax_state.cpp4
-rw-r--r--source/3rd-party/Luax/luax_state.h4
-rw-r--r--source/3rd-party/Luax/luax_state.inl71
11 files changed, 320 insertions, 196 deletions
diff --git a/source/3rd-party/Luax/luax_class.cpp b/source/3rd-party/Luax/luax_class.cpp
index bb1609d..762f0dc 100644
--- a/source/3rd-party/Luax/luax_class.cpp
+++ b/source/3rd-party/Luax/luax_class.cpp
@@ -1,6 +1,6 @@
#include "luax_class.hpp"
-#include "luax_vm.h"
#include "luax_cfunctions.h"
+#include "luax_vm.h"
namespace Luax
{
@@ -29,30 +29,30 @@ namespace Luax
lua_setfield(L, -2, "GetClass");
// TypeOf()
- lua_pushcfunction(L, l_TypeOf);
+ lua_pushcfunction(L, _TypeOf);
lua_setfield(L, -2, "TypeOf");
// New()
lua_pushvalue(L, -1); // class table
- lua_pushcclosure(L, l_New, 1);
+ lua_pushcclosure(L, _New, 1);
lua_setfield(L, -2, "New");
// Extend()
lua_pushvalue(L, -1); // class table
- lua_pushcclosure(L, l_Extend, 1);
+ lua_pushcclosure(L, _Extend, 1);
lua_setfield(L, -2, "Extend");
lua_pushvalue(L, -1); // class table
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
- lua_pushcclosure(L, _Tostring, 1);
+ lua_pushcclosure(L, __tostring, 1);
lua_setfield(L, -2, "__tostring");
return 1;
}
- int LuaxPlainClass::_Tostring(lua_State* L)
+ int LuaxPlainClass::__tostring(lua_State* L)
{
// upvalues:
// 1: class name
@@ -75,7 +75,7 @@ namespace Luax
///
/// NewnԻȡ__init__initʼʵ
///
- int LuaxPlainClass::l_New(lua_State* L)
+ int LuaxPlainClass::_New(lua_State* L)
{
LUAX_STATE(L);
@@ -140,7 +140,7 @@ namespace Luax
return 1;
}
- int LuaxPlainClass::l_Extend(lua_State* L)
+ int LuaxPlainClass::_Extend(lua_State* L)
{
LUAX_STATE(L);
@@ -168,12 +168,12 @@ namespace Luax
// New()
lua_pushvalue(L, -1); // class table
- lua_pushcclosure(L, l_New, 1);
+ lua_pushcclosure(L, _New, 1);
lua_setfield(L, -2, "New");
// Extend()
lua_pushvalue(L, -1); // class table
- lua_pushcclosure(L, l_Extend, 1);
+ lua_pushcclosure(L, _Extend, 1);
lua_setfield(L, -2, "Extend");
// .__base
@@ -184,7 +184,7 @@ namespace Luax
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
- lua_pushcclosure(L, _Tostring, 1);
+ lua_pushcclosure(L, __tostring, 1);
lua_setfield(L, -2, "__tostring");
// classmetatableΪbaseClass
@@ -194,7 +194,7 @@ namespace Luax
return 1;
}
- int LuaxPlainClass::l_TypeOf(lua_State* L)
+ int LuaxPlainClass::_TypeOf(lua_State* L)
{
// params:
// 1: lua instance
diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp
index ba8d16a..c41adbd 100644
--- a/source/3rd-party/Luax/luax_class.hpp
+++ b/source/3rd-party/Luax/luax_class.hpp
@@ -1,27 +1,36 @@
#ifndef __LUAX_CLASS_H__
#define __LUAX_CLASS_H__
+#include "luax_config.h"
+
+#if LUAX_PROFILER
+#include <unordered_set>
+#endif
+
#include <vector>
-#include "luax_config.h"
#include "luax_ref.h"
#include "luax_memberref.h"
#include "luax_cfunctions.h"
+#include "luax_dog.h"
namespace Luax
{
+ class LuaxVM;
+
///
/// RegisterLuaxClass עķͳԱö١ȵclass table
/// LuaxGetFactoryName ùͬʱעʱעΪsingletonͨ
/// ʱ
///
#define LUAX_DECL_FACTORY(type) \
- static void RegisterLuaxClass(Luax::LuaxState&);\
- static void RegisterLuaxPostprocess(Luax::LuaxState&); \
+ friend class Luax::LuaxState; \
+ static void RegisterLuaxClass(Luax::LuaxState&); \
+ static void RegisterLuaxPostprocess(Luax::LuaxState&); \
static const char* GetLuaxFactoryName() { return #type; };\
static const char* GetLuaxClassName() { return #type; };\
- static bool IsLuaxClassSingleton() { return false; }
+ static bool IsLuaxClassSingleton() { return false; }
///
/// Ϊij󹤳ʹô˺꣬עһڣעắеãעЩ
@@ -30,21 +39,22 @@ namespace Luax
#define LUAX_DECL_ABSTRACT_FACTORY() \
static void RegisterLuaxClass(Luax::LuaxState&);\
static void RegisterLuaxPostprocess(Luax::LuaxState&)
-
+
///
/// RegisterLuaxClass עķͳԱö١ȵclass table
/// LuaxGetSingletonName õ
///
#define LUAX_DECL_SINGLETON(type) \
- static void RegisterLuaxClass(Luax::LuaxState&); \
- static void RegisterLuaxPostprocess(Luax::LuaxState&); \
+ friend class Luax::LuaxState; \
+ static void RegisterLuaxClass(Luax::LuaxState&); \
+ static void RegisterLuaxPostprocess(Luax::LuaxState&); \
static const char* GetLuaxSingletonName() { return #type; }; \
static const char* GetLuaxClassName() { return #type; }; \
- static bool IsLuaxClassSingleton() { return true; }
-
+ static bool IsLuaxClassSingleton() { return true; }
+
#define LUAX_DECL_METHOD(mtd) static int mtd(lua_State* L)
-#define LUAX_DECL_ENUM(e, under_line_index) static void _luax_decl_enum_##e()
+#define LUAX_DECL_ENUM(e, under_line_index) static void _luax_dec_enum_##e()
///
/// ʵֵĺꡣһL
@@ -58,10 +68,10 @@ namespace Luax
#define LUAX_POSTPROCESS(type) void type::RegisterLuaxPostprocess(Luax::LuaxState& state)
///
- /// עĺꡣ
+ /// עĺꡣ֮ǰÿɱ꣬ûluaclastable refûעԡ
///
-#define LUAX_REGISTER_FACTORY(state, type) state.RegisterFactory<type>()
-#define LUAX_REGISTER_SINGLETON(state, type) state.RegisterSingleton<type>()
+#define LUAX_REGISTER_FACTORY(state, param) state.RegisterFactory<param>()
+#define LUAX_REGISTER_SINGLETON(state, param) state.RegisterSingleton<param>()
#define LUAX_REGISTER_ABSTRACT_FACTORY(state, type) type::RegisterLuaxPostprocess(state)
#define LUAX_REGISTER_METHODS(state, ...) \
do{ \
@@ -89,56 +99,82 @@ namespace Luax
/// ݳԱݳԱʼɵһЩ⡣һ㣬vpbӽӽC#Java
/// InterfaceԣIͷʶһӿڡ
///
- class ILuaxNativeAccessor
+ class LuaxObject
{
public:
+ LuaxObject() {};
+ virtual ~LuaxObject() {};
///
- /// Աùʵref tableáȡ
+ /// Աùʵref tableáȡ
///
- virtual bool PushLuaxMemberRef(LuaxState& state, int refID) { assert(false); return false; };
+ virtual bool PushLuaxMemberRef(LuaxState& state, int refID) = 0;
+ virtual bool PushLuaxUserdata(LuaxState& state) = 0;
+ virtual bool PushLuaxMemberTable(LuaxState& state) = 0;
+ virtual bool PushLuaxRefTable(LuaxState& state) = 0;
- virtual bool PushLuaxUserdata(LuaxState& state) { assert(false); return false; };
- virtual bool PushLuaxMemberTable(LuaxState& state) { assert(false); return false; };
- virtual bool PushLuaxRefTable(LuaxState& state) { assert(false); return false; };
+ ///
+ /// LuaxNativeClassʵ֡
+ ///
+ virtual void Retain() = 0;
+ virtual void Release() = 0;
};
+ //class LuaxNativeClassBase
+ //{
+ //}
+
///
/// Ҫ¶luanative classҪ̳дࡣͨluaʵҪȷüȷԣ
/// ߳Ҫȷͷš
///
- template<class T>
- class LuaxNativeClass : virtual public ILuaxNativeAccessor
+ template<class TYPE, class BASE = LuaxObject>
+ class LuaxNativeClass : public BASE
{
public:
- static bool IsTypeOf(ILuaxNativeAccessor);
-
///
/// userdataΪkeyref tableuserdataһãάuserdataڡ
/// Ƚmember refʵᱻαͬʵõƵЩʵ壬
- ///luaƵĵgc⡣
+ /// luaƵĵgc⡣
///
- template<class U> void LuaxRetain(LuaxState& state, U* userdata);
+ template<class USERDATA> void LuaxRetain(LuaxState& state, USERDATA* userdata);
///
/// userdataһref tableԳԻuserdata
///
- template<class U> void LuaxRelease(LuaxState& state, U* userdata);
-
- bool PushLuaxMemberRef(LuaxState& state, int refID) override;
+ template<class USERDATA> void LuaxRelease(LuaxState& state, USERDATA* userdata);
///
/// userdata pushջûгʼmUserdataʼúԪѳʼõ
/// userdataջһáһnativeȨƽluaƵķ
///
- bool PushLuaxUserdata(LuaxState& state);
- bool PushLuaxMemberTable(LuaxState& state);
- bool PushLuaxRefTable(LuaxState& state);
+ bool PushLuaxMemberRef(LuaxState& state, int refID) override;
+ bool PushLuaxUserdata(LuaxState& state) override;
+ bool PushLuaxMemberTable(LuaxState& state) override;
+ bool PushLuaxRefTable(LuaxState& state) override;
+
+ ///
+ /// Watch dog һnativeáluaVMòṩⲿӿڡ̳д಻ֱʹ
+ /// deleteӦʹReleaseͷšһ__gcУû
+ /// nativeиͷţҪʹRelease
+ ///
+ /// nativeӿڡ
+ ///
+ void Retain() override;
+ void Release() override;
+
+#if LUAX_PROFILER
+ // Զϴʵdeleteռ
+ static void operator delete(void* pdead, size_t size);
+#endif
protected:
+ LuaxNativeClass();
+ virtual ~LuaxNativeClass();
+
///
/// Աùʵref tableáȡ
///
@@ -146,36 +182,49 @@ namespace Luax
bool PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
void ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef);
- LuaxNativeClass();
- virtual ~LuaxNativeClass();
-
private:
friend class LuaxState;
- static void RegisterLuaxClass(LuaxState& state);
+ static void RegisterLuaxClassShared(LuaxState& state);
static void RegisterLuaxFactoryClass(LuaxState& state);
static void RegisterLuaxSingletonClass(LuaxState& state);
static void SetLuaxClassTableRef(LuaxState& state, int idx);
-
static void PushLuaxClassTable(LuaxState& state);
-
- ///
- /// ȡַҪַֻͨڶϴʵõջϺ;̬ı
- /// ȡַ֤ü׼ȷҪãʹôݶǴݵַ
- ///
- //void* operator &();
///
/// userdataʵstate
///
void BindToLua(LuaxState& state);
+ //------------------------------------------------------------------------------//
+
+ //
+ LUAX_DECL_METHOD(__tostring);
+ LUAX_DECL_METHOD(_GetClass);
+ LUAX_DECL_METHOD(_GetClassName);
+
+ //
+ LUAX_DECL_METHOD(__gc);
+#if LUAX_ENABLE_NATIVE_EXTEND
+ LUAX_DECL_METHOD(_ExtendFactory);
+#endif
+ LUAX_DECL_METHOD(_GetRefTable);
+ LUAX_DECL_METHOD(_New);
+
+ //
+#if LUAX_ENABLE_NATIVE_EXTEND
+ LUAX_DECL_METHOD(_ExtendSingleton);
+#endif
+
+ //--------------------------------------------------------------------------------//
+
///
/// class table͵С
///
static LuaxStrongRef mClassTable;
+
///
/// ǵsingletonùϵԱ֤ᱻͨref table
/// ijԱȫڵģֱ_LUAX_STRONGREF_TABLEuserdata
@@ -183,7 +232,7 @@ namespace Luax
/// ģtable_LUAX_STRONGREF_TABLE
///
static LuaxStrongRef mSingletonRefTable;
-
+
///
/// ͨuserdataõ:
/// 1: ref table
@@ -192,27 +241,20 @@ namespace Luax
///
LuaxWeakRef mUserdata;
- //
- LUAX_DECL_METHOD( _Tostring );
- LUAX_DECL_METHOD( l_GetClass );
- LUAX_DECL_METHOD( l_GetClassName );
+ ///
+ /// ͨɾ
+ ///
+ LuaxDog mWatchDog;
- //
- LUAX_DECL_METHOD( _GC );
-#if LUAX_ENABLE_NATIVE_EXTEND
- LUAX_DECL_METHOD( l_ExtendFactory );
+#if LUAX_PROFILER
+ // йܴ˶
+ std::unordered_set<LuaxVM*> mRefVMs;
+ // գ಻ⲿʹdeleteֱɾӦʹRelease
+ bool mSafer;
#endif
- LUAX_DECL_METHOD( l_GetRefTable );
- LUAX_DECL_METHOD( l_New );
- //
-#if LUAX_ENABLE_NATIVE_EXTEND
- LUAX_DECL_METHOD( l_ExtendSingleton );
-#endif
};
- //--------------------------------------------------------------------------------//
-
#if LUAX_ENABLE_PLAIN_CLASS
///
/// lua
@@ -226,10 +268,10 @@ namespace Luax
///
static int registry(lua_State* L);
- LUAX_DECL_METHOD( _Tostring );
- LUAX_DECL_METHOD( l_Extend );
- LUAX_DECL_METHOD( l_New );
- LUAX_DECL_METHOD( l_TypeOf );
+ LUAX_DECL_METHOD(__tostring);
+ LUAX_DECL_METHOD(_Extend);
+ LUAX_DECL_METHOD(_New);
+ LUAX_DECL_METHOD(_TypeOf);
};
#endif
diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl
index 45e6552..95965ff 100644
--- a/source/3rd-party/Luax/luax_class.inl
+++ b/source/3rd-party/Luax/luax_class.inl
@@ -2,17 +2,16 @@ namespace Luax
{
//--------------------------------------------------------------------------------//
- // ӿ
///
/// ԲͬͣͨGetLuaClassName࣬GetClassNameᱻǣָluax_c_getupvalue
///
- template<typename T>
- int LuaxNativeClass<T>::l_GetClassName(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_GetClassName(lua_State* L)
{
LUAX_SETUP(L, "*");
- cc8* type = T::GetLuaxClassName();
+ cc8* type = TYPE::GetLuaxClassName();
state.Push(type);
return 1;
}
@@ -22,12 +21,12 @@ namespace Luax
///
/// עṤ͵еԱ
///
- template<typename T>
- void LuaxNativeClass<T>::RegisterLuaxClass(LuaxState& state)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::RegisterLuaxClassShared(LuaxState& state)
{
luaL_Reg regTable[] = {
- { "GetClass", l_GetClass },
- { "GetClassName", l_GetClassName },
+ { "GetClass", _GetClass },
+ { "GetClassName", _GetClassName },
{ NULL, NULL }
};
@@ -37,11 +36,11 @@ namespace Luax
///
/// ijԱעclass table
///
- template<typename T>
- void LuaxNativeClass<T>::RegisterLuaxFactoryClass(LuaxState& state)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::RegisterLuaxFactoryClass(LuaxState& state)
{
luaL_Reg regTable[] = {
- { "GetRefTable", l_GetRefTable },
+ { "GetRefTable", _GetRefTable },
{ NULL, NULL }
};
@@ -51,8 +50,8 @@ namespace Luax
///
/// ijԱעclass table
///
- template<typename T>
- void LuaxNativeClass<T>::RegisterLuaxSingletonClass(LuaxState& state)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::RegisterLuaxSingletonClass(LuaxState& state)
{
luaL_Reg regTable[] = {
{ NULL, NULL }
@@ -61,33 +60,70 @@ namespace Luax
state.RegisterMethods(regTable);
}
- template<typename T>
- void LuaxNativeClass<T>::PushLuaxClassTable(LuaxState& state)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::PushLuaxClassTable(LuaxState& state)
{
assert(mClassTable);
mClassTable.PushRef(state);
}
- template<typename T>
- void LuaxNativeClass<T>::SetLuaxClassTableRef(LuaxState& state, int idx)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::SetLuaxClassTableRef(LuaxState& state, int idx)
{
mClassTable.SetRef(state, idx);
}
- template<typename T>
- LuaxNativeClass<T>::LuaxNativeClass()
+ template<class TYPE, class BASE>
+ LuaxNativeClass<TYPE, BASE>::LuaxNativeClass()
+ : mWatchDog()
+#if LUAX_PROFILER
+ , mSafer(false)
+#endif
+ {
+ }
+
+ template<class TYPE, class BASE>
+ LuaxNativeClass<TYPE, BASE>::~LuaxNativeClass()
+ {
+ }
+
+#if LUAX_PROFILER
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::operator delete(void* pdead, size_t size)
+ {
+ if (pdead == nullptr)
+ return;
+ // ϴʵʹReleaseͷš
+ LuaxNativeClass* p = static_cast<LuaxNativeClass*>(pdead);
+ assert(p->mSafer);
+ ::operator delete(pdead, size);
+ }
+#endif
+
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::Retain()
{
+ ++mWatchDog.mNativeRef;
}
- template<typename T>
- LuaxNativeClass<T>::~LuaxNativeClass()
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::Release()
{
+ if (mWatchDog.mNativeRef > 0)
+ --mWatchDog.mNativeRef;
+ if (mWatchDog)
+ {
+#if LUAX_PROFILER
+ mSafer = true;
+#endif
+ delete this;
+ }
}
- template<typename T>
+ template<class TYPE, class BASE>
template<typename U>
- void LuaxNativeClass<T>::LuaxRetain(LuaxState& state, U* userdata)
+ void LuaxNativeClass<TYPE, BASE>::LuaxRetain(LuaxState& state, U* userdata)
{
if (PushLuaxRefTable(state))
{
@@ -103,9 +139,9 @@ namespace Luax
}
}
- template<typename T>
+ template<class TYPE, class BASE>
template<typename U>
- void LuaxNativeClass<T>::LuaxRelease(LuaxState& state, U* userdata)
+ void LuaxNativeClass<TYPE, BASE>::LuaxRelease(LuaxState& state, U* userdata)
{
if (PushLuaxRefTable(state))
{
@@ -139,10 +175,10 @@ namespace Luax
}
}
- template<typename T>
- bool LuaxNativeClass<T>::PushLuaxUserdata(LuaxState& state)
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxUserdata(LuaxState& state)
{
- assert(!T::IsLuaxClassSingleton());
+ assert(!TYPE::IsLuaxClassSingleton());
if (!mUserdata)
{
BindToLua(state);
@@ -151,8 +187,8 @@ namespace Luax
return mUserdata.PushRef(state);
}
- template<typename T>
- bool LuaxNativeClass<T>::PushLuaxMemberTable(LuaxState& state)
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberTable(LuaxState& state)
{
int top = state.GetTop();
if (this->PushLuaxUserdata(state))
@@ -172,11 +208,11 @@ namespace Luax
return false;
}
- template<typename T>
- bool LuaxNativeClass<T>::PushLuaxRefTable(LuaxState& state)
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxRefTable(LuaxState& state)
{
// Singleton
- if (T::IsLuaxClassSingleton())
+ if (TYPE::IsLuaxClassSingleton())
{
if (!this->mSingletonRefTable) {
lua_newtable(state);
@@ -209,19 +245,21 @@ namespace Luax
/// member table luaʵijԱ
/// class table б͵ʵеĺ
///
- template<typename T>
- void LuaxNativeClass<T>::BindToLua(LuaxState& state)
+ /// BindToLuaֻڵһעLuaʱá
+ ///
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::BindToLua(LuaxState& state)
{
// ܰuserdata
- assert(!T::IsLuaxClassSingleton());
+ assert(!TYPE::IsLuaxClassSingleton());
assert(!mUserdata);
///
- /// userdataջעַҪתΪT*ֱthisܻᵼ¶ؼ̳еɥʧ̬
+ /// userdataջעַҪתΪTYPE*ֱthisܻᵼ¶ؼ̳еɥʧ̬
/// ֱӴthisȥڶؼ̳£òһͷ麯ġҪthis
/// תΪĵ͵ַõһ麯ͨһʵֶ̬
///
- T* p = static_cast<T*>(this);
+ TYPE* p = static_cast<TYPE*>(this);
state.PushPtrUserdata(p);
lua_newtable(state); // ref table޷luaʣC
@@ -239,10 +277,10 @@ namespace Luax
int refTable = top - 2;
// ref table ע __tostring __gc
- lua_pushcfunction(state, _Tostring);
+ lua_pushcfunction(state, __tostring);
lua_setfield(state, refTable, "__tostring");
- lua_pushcfunction(state, _GC);
+ lua_pushcfunction(state, __gc);
lua_setfield(state, refTable, "__gc");
// ref table __index __newindex Ϊ member table
@@ -260,13 +298,19 @@ namespace Luax
// һuserdataãͨPushLuaUserdatalua
mUserdata.SetRef(state, -1);
assert(mUserdata);
+
+ // һãGCʱ-1
+ ++mWatchDog.mVMRef;
+#if LUAX_PROFILER
+ mRefVMs.insert(state.GetVM());
+#endif
}
///
/// Աù
///
- template<typename T>
- void LuaxNativeClass<T>::SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx)
{
ClearLuaxMemberRef(state, memRef);
if (!lua_isnil(state, idx))
@@ -281,8 +325,8 @@ namespace Luax
}
}
- template<typename T>
- bool LuaxNativeClass<T>::PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
{
if (memRef)
{
@@ -301,8 +345,8 @@ namespace Luax
return false;
}
- template<typename T>
- bool LuaxNativeClass<T>::PushLuaxMemberRef(LuaxState& state, int refID)
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberRef(LuaxState& state, int refID)
{
if (PushLuaxRefTable(state))
{
@@ -317,8 +361,8 @@ namespace Luax
return false;
}
- template<typename T>
- void LuaxNativeClass<T>::ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
{
if (memRef)
{
@@ -336,16 +380,20 @@ namespace Luax
///
/// ͷŹʵ
///
- template<typename T>
- int LuaxNativeClass<T>::_GC(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::__gc(lua_State* L)
{
+ LUAX_STATE(L);
+ TYPE* self = state.GetUserdata<TYPE>(1);
+ assert(self);
+
#if LUAX_PROFILER
- std::cout << "Luax: GC<" << T::GetLuaxClassName() << ">\n";
+ std::cout << "Luax: GC<" << TYPE::GetLuaxClassName() << ">\n";
#endif
- LUAX_SETUP(L, "U");
- T* self = state.GetUserdata<T>(1);
- delete self;
+ --self->mWatchDog.mVMRef;
+ self->LuaxNativeClass<TYPE, BASE>::Release();
+
return 0;
}
@@ -353,14 +401,14 @@ namespace Luax
/// ʽ:
/// ַ
///
- template<typename T>
- int LuaxNativeClass<T>::_Tostring(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::__tostring(lua_State* L)
{
// params:
// 1: userdata
LUAX_STATE(L);
- T* self = state.GetUserdata<T>(1);
+ TYPE* self = state.GetUserdata<TYPE>(1);
if (self)
{
cc8* classname = "";
@@ -373,7 +421,7 @@ namespace Luax
}
else
{
- classname = T::GetLuaxClassName();
+ classname = TYPE::GetLuaxClassName();
}
lua_pushfstring(L, "%s: %p", classname, self);
return 1;
@@ -386,8 +434,8 @@ namespace Luax
/// ࣬luaijԱΪƣDZ֤userdataͳһNative classṩ__init֧֣
/// nativeʵ崴ʹ__initгʼӵкͻһNewбnativeһ͡
///
- template<typename T>
- int LuaxNativeClass<T>::l_ExtendFactory(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_ExtendFactory(lua_State* L)
{
// upvalues:
// 1: base class
@@ -414,13 +462,13 @@ namespace Luax
// .Extend()
lua_pushvalue(L, inheritClass);
- lua_pushcclosure(L, l_ExtendFactory, 1);
+ lua_pushcclosure(L, _ExtendFactory, 1);
lua_setfield(L, -2, "Extend");
// .New()
lua_pushvalue(L, inheritClass);
lua_getfield(L, baseClass, "New");
- lua_pushcclosure(L, l_New, 2);
+ lua_pushcclosure(L, _New, 2);
lua_setfield(L, -2, "New");
// __base = baseClass
@@ -438,8 +486,8 @@ namespace Luax
return 1;
}
- template<typename T>
- int LuaxNativeClass<T>::l_ExtendSingleton(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_ExtendSingleton(lua_State* L)
{
// upvalues:
// 1: base class
@@ -466,7 +514,7 @@ namespace Luax
// .Extend()
lua_pushvalue(L, inheritClass);
- lua_pushcclosure(L, l_ExtendFactory, 1);
+ lua_pushcclosure(L, _ExtendFactory, 1);
lua_setfield(L, -2, "Extend");
// __base = baseClass
@@ -485,8 +533,8 @@ namespace Luax
}
#endif /*LUAX_ENABLE_NATIVE_EXTEND*/
- template<typename T>
- int LuaxNativeClass<T>::l_GetClass(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_GetClass(lua_State* L)
{
LUAX_STATE(L);
if (!mClassTable)
@@ -496,19 +544,19 @@ namespace Luax
return 1;
}
- template<typename T>
- int LuaxNativeClass<T>::l_GetRefTable(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_GetRefTable(lua_State* L)
{
LUAX_STATE(L);
- T* self = state.GetUserdata<T>(1);
+ TYPE* self = state.GetUserdata<TYPE>(1);
bool success = self->PushLuaxRefTable(state);
if (!success)
lua_pushnil(L);
return 1;
}
- template<typename T>
- int LuaxNativeClass<T>::l_New(lua_State* L)
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_New(lua_State* L)
{
LUAX_STATE(L);
@@ -580,7 +628,7 @@ namespace Luax
return 0;
}
- template<typename T> LuaxStrongRef LuaxNativeClass<T>::mClassTable; // class table
- template<typename T> LuaxStrongRef LuaxNativeClass<T>::mSingletonRefTable; //
+ template<class TYPE, class BASE> LuaxStrongRef LuaxNativeClass<TYPE, BASE>::mClassTable; // class table
+ template<class TYPE, class BASE> LuaxStrongRef LuaxNativeClass<TYPE, BASE>::mSingletonRefTable; //
} \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_config.h b/source/3rd-party/Luax/luax_config.h
index 2a8ed3e..31ea7df 100644
--- a/source/3rd-party/Luax/luax_config.h
+++ b/source/3rd-party/Luax/luax_config.h
@@ -57,10 +57,10 @@ namespace Luax
#define LUAX_PROFILER 1
+}
+
#if LUAX_PROFILER
#include <iostream>
#endif
-}
-
-#endif \ No newline at end of file
+#endif // __LUAX_TYPE_H__ \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_dog.cpp b/source/3rd-party/Luax/luax_dog.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/source/3rd-party/Luax/luax_dog.cpp
diff --git a/source/3rd-party/Luax/luax_dog.h b/source/3rd-party/Luax/luax_dog.h
new file mode 100644
index 0000000..f6d95d5
--- /dev/null
+++ b/source/3rd-party/Luax/luax_dog.h
@@ -0,0 +1,33 @@
+#ifndef __LUAX_DOG_H__
+#define __LUAX_DOG_H__
+
+#include "luax_config.h"
+
+namespace Luax
+{
+
+ ///
+ /// LuaxNativeClassʵüwatch dogֻwatch dogͨʱſdelete
+ ///
+ class LuaxDog
+ {
+ public:
+ LuaxDog()
+ : mVMRef(0)
+ , mNativeRef(0)
+ {
+ }
+
+ inline operator bool()
+ {
+ return mVMRef == 0 && mNativeRef == 0;
+ }
+
+ uint mVMRef; // йܸ
+ uint mNativeRef; //
+
+ };
+
+}
+
+#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_enum.cpp b/source/3rd-party/Luax/luax_enum.cpp
index 59d8161..ec73fce 100644
--- a/source/3rd-party/Luax/luax_enum.cpp
+++ b/source/3rd-party/Luax/luax_enum.cpp
@@ -8,7 +8,7 @@ namespace Luax
///
/// ֻmetatable__index
///
- int l_rmt__index(lua_State* L)
+ int _rmt__index(lua_State* L)
{
// params:
// 1: enum table
@@ -24,7 +24,7 @@ namespace Luax
return 1;
}
- int l_rmt__newindex(lua_State* L)
+ int _rmt__newindex(lua_State* L)
{
// upvalue:
// 1: enum table name
@@ -53,7 +53,7 @@ namespace Luax
lua_setfield(L, -2, "__index");
lua_pushstring(L, name);
- lua_pushcclosure(L, l_rmt__newindex, 1);
+ lua_pushcclosure(L, _rmt__newindex, 1);
lua_setfield(L, -2, "__newindex");
lua_newtable(L); // enum table
diff --git a/source/3rd-party/Luax/luax_enum.h b/source/3rd-party/Luax/luax_enum.h
index 395eaf0..9afece2 100644
--- a/source/3rd-party/Luax/luax_enum.h
+++ b/source/3rd-party/Luax/luax_enum.h
@@ -15,9 +15,9 @@ namespace Luax
int value;
};
- extern int l_rmt__index(lua_State* L);
+ extern int _rmt__index(lua_State* L);
- extern int l_rmt__newindex(lua_State* L);
+ extern int _rmt__newindex(lua_State* L);
//--------------------------------------------------------------------------------//
diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp
index 612f26e..a2610b4 100644
--- a/source/3rd-party/Luax/luax_state.cpp
+++ b/source/3rd-party/Luax/luax_state.cpp
@@ -684,13 +684,13 @@ namespace Luax
// __index
//lua_pushvalue(L, -1); // metatable
- //lua_pushcclosure(L, l_rmt__index, 1);
+ //lua_pushcclosure(L, _rmt__index, 1);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
// __newinedx
lua_pushstring(L, name); // enum name
- lua_pushcclosure(L, l_rmt__newindex, 1);
+ lua_pushcclosure(L, _rmt__newindex, 1);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, et);
diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h
index 7c7d813..e162965 100644
--- a/source/3rd-party/Luax/luax_state.h
+++ b/source/3rd-party/Luax/luax_state.h
@@ -153,12 +153,12 @@ namespace Luax
///
/// עṤͨ࣬New
///
- template<typename T> void RegisterFactory();
+ template<class TYPE> void RegisterFactory();
///
/// עᵥûNew
///
- template<typename T> void RegisterSingleton();
+ template<class TYPE> void RegisterSingleton();
///
/// עö
diff --git a/source/3rd-party/Luax/luax_state.inl b/source/3rd-party/Luax/luax_state.inl
index 20d132c..6671bb5 100644
--- a/source/3rd-party/Luax/luax_state.inl
+++ b/source/3rd-party/Luax/luax_state.inl
@@ -4,9 +4,11 @@ namespace Luax
///
/// עṤעclass tabletype nameΪƿռϡע׶βԪȵNewõʱŻᡣ
///
- template<typename T>
+ template<class TYPE>
void LuaxState::RegisterFactory()
{
+ cc8* type = TYPE::GetLuaxFactoryName();
+
lua_State* L = mState;
LuaxState& state = *this;
@@ -15,11 +17,11 @@ namespace Luax
// class table
lua_newtable(L);
- LuaxNativeClass<T>::RegisterLuaxClass(state);
- LuaxNativeClass<T>::RegisterLuaxFactoryClass(state);
- T::RegisterLuaxClass(state);
+ TYPE::RegisterLuaxClassShared(state);
+ TYPE::RegisterLuaxFactoryClass(state);
+ TYPE::RegisterLuaxClass(state);
- // TǷûעķ
+ // TYPEǷûעķ
#define _assertmethod(I, NAME) \
GetField(I, NAME); \
assert(IsType(-1, LUA_TFUNCTION)); \
@@ -32,7 +34,7 @@ namespace Luax
#if LUAX_ENABLE_NATIVE_EXTEND
// .Extend()
lua_pushvalue(state, -1); // class table
- lua_pushcclosure(state, LuaxNativeClass<T>::l_ExtendFactory, 1);
+ lua_pushcclosure(state, TYPE::_ExtendFactory, 1);
lua_setfield(state, -2, "Extend");
#endif
@@ -40,22 +42,21 @@ namespace Luax
lua_pushvalue(state, -1); // class table
lua_setfield(state, -2, "__index");
- LuaxNativeClass<T>::SetLuaxClassTableRef(state, -1);
+ TYPE::SetLuaxClassTableRef(state, -1);
- cc8* type = T::GetLuaxFactoryName();
SetField(top, type);
// reset top
lua_settop(L, top);
//
- T::RegisterLuaxPostprocess(state);
+ TYPE::RegisterLuaxPostprocess(state);
}
///
/// Singleton
///
- template<typename T>
+ template<typename TYPE>
void LuaxState::RegisterSingleton()
{
lua_State* L = mState;
@@ -66,11 +67,11 @@ namespace Luax
// class table.
lua_newtable(L);
- LuaxNativeClass<T>::RegisterLuaxClass(state);
- LuaxNativeClass<T>::RegisterLuaxSingletonClass(state);
- T::RegisterLuaxClass(state);
+ TYPE::RegisterLuaxClassShared(state);
+ TYPE::RegisterLuaxSingletonClass(state);
+ TYPE::RegisterLuaxClass(state);
- LuaxNativeClass<T>::SetLuaxClassTableRef(state, -1);
+ TYPE::SetLuaxClassTableRef(state, -1);
lua_pushvalue(state, -1);
lua_setfield(state, -2, "__index");
@@ -78,22 +79,22 @@ namespace Luax
#if LUAX_ENABLE_NATIVE_EXTEND
// .Extend()
lua_pushvalue(state, -1); // class table
- lua_pushcclosure(state, LuaxNativeClass<T>::l_ExtendSingleton, 1);
+ lua_pushcclosure(state, TYPE::_ExtendSingleton, 1);
lua_setfield(state, -2, "Extend");
#endif
- cc8* type = T::GetLuaxSingletonName();
+ cc8* type = TYPE::GetLuaxSingletonName();
SetField(top, type);
// reset top
lua_settop(L, top);
//
- T::RegisterLuaxPostprocess(state);
+ TYPE::RegisterLuaxPostprocess(state);
}
- template<typename T>
- void LuaxState::SetField(int idx, cc8* key, T value)
+ template<typename TYPE>
+ void LuaxState::SetField(int idx, cc8* key, TYPE value)
{
if (IsTableOrUserdata(idx))
{
@@ -103,8 +104,8 @@ namespace Luax
}
}
- template<typename T>
- void LuaxState::SetFieldByIndex(int idx, int key, T value)
+ template<typename TYPE>
+ void LuaxState::SetFieldByIndex(int idx, int key, TYPE value)
{
if (IsTableOrUserdata(idx))
{
@@ -114,28 +115,28 @@ namespace Luax
}
}
- template<typename T>
- T LuaxState::GetField(int idx, cc8* key, T value)
+ template<typename TYPE>
+ TYPE LuaxState::GetField(int idx, cc8* key, TYPE value)
{
GetField(idx, key);
- T result = GetValue < T >(-1, value);
+ TYPE result = GetValue < TYPE >(-1, value);
this->Pop();
return result;
}
- template<typename T>
- T LuaxState::GetField(int idx, int key, T value)
+ template<typename TYPE>
+ TYPE LuaxState::GetField(int idx, int key, TYPE value)
{
GetField(idx, key);
- T result = GetValue < T >(-1, value);
+ TYPE result = GetValue < TYPE >(-1, value);
Pop();
return result;
}
- template<typename T>
- T* LuaxState::GetUserdata(int idx)
+ template<typename TYPE>
+ TYPE* LuaxState::GetUserdata(int idx)
{
void* p = nullptr;
@@ -144,11 +145,11 @@ namespace Luax
p = *(void**)lua_touserdata(mState, idx);
}
- return static_cast<T*>(p);
+ return static_cast<TYPE*>(p);
}
- template<typename T>
- T* LuaxState::CheckUserdata(int idx)
+ template<typename TYPE>
+ TYPE* LuaxState::CheckUserdata(int idx)
{
if (IsType(idx, LUA_TUSERDATA))
{
@@ -158,11 +159,11 @@ namespace Luax
{
if (lua_getmetatable(mState, -1)) // class table
{
- T::PushLuaxClassTable(*this); // target class table
+ TYPE::PushLuaxClassTable(*this); // target class table
if (lua_rawequal(mState, -1, -2))
{
Pop(4); // ref\member\class\target class
- T* udata = GetUserdata<T>(idx);
+ TYPE* udata = GetUserdata<TYPE>(idx);
return udata; // userdata
}
Pop(2); // target class table\class table
@@ -172,7 +173,7 @@ namespace Luax
Pop(1); // ref table
}
}
- luaL_typerror(mState, idx, T::GetLuaxClassName());
+ luaL_typerror(mState, idx, TYPE::GetLuaxClassName());
return nullptr;
}