From 8d8c4ff1664625e7428d0d31cd798d9321680cb2 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 14 Mar 2019 09:08:07 +0800 Subject: *luax --- Source/3rdParty/Luax/luax_state.inl | 182 +++++++++++++++++++++--------------- 1 file changed, 106 insertions(+), 76 deletions(-) (limited to 'Source/3rdParty/Luax/luax_state.inl') 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 -- cgit v1.1-26-g67d0