From 462051975bc291bbdda9dba0f7769b0cb0ae14cc Mon Sep 17 00:00:00 2001 From: Davit Samvelyan Date: Tue, 26 Sep 2023 10:01:17 +0200 Subject: [PATCH] Fix format specifier for size_t. - Fix format specifier for sizeof result. - Rename base.hpp to object.hpp - Do not try to convert raw pointer to shared_ptr --- include/luabind/bind.hpp | 2 +- include/luabind/exception.hpp | 2 +- include/luabind/mirror.hpp | 37 ++++++++++++------------ include/luabind/{base.hpp => object.hpp} | 6 ++-- include/luabind/user_data.hpp | 2 +- include/luabind/wrapper.hpp | 12 ++++---- 6 files changed, 30 insertions(+), 31 deletions(-) rename include/luabind/{base.hpp => object.hpp} (62%) diff --git a/include/luabind/bind.hpp b/include/luabind/bind.hpp index a219f0c..5207cb6 100644 --- a/include/luabind/bind.hpp +++ b/include/luabind/bind.hpp @@ -1,7 +1,7 @@ #ifndef LUABIND_BIND_HPP #define LUABIND_BIND_HPP -#include "base.hpp" +#include "object.hpp" #include "exception.hpp" #include "mirror.hpp" #include "type_storage.hpp" diff --git a/include/luabind/exception.hpp b/include/luabind/exception.hpp index dba7a2c..bb32d85 100644 --- a/include/luabind/exception.hpp +++ b/include/luabind/exception.hpp @@ -26,7 +26,7 @@ class error : public std::exception { [[gnu::format(printf, 1, 2)]] inline void reportError(const char* fmt, ...) { std::va_list args; va_start(args, fmt); - constexpr size_t bufferSize = 1024; + constexpr size_t bufferSize = 256; char buffer[bufferSize]; std::vsnprintf(buffer, bufferSize, fmt, args); va_end(args); diff --git a/include/luabind/mirror.hpp b/include/luabind/mirror.hpp index 7883c73..490899c 100644 --- a/include/luabind/mirror.hpp +++ b/include/luabind/mirror.hpp @@ -28,9 +28,7 @@ struct value_mirror { } static const T& from_lua(lua_State* L, int idx) { - auto ud = user_data::from_lua(L, idx); - T* t = dynamic_cast(ud->object); - return *t; + return *(value_mirror::from_lua(L, idx)); } }; @@ -40,24 +38,20 @@ struct value_mirror { using raw_type = std::remove_cv_t; static int to_lua(lua_State* L, T* v) { - if constexpr (std::is_base_of_v, raw_type>) { - try { - std::shared_ptr shared = v->shared_from_this(); - return value_mirror>::to_lua(L, std::move(shared)); - } catch (const std::bad_weak_ptr&) { - } - } cpp_user_data::to_lua(L, v); return 1; } static T* from_lua(lua_State* L, int idx) { - auto ud = user_data::from_lua(L, idx); - if (ud == nullptr || ud->object == nullptr) { - return nullptr; + auto* ud = user_data::from_lua(L, idx); + if (ud == nullptr) [[unlikely]] { + reportError("Argument at %i has invalid type. Need user_data of type '%s', but got lua type '%s'", + idx, + type_storage::type_name(L).data(), + lua_typename(L, lua_type(L, idx))); } auto p = dynamic_cast(ud->object); - if (p == nullptr) { + if (p == nullptr && ud->object != nullptr) [[unlikely]] { reportError("Argument at %i has invalid type. Need '%s' but got '%s'.", idx, type_storage::type_name(L).data(), @@ -87,13 +81,19 @@ struct value_mirror> { } static type from_lua(lua_State* L, int idx) { - auto ud = user_data::from_lua(L, idx); + auto* ud = user_data::from_lua(L, idx); + if (ud == nullptr) [[unlikely]] { + reportError("Argument at %i has invalid type. Need user_data of type '%s', but got lua type '%s'", + idx, + type_storage::type_name(L).data(), + lua_typename(L, lua_type(L, idx))); + } auto sud = dynamic_cast(ud); - if (sud == nullptr) { + if (sud == nullptr) [[unlikely]] { reportError("Argument %i does not represent shared_ptr.", idx); } auto r = std::dynamic_pointer_cast(sud->data); - if (!r) { + if (!r && sud->data) [[unlikely]] { reportError("Argument at %i has invalid type. Need '%s' but got '%s'.", idx, type_storage::type_name(L).data(), @@ -123,7 +123,7 @@ struct value_mirror { static bool from_lua(lua_State* L, int idx) { int isb = lua_isboolean(L, idx); - if (isb != 1) { + if (isb != 1) [[unlikely]] { reportError("Provided argument at %i is not a boolean.", idx); } int r = lua_toboolean(L, idx); @@ -154,7 +154,6 @@ struct number_mirror { return static_cast(lua_tointeger(L, idx)); } else { if (lua_type(L, idx) != LUA_TNUMBER) { - // lua_error does longjmp, think about memory leak. reportError("Provided argument at %i is not a number.", idx); } return static_cast(lua_tonumber(L, idx)); diff --git a/include/luabind/base.hpp b/include/luabind/object.hpp similarity index 62% rename from include/luabind/base.hpp rename to include/luabind/object.hpp index 099128c..be05b3d 100644 --- a/include/luabind/base.hpp +++ b/include/luabind/object.hpp @@ -1,5 +1,5 @@ -#ifndef LUABIND_BASE_HPP -#define LUABIND_BASE_HPP +#ifndef LUABIND_OBJECT_HPP +#define LUABIND_OBJECT_HPP namespace luabind { @@ -12,4 +12,4 @@ inline Object::~Object() = default; } // namespace luabind -#endif // LUABIND_BASE_HPP +#endif // LUABIND_OBJECT_HPP diff --git a/include/luabind/user_data.hpp b/include/luabind/user_data.hpp index 1c7addb..db1d2fa 100644 --- a/include/luabind/user_data.hpp +++ b/include/luabind/user_data.hpp @@ -1,7 +1,7 @@ #ifndef LUABIND_USER_DATA #define LUABIND_USER_DATA -#include "base.hpp" +#include "object.hpp" #include "type_storage.hpp" #include diff --git a/include/luabind/wrapper.hpp b/include/luabind/wrapper.hpp index 1be9c3a..cdcf221 100644 --- a/include/luabind/wrapper.hpp +++ b/include/luabind/wrapper.hpp @@ -47,7 +47,7 @@ struct ctor_wrapper { // +1 first argument is the Type metatable if (num_args != sizeof...(Args) + 1) { reportError( - "Invalid number of arguments, should be %lu, but %i were given.", sizeof...(Args), num_args - 1); + "Invalid number of arguments, should be %zu, but %i were given.", sizeof...(Args), num_args - 1); } return lua_user_data::to_lua(L, value_mirror::from_lua(L, Indices)...); } @@ -68,7 +68,7 @@ struct shared_ctor_wrapper { // +1 first argument is the Type metatable if (num_args != sizeof...(Args) + 1) { reportError( - "Invalid number of arguments, should be %lu, but %i were given.", sizeof...(Args), num_args - 1); + "Invalid number of arguments, should be %zu, but %i were given.", sizeof...(Args), num_args - 1); } return shared_user_data::to_lua(L, std::make_shared(value_mirror::from_lua(L, Indices)...)); } @@ -90,7 +90,7 @@ struct function_wrapper { int num_args = lua_gettop(L); if (num_args != sizeof...(Args) + 1) { reportError( - "Invalid number of arguments, should be %lu, but %i were given.", sizeof...(Args), num_args - 1); + "Invalid number of arguments, should be %zu, but %i were given.", sizeof...(Args), num_args - 1); } T* self = value_mirror::from_lua(L, 1); if constexpr (std::is_same_v) { @@ -113,7 +113,7 @@ struct function_wrapper { int num_args = lua_gettop(L); if (num_args != sizeof...(Args) + 1) { reportError( - "Invalid number of arguments, should be %lu, but %i were given.", sizeof...(Args), num_args - 1); + "Invalid number of arguments, should be %zu, but %i were given.", sizeof...(Args), num_args - 1); } const T* self = value_mirror::from_lua(L, 1); if constexpr (std::is_same_v) { @@ -135,7 +135,7 @@ struct function_wrapper { static int indexed_call_helper(lua_State* L, std::index_sequence) { int num_args = lua_gettop(L); if (num_args != sizeof...(Args)) { - reportError("Invalid number of arguments, should be %lu, but %i were given.", sizeof...(Args), num_args); + reportError("Invalid number of arguments, should be %zu, but %i were given.", sizeof...(Args), num_args); } if constexpr (std::is_same_v) { (*func)(value_mirror::from_lua(L, Indices)...); @@ -162,7 +162,7 @@ struct class_function_wrapper { int num_args = lua_gettop(L); if (num_args != sizeof...(Args) + 1) { reportError( - "Invalid number of arguments, should be %lu, but %i were given.", sizeof...(Args), num_args - 1); + "Invalid number of arguments, should be %zu, but %i were given.", sizeof...(Args), num_args - 1); } if constexpr (std::is_same_v) { (*func)(value_mirror::from_lua(L, Indices)...);