summaryrefslogtreecommitdiff
path: root/Source/external/Luax/luax_class.inc
diff options
context:
space:
mode:
Diffstat (limited to 'Source/external/Luax/luax_class.inc')
-rw-r--r--Source/external/Luax/luax_class.inc637
1 files changed, 637 insertions, 0 deletions
diff --git a/Source/external/Luax/luax_class.inc b/Source/external/Luax/luax_class.inc
new file mode 100644
index 0000000..1d6a89f
--- /dev/null
+++ b/Source/external/Luax/luax_class.inc
@@ -0,0 +1,637 @@
+namespace Luax
+{
+
+ //--------------------------------------------------------------------------------//
+
+ ///
+ /// ԲͬͣͨGetLuaClassName࣬GetClassNameᱻǣָluax_c_getupvalue
+ ///
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_GetClassName(lua_State* L)
+ {
+ LUAX_SETUP(L, "*");
+
+ cc8* type = TYPE::GetLuaxClassName();
+ state.Push(type);
+ return 1;
+ }
+
+ //--------------------------------------------------------------------------------//
+
+ ///
+ /// עṤ͵еԱ
+ ///
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::RegisterLuaxClassShared(LuaxState& state)
+ {
+ luaL_Reg regTable[] = {
+ { "GetClass", _GetClass },
+ { "GetClassName", _GetClassName },
+ { NULL, NULL }
+ };
+
+ state.RegisterMethods(regTable);
+ }
+
+ ///
+ /// ijԱעclass table
+ ///
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::RegisterLuaxFactoryClass(LuaxState& state)
+ {
+ luaL_Reg regTable[] = {
+ { "GetRefTable", _GetRefTable },
+ { NULL, NULL }
+ };
+
+ state.RegisterMethods(regTable);
+ }
+
+ ///
+ /// ijԱעclass table
+ ///
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::RegisterLuaxSingletonClass(LuaxState& state)
+ {
+ luaL_Reg regTable[] = {
+ { NULL, NULL }
+ };
+
+ state.RegisterMethods(regTable);
+ }
+
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::PushLuaxClassTable(LuaxState& state)
+ {
+ assert(mClassTable);
+
+ mClassTable.PushRef(state);
+ }
+
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::SetLuaxClassTableRef(LuaxState& state, int idx)
+ {
+ mClassTable.SetRef(state, idx);
+ }
+
+ 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ͷš
+ TYPE* p = static_cast<TYPE*>(pdead);
+ assert(p->mSafer);
+ ::operator delete(pdead, size);
+ }
+#endif
+
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::Retain()
+ {
+ ++mWatchDog.mNativeRef;
+ }
+
+ 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<class TYPE, class BASE>
+ template<typename U>
+ void LuaxNativeClass<TYPE, BASE>::LuaxRetain(LuaxState& state, U* userdata)
+ {
+ if (PushLuaxRefTable(state))
+ {
+ if (userdata->PushLuaxUserdata(state))
+ {
+ lua_pushvalue(state, -1); // copy the userdata
+ lua_gettable(state, -3); // get the count (or nil)
+ u32 count = state.GetValue<u32>(-1, 0); // get the count (or 0)
+ lua_pop(state, 1); // pop the old count
+ lua_pushnumber(state, count + 1); // push the new count
+ lua_settable(state, -3); // save it in the table: reftable[userdata] = count
+ }
+ }
+ }
+
+ template<class TYPE, class BASE>
+ template<typename U>
+ void LuaxNativeClass<TYPE, BASE>::LuaxRelease(LuaxState& state, U* userdata)
+ {
+ if (PushLuaxRefTable(state))
+ {
+ if (userdata->PushLuaxUserdata(state))
+ {
+ lua_pushvalue(state, -1); // copy the userdata
+ lua_gettable(state, -3); // get the count (or nil)
+ u32 count = state.GetValue<u32>(-1, 0); // get the count (or 0)
+ lua_pop(state, 1); // pop the old count
+
+ // no such reference
+ if (count == 0)
+ {
+ state.Pop(2); // userdata, reftable
+ return; // nothing to do
+ }
+
+ if (count > 1) {
+ lua_pushnumber(state, count - 1); // push the new count
+ }
+ else {
+ lua_pushnil(state); // maybe cause gc
+ }
+ lua_settable(state, -3); // save it in the table
+
+ state.Pop(1); // reftable
+ return;
+ }
+ state.Pop(2); // nil, reftable
+ return;
+ }
+ }
+
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxUserdata(LuaxState& state)
+ {
+ assert(!TYPE::IsLuaxClassSingleton());
+ if (!mUserdata)
+ {
+ BindToLua(state);
+ return true;
+ }
+ return mUserdata.PushRef(state);
+ }
+
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberTable(LuaxState& state)
+ {
+ int top = state.GetTop();
+ if (this->PushLuaxUserdata(state))
+ {
+ if (lua_getmetatable(state, -1)) // ref table
+ {
+ lua_replace(state, -2);
+ if (lua_getmetatable(state, -1)) // member table
+ {
+ lua_replace(state, -2);
+ return true;
+ }
+ }
+ }
+ lua_settop(state, top);
+ lua_pushnil(state);
+ return false;
+ }
+
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxRefTable(LuaxState& state)
+ {
+ // Singleton
+ if (TYPE::IsLuaxClassSingleton())
+ {
+ if (!this->mSingletonRefTable) {
+ lua_newtable(state);
+ this->mSingletonRefTable.SetRef(state, -1); // strong ref to member table won't be garbage collected
+ }
+ else {
+ this->mSingletonRefTable.PushRef(state);
+ }
+ return true;
+ }
+ // Factory
+ else
+ {
+ if (this->PushLuaxUserdata(state))
+ {
+ if (lua_getmetatable(state, -1))
+ {
+ lua_replace(state, -2);
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ ///
+ /// userdataԴref tablemember tableclass table
+ /// ref table kvǿtableuserdataüͨuserdataΪkey
+ /// ΪvalueԼԱ
+ /// member table luaʵijԱ
+ /// class table б͵ʵеĺ
+ ///
+ /// BindToLuaֻڵһעLuaʱá
+ ///
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::BindToLua(LuaxState& state)
+ {
+ // ܰuserdata
+ assert(!TYPE::IsLuaxClassSingleton());
+ assert(!mUserdata);
+
+ ///
+ /// userdataջעַҪתΪTYPE*ֱthisܻᵼ¶ؼ̳еɥʧ̬
+ /// ֱӴthisȥڶؼ̳£òһͷ麯ġҪthis
+ /// תΪĵ͵ַõһ麯ͨһʵֶ̬
+ ///
+ TYPE* p = static_cast<TYPE*>(this);
+ state.PushPtrUserdata(p);
+
+ lua_newtable(state); // ref table޷luaʣC
+ lua_newtable(state); // member tableluaдĶԱ
+ PushLuaxClassTable(state); // class table
+
+ // stack:
+ // -1: class table
+ // -2: member table
+ // -3: ref table
+ // -4: userdata
+
+ int top = state.GetTop();
+ int memberTable = top - 1;
+ int refTable = top - 2;
+
+ // ref table ע __tostring __gc
+ lua_pushcfunction(state, __tostring);
+ lua_setfield(state, refTable, "__tostring");
+
+ lua_pushcfunction(state, __gc);
+ lua_setfield(state, refTable, "__gc");
+
+ // ref table __index __newindex Ϊ member table
+ lua_pushvalue(state, memberTable);
+ lua_setfield(state, refTable, "__index");
+
+ lua_pushvalue(state, memberTable);
+ lua_setfield(state, refTable, "__newindex");
+
+ // Ԫ
+ lua_setmetatable(state, -2); // class is meta of member
+ lua_setmetatable(state, -2); // member is meta of ref
+ lua_setmetatable(state, -2); // ref is meta of userdata
+
+ // һuserdataãͨPushLuaUserdatalua
+ mUserdata.SetRef(state, -1);
+ assert(mUserdata);
+
+ // һãGCʱ-1
+ ++mWatchDog.mVMRef;
+#if LUAX_PROFILER
+ mRefVMs.insert(state.GetVM());
+#endif
+ }
+
+ ///
+ /// Աù
+ ///
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::SetLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef, int idx)
+ {
+ ClearLuaxMemberRef(state, memRef);
+ if (!lua_isnil(state, idx))
+ {
+ idx = state.AbsIndex(idx);
+ if (PushLuaxRefTable(state))
+ {
+ lua_pushvalue(state, idx);
+ memRef.refID = luaL_ref(state, -2);
+ state.Pop(); // ref table
+ }
+ }
+ }
+
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
+ {
+ if (memRef)
+ {
+ if (PushLuaxRefTable(state))
+ {
+ lua_rawgeti(state, -1, memRef.refID);
+ lua_replace(state, -2); // ref table
+ if (lua_isnil(state, -1))
+ goto failed;
+ return true;
+ }
+ }
+ lua_pushnil(state);
+ failed:
+ memRef.refID = LUA_NOREF;
+ return false;
+ }
+
+ template<class TYPE, class BASE>
+ bool LuaxNativeClass<TYPE, BASE>::PushLuaxMemberRef(LuaxState& state, int refID)
+ {
+ if (PushLuaxRefTable(state))
+ {
+ lua_rawgeti(state, -1, refID);
+ lua_replace(state, -2); // ref table
+ if (lua_isnil(state, -1))
+ goto failed;
+ return true;
+ }
+ lua_pushnil(state);
+ failed:
+ return false;
+ }
+
+ template<class TYPE, class BASE>
+ void LuaxNativeClass<TYPE, BASE>::ClearLuaxMemberRef(LuaxState& state, LuaxMemberRef& memRef)
+ {
+ if (memRef)
+ {
+ if (PushLuaxRefTable(state))
+ {
+ luaL_unref(state, -1, memRef.refID);
+ state.Pop(); // ref table
+ }
+ memRef.refID = LUA_NOREF;
+ }
+ }
+
+ //--------------------------------------------------------------------------------//
+
+ ///
+ /// ͷŹʵ
+ ///
+ 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<" << TYPE::GetLuaxClassName() << ">\n";
+#endif
+
+ if(self->mWatchDog.mVMRef > 0)
+ --self->mWatchDog.mVMRef;
+
+ self->Release();
+
+ return 0;
+ }
+
+ ///
+ /// ʽ:
+ /// ַ
+ ///
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::__tostring(lua_State* L)
+ {
+ // params:
+ // 1: userdata
+
+ LUAX_STATE(L);
+ TYPE* self = state.GetUserdata<TYPE>(1);
+ if (self)
+ {
+ cc8* classname = "";
+ lua_getfield(state, 1, "GetClassName");
+ if (state.IsType(-1, LUA_TFUNCTION))
+ {
+ lua_pushvalue(L, 1); // userdata
+ state.Call(1, 1); // GetClassName
+ classname = state.GetValue<cc8*>(-1, "");
+ }
+ else
+ {
+ classname = TYPE::GetLuaxClassName();
+ }
+ lua_pushfstring(L, "%s: %p", classname, self);
+ return 1;
+ }
+ return 0;
+ }
+
+#if LUAX_ENABLE_NATIVE_EXTEND
+ ///
+ /// ࣬luaijԱΪƣDZ֤userdataͳһNative classṩ__init֧֣
+ /// nativeʵ崴ʹ__initгʼӵкͻһNewбnativeһ͡
+ ///
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_ExtendFactory(lua_State* L)
+ {
+ // upvalues:
+ // 1: base class
+
+ // params:
+ // 1: class name
+
+ int baseClass = lua_upvalueindex(1);
+
+ lua_newtable(L); // class table
+
+ int inheritClass = lua_gettop(L);
+
+ // .GetClassName()
+ cc8* type = lua_tostring(L, 1);
+ lua_pushstring(L, type);
+ lua_pushcclosure(L, luax_c_getupvalue, 1);
+ lua_setfield(L, -2, "GetClassName");
+
+ // .GetClass()
+ lua_pushvalue(L, inheritClass);
+ lua_pushcclosure(L, luax_c_getupvalue, 1);
+ lua_setfield(L, -2, "GetClass");
+
+ // .Extend()
+ lua_pushvalue(L, inheritClass);
+ lua_pushcclosure(L, _ExtendFactory, 1);
+ lua_setfield(L, -2, "Extend");
+
+ // .New()
+ lua_pushvalue(L, inheritClass);
+ lua_getfield(L, baseClass, "New");
+ lua_pushcclosure(L, _New, 2);
+ lua_setfield(L, -2, "New");
+
+ // __base = baseClass
+ lua_pushvalue(L, baseClass);
+ lua_setfield(L, -2, "__base");
+
+ // __index = inheritClass
+ lua_pushvalue(L, inheritClass);
+ lua_setfield(L, -2, "__index");
+
+ // metatable is baseClass
+ lua_pushvalue(L, baseClass);
+ lua_setmetatable(L, inheritClass);
+
+ return 1;
+ }
+
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_ExtendSingleton(lua_State* L)
+ {
+ // upvalues:
+ // 1: base class
+
+ // params:
+ // 1: class name
+
+ int baseClass = lua_upvalueindex(1);
+
+ lua_newtable(L); // class name
+
+ int inheritClass = lua_gettop(L);
+
+ // .GetClassName()
+ cc8* type = lua_tostring(L, 1);
+ lua_pushstring(L, type);
+ lua_pushcclosure(L, luax_c_getupvalue, 1);
+ lua_setfield(L, -2, "GetClassName");
+
+ // .GetClass()
+ lua_pushvalue(L, inheritClass);
+ lua_pushcclosure(L, luax_c_getupvalue, 1);
+ lua_setfield(L, -2, "GetClass");
+
+ // .Extend()
+ lua_pushvalue(L, inheritClass);
+ lua_pushcclosure(L, _ExtendFactory, 1);
+ lua_setfield(L, -2, "Extend");
+
+ // __base = baseClass
+ lua_pushvalue(L, baseClass);
+ lua_setfield(L, -2, "__base");
+
+ // __index = inheritClass
+ lua_pushvalue(L, inheritClass);
+ lua_setfield(L, -2, "__index");
+
+ // metatable is baseClass
+ lua_pushvalue(L, baseClass);
+ lua_setmetatable(L, inheritClass);
+
+ return 1;
+ }
+#endif /*LUAX_ENABLE_NATIVE_EXTEND*/
+
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_GetClass(lua_State* L)
+ {
+ LUAX_STATE(L);
+ if (!mClassTable)
+ lua_pushnil(L);
+ else
+ mClassTable.PushRef(state);
+ return 1;
+ }
+
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_GetRefTable(lua_State* L)
+ {
+ LUAX_STATE(L);
+ TYPE* self = state.GetUserdata<TYPE>(1);
+ bool success = self->PushLuaxRefTable(state);
+ if (!success)
+ lua_pushnil(L);
+ return 1;
+ }
+
+ template<class TYPE, class BASE>
+ int LuaxNativeClass<TYPE, BASE>::_New(lua_State* L)
+ {
+ LUAX_STATE(L);
+
+ // upvalues:
+ // 1: class table
+ // 2: original New()
+
+ // stack:
+ // -1~-n: args
+
+ int n = lua_gettop(L); // n args
+
+ lua_pushvalue(L, lua_upvalueindex(2));
+ if (state.IsType(-1, LUA_TFUNCTION))
+ {
+ // stack:
+ // -1: New
+ // -2~-1-n: args
+
+ state.PushValues(-1 - n, n);
+
+ // stack:
+ // -1~-n: args
+ // -n-1: New
+ // -n-2~-1-2n: args
+
+ state.Call(n, 1);
+
+ // stack:
+ // -1: userdata
+ // -2~-1-n: args
+
+ // reset member table's metatable to class table
+ if (state.IsType(-1, LUA_TUSERDATA))
+ {
+ if (lua_getmetatable(L, -1)) // ref table
+ {
+ if (lua_getmetatable(L, -1)) // member table
+ {
+ lua_pushvalue(L, lua_upvalueindex(1)); // class table
+ lua_setmetatable(L, -2);
+ state.Pop(); // member table
+ }
+ state.Pop(); // ref table
+ }
+
+ // stack:
+ // -1: userdata
+ // -2~-1-n: args
+
+ int args = state.AbsIndex(-1 - n);
+
+ // Ե__init
+ lua_getfield(L, lua_upvalueindex(1), "__init");
+
+ if (state.IsType(-1, LUA_TFUNCTION))
+ {
+ lua_pushvalue(L, -2); // userdata
+ state.PushValues(args, n);
+ state.Call(n + 1, 0);
+ }
+ else
+ state.Pop();
+
+ }
+
+ return 1;
+ }
+ return 0;
+ }
+
+ 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