From 82956beb1fe17e1226327638c8ab22b5f5adfc1d Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 20 Mar 2019 22:43:25 +0800 Subject: *misc --- source/3rd-party/Luax/luax_class.hpp | 25 +++++-- source/3rd-party/Luax/luax_class.inl | 8 +-- source/3rd-party/Luax/luax_config.h | 2 +- source/3rd-party/Luax/luax_enum.cpp | 2 +- source/3rd-party/Luax/luax_enum.h | 2 +- source/3rd-party/Luax/luax_state.cpp | 122 +++++++++++++++++++++++++++++++---- source/3rd-party/Luax/luax_state.h | 33 ++++++++-- source/3rd-party/Luax/luax_state.inl | 31 ++++++++- 8 files changed, 197 insertions(+), 28 deletions(-) (limited to 'source/3rd-party') diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp index ea1fab9..539ff1a 100644 --- a/source/3rd-party/Luax/luax_class.hpp +++ b/source/3rd-party/Luax/luax_class.hpp @@ -35,15 +35,32 @@ namespace Luax static const char* GetLuaxClassName() { return #type; }; \ static bool IsLuaxClassSingleton() { return true; }; + /// + /// 标明方法实现的宏。上下文里有一个L。 + /// #define LUAX_IMPL_METHOD(type, f) int type::f(lua_State* L) + /// + /// 由应用程序实现的两个接口。上下文里有一个state。 + /// #define LUAX_REGISTRY(type) void type::RegisterLuaxClass(Luax::LuaxState& state) - #define LUAX_POSTPROCESS(type) void type::RegisterLuaxPostprocess(Luax::LuaxState& state) -#define LUAX_REGISTER_FACTORY(stt, type) stt.RegisterFactory() - -#define LUAX_REGISTER_SINGLETON(stt, type) stt.RegisterSingleton() + /// + /// 用来注册的宏。 + /// +#define LUAX_REGISTER_FACTORY(state, type) state.RegisterFactory() +#define LUAX_REGISTER_SINGLETON(state, type) state.RegisterSingleton() +#define LUAX_REGISTER_METHODS(state, ...) \ + do{ \ + luaL_Reg __m[] = {__VA_ARGS__,{0, 0}}; \ + state.RegisterMethods(__m); \ + }while(0) +#define LUAX_REGISTER_ENUM(state, name, ...) \ + do{ \ + Luax::LuaxEnum __e[] = {__VA_ARGS__,{0, 0}}; \ + state.RegisterEnum(name, __e); \ + }while(0) /// /// 需要暴露给lua的native class需要继承此类。通过lua管理的实例要确保引用计数的正确性,在多个线程中需要确定不会误释放。 diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl index 028d4f9..7a24896 100644 --- a/source/3rd-party/Luax/luax_class.inl +++ b/source/3rd-party/Luax/luax_class.inl @@ -206,7 +206,7 @@ namespace Luax assert(!mUserdata); // 创建userdata并留在栈顶 - state.PushPtrUserData(this); + state.PushPtrUserdata(this); lua_newtable(state); // ref table,无法在lua处访问,用来管理C对象的生命周期 lua_newtable(state); // member table,lua中创建的对象成员都保存在这里 @@ -308,7 +308,7 @@ namespace Luax int LuaxNativeClass::l___gc(lua_State* L) { LUAX_SETUP(L, "U"); - T* self = state.GetLuaUserdata(1); + T* self = state.GetUserdata(1); delete self; return 0; } @@ -324,7 +324,7 @@ namespace Luax // 1: userdata LUAX_STATE(L); - T* self = state.GetLuaUserdata(1); + T* self = state.GetUserdata(1); if (self) { cc8* classname = ""; @@ -464,7 +464,7 @@ namespace Luax int LuaxNativeClass::l_GetRefTable(lua_State* L) { LUAX_STATE(L); - T* self = state.GetLuaUserdata(1); + T* self = state.GetUserdata(1); bool success = self->PushLuaxRefTable(state); if (!success) lua_pushnil(L); diff --git a/source/3rd-party/Luax/luax_config.h b/source/3rd-party/Luax/luax_config.h index c6562cc..d95ae8b 100644 --- a/source/3rd-party/Luax/luax_config.h +++ b/source/3rd-party/Luax/luax_config.h @@ -53,7 +53,7 @@ namespace Luax #define LUAX_ENABLE_NATIVE_EXTEND 0 #define LUAX_ENABLE_PLAIN_CLASS 1 -#define LUAX_ENABLE_PLAIN_ENABLE 1 +#define LUAX_ENABLE_PLAIN_ENUM 1 } diff --git a/source/3rd-party/Luax/luax_enum.cpp b/source/3rd-party/Luax/luax_enum.cpp index 88bbab4..b054d98 100644 --- a/source/3rd-party/Luax/luax_enum.cpp +++ b/source/3rd-party/Luax/luax_enum.cpp @@ -35,7 +35,7 @@ namespace Luax } //-------------------------------------------------------------------------------------------------------------- -#if LUAX_ENABLE_PLAIN_ENABLE +#if LUAX_ENABLE_PLAIN_ENUM int LuaxPlainEnum::registry(lua_State* L) { // params: diff --git a/source/3rd-party/Luax/luax_enum.h b/source/3rd-party/Luax/luax_enum.h index 36f6bab..c385dc5 100644 --- a/source/3rd-party/Luax/luax_enum.h +++ b/source/3rd-party/Luax/luax_enum.h @@ -20,7 +20,7 @@ namespace Luax extern int l_rmt__newindex(lua_State* L); //-------------------------------------------------------------------------------------------------------------- -#if LUAX_ENABLE_PLAIN_ENABLE +#if LUAX_ENABLE_PLAIN_ENUM /// /// 纯lua的枚举,创建不可修改的table /// diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp index 881aaea..622f352 100644 --- a/source/3rd-party/Luax/luax_state.cpp +++ b/source/3rd-party/Luax/luax_state.cpp @@ -596,15 +596,6 @@ namespace Luax return value; } - template<> - int LuaxState::GetValue(int idx, int value) - { - if (this->IsType(idx, LUA_TNUMBER)) { - return (int)lua_tointeger(this->mState, idx); - } - return value; - } - template <> u16 LuaxState::GetValue < u16 >(int idx, const u16 value) { @@ -641,7 +632,7 @@ namespace Luax return value; } - void LuaxState::PushPtrUserData(void* ptr) { + void LuaxState::PushPtrUserdata(void* ptr) { void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*)); assert(handle); @@ -725,7 +716,7 @@ namespace Luax } #endif -#if LUAX_ENABLE_PLAIN_ENABLE +#if LUAX_ENABLE_PLAIN_ENUM void LuaxState::RegisterPlainEnumRegistry(cc8* name) { assert(lua_istable(mState, -1)); @@ -734,4 +725,113 @@ namespace Luax } #endif + int LuaxState::ErrorType(int idx, cc8* hint) + { + return luaL_typerror(mState, idx, hint); + } + + template <> + bool LuaxState::CheckParam < bool >(int idx) + { + bool b = false; + if (lua_type(mState, idx) == LUA_TBOOLEAN) + { + b = lua_toboolean(mState, idx); + } + else + { + luaL_typerror(mState, idx, lua_typename(mState, LUA_TBOOLEAN)); + } + return b; + } + + template <> + cc8* LuaxState::CheckParam < cc8* >(int idx) + { + return luaL_checkstring(mState, idx); + } + + template <> + double LuaxState::CheckParam < double >(int idx) + { + return luaL_checknumber(mState, idx); + } + + template <> + float LuaxState::CheckParam < float >(int idx) + { + return luaL_checknumber(mState, idx); + } + + template <> + s8 LuaxState::CheckParam < s8 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + s16 LuaxState::CheckParam < s16 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + s32 LuaxState::CheckParam < s32 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + s64 LuaxState::CheckParam < s64 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + u8 LuaxState::CheckParam < u8 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + u16 LuaxState::CheckParam < u16 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + u32 LuaxState::CheckParam < u32 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + u64 LuaxState::CheckParam < u64 >(int idx) + { + return luaL_checkinteger(mState, idx); + } + + template <> + std::string LuaxState::CheckParam < std::string >(int idx) + { + return luaL_checkstring(mState, idx); + } + + /// + /// check light userdata + /// + template <> + const void* LuaxState::CheckParam < const void* >(int idx) + { + if (IsType(idx, LUA_TLIGHTUSERDATA)) + { + return GetValue(idx, nullptr); + } + else + { + luaL_typerror(mState, idx, "light userdata"); + return nullptr; + } + } + } \ No newline at end of file diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h index fbe424b..564eba3 100644 --- a/source/3rd-party/Luax/luax_state.h +++ b/source/3rd-party/Luax/luax_state.h @@ -107,13 +107,17 @@ namespace Luax /// /// 以void** 的形式创建userdata,并将值设置为ptr /// - void PushPtrUserData(void* ptr); + void PushPtrUserdata(void* ptr); void Pop(int n = 1); void Settop(int idx); - template T* GetLuaUserdata(int idx = 1); + template T* GetUserdata(int idx = 1); + + //------------------------------------------------------------------------------------------------------------ + + int ErrorType(int idx, cc8* hint); //------------------------------------------------------------------------------------------------------------ @@ -123,6 +127,9 @@ namespace Luax template void SetField(int idx, cc8* key, T value); template void SetFieldByIndex(int idx, int key, T value); + template T* CheckUserdata(int idx); + template T CheckParam(int idx); + //------------------------------------------------------------------------------------------------------------ void DoString(const std::string& code); @@ -175,7 +182,7 @@ namespace Luax void RegisterPlainClassRegistry(cc8* name); #endif -#if LUAX_ENABLE_PLAIN_ENABLE +#if LUAX_ENABLE_PLAIN_ENUM /// /// 注册纯lua的枚举,以防止修改枚举值。 /// @@ -203,7 +210,7 @@ namespace Luax }; //-------------------------------------------------------------------------------------------------------------- - // GetValue()模板特化 + // GetValue()模板实例化 template <> bool LuaxState::GetValue < bool >(int idx, const bool value); template <> cc8* LuaxState::GetValue < cc8* >(int idx, const cc8* value); @@ -219,8 +226,24 @@ namespace Luax template <> u64 LuaxState::GetValue < u64 >(int idx, const u64 value); template <> std::string LuaxState::GetValue < std::string >(int idx, const std::string value); template <> const void* LuaxState::GetValue < const void* >(int idx, const void* value); - template <> int LuaxState::GetValue < int >(int idx, int value); + //-------------------------------------------------------------------------------------------------------------- + // CheckParam模板实例化 + + template <> bool LuaxState::CheckParam < bool >(int idx); + template <> cc8* LuaxState::CheckParam < cc8* >(int idx); + template <> double LuaxState::CheckParam < double >(int idx); + template <> float LuaxState::CheckParam < float >(int idx); + template <> s8 LuaxState::CheckParam < s8 >(int idx); + template <> s16 LuaxState::CheckParam < s16 >(int idx); + template <> s32 LuaxState::CheckParam < s32 >(int idx); + template <> s64 LuaxState::CheckParam < s64 >(int idx); + template <> u8 LuaxState::CheckParam < u8 >(int idx); + template <> u16 LuaxState::CheckParam < u16 >(int idx); + template <> u32 LuaxState::CheckParam < u32 >(int idx); + template <> u64 LuaxState::CheckParam < u64 >(int idx); + template <> std::string LuaxState::CheckParam < std::string >(int idx); + template <> const void* LuaxState::CheckParam < const void* >(int idx); /// /// 在成员方法里创建LuaxState并对参数进行检查。 diff --git a/source/3rd-party/Luax/luax_state.inl b/source/3rd-party/Luax/luax_state.inl index 06d9350..4af45a2 100644 --- a/source/3rd-party/Luax/luax_state.inl +++ b/source/3rd-party/Luax/luax_state.inl @@ -125,7 +125,7 @@ namespace Luax } template - T* LuaxState::GetLuaUserdata(int idx) + T* LuaxState::GetUserdata(int idx) { void* p = nullptr; @@ -137,4 +137,33 @@ namespace Luax return static_cast(p); } + template + T* LuaxState::CheckUserdata(int idx) + { + if (IsType(idx, LUA_TUSERDATA)) + { + if (lua_getmetatable(mState, idx)) // ref table + { + if (lua_getmetatable(mState, -1)) // member table + { + if (lua_getmetatable(mState, -1)) // class table + { + T::PushLuaxClassTable(*this); // target class table + if (lua_rawequal(mState, -1, -2)) + { + Pop(4); // ref\member\class\target class + T* udata = GetUserdata(idx); + return udata; // userdata + } + Pop(2); // target class table\class table + } + Pop(1); // member table + } + Pop(1); // ref table + } + } + luaL_typerror(mState, idx, T::GetLuaxClassName()); + return nullptr; + } + } \ No newline at end of file -- cgit v1.1-26-g67d0