Skip to content

Commit

Permalink
Fix format specifier for size_t.
Browse files Browse the repository at this point in the history
- Fix format specifier for sizeof result.
- Rename base.hpp to object.hpp
- Do not try to convert raw pointer to shared_ptr
  • Loading branch information
davits committed Sep 26, 2023
1 parent 52ab37c commit 4620519
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion include/luabind/bind.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion include/luabind/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 18 additions & 19 deletions include/luabind/mirror.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T*>(ud->object);
return *t;
return *(value_mirror<T*>::from_lua(L, idx));
}
};

Expand All @@ -40,24 +38,20 @@ struct value_mirror<T*> {
using raw_type = std::remove_cv_t<T>;

static int to_lua(lua_State* L, T* v) {
if constexpr (std::is_base_of_v<std::enable_shared_from_this<raw_type>, raw_type>) {
try {
std::shared_ptr<raw_type> shared = v->shared_from_this();
return value_mirror<std::shared_ptr<raw_type>>::to_lua(L, std::move(shared));
} catch (const std::bad_weak_ptr&) {
}
}
cpp_user_data<T>::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<T>(L).data(),
lua_typename(L, lua_type(L, idx)));
}
auto p = dynamic_cast<T*>(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<T>(L).data(),
Expand Down Expand Up @@ -87,13 +81,19 @@ struct value_mirror<std::shared_ptr<T>> {
}

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<T>(L).data(),
lua_typename(L, lua_type(L, idx)));
}
auto sud = dynamic_cast<shared_user_data*>(ud);
if (sud == nullptr) {
if (sud == nullptr) [[unlikely]] {
reportError("Argument %i does not represent shared_ptr.", idx);
}
auto r = std::dynamic_pointer_cast<T>(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<T>(L).data(),
Expand Down Expand Up @@ -123,7 +123,7 @@ struct value_mirror<bool> {

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);
Expand Down Expand Up @@ -154,7 +154,6 @@ struct number_mirror {
return static_cast<raw_type>(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<raw_type>(lua_tonumber(L, idx));
Expand Down
6 changes: 3 additions & 3 deletions include/luabind/base.hpp → include/luabind/object.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef LUABIND_BASE_HPP
#define LUABIND_BASE_HPP
#ifndef LUABIND_OBJECT_HPP
#define LUABIND_OBJECT_HPP

namespace luabind {

Expand All @@ -12,4 +12,4 @@ inline Object::~Object() = default;

} // namespace luabind

#endif // LUABIND_BASE_HPP
#endif // LUABIND_OBJECT_HPP
2 changes: 1 addition & 1 deletion include/luabind/user_data.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LUABIND_USER_DATA
#define LUABIND_USER_DATA

#include "base.hpp"
#include "object.hpp"
#include "type_storage.hpp"

#include <memory>
Expand Down
12 changes: 6 additions & 6 deletions include/luabind/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>::to_lua(L, value_mirror<Args>::from_lua(L, Indices)...);
}
Expand All @@ -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<Type>(value_mirror<Args>::from_lua(L, Indices)...));
}
Expand All @@ -90,7 +90,7 @@ struct function_wrapper<R (T::*)(Args...), func> {
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<T*>::from_lua(L, 1);
if constexpr (std::is_same_v<R, void>) {
Expand All @@ -113,7 +113,7 @@ struct function_wrapper<R (T::*)(Args...) const, func> {
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<const T*>::from_lua(L, 1);
if constexpr (std::is_same_v<R, void>) {
Expand All @@ -135,7 +135,7 @@ struct function_wrapper<R (*)(Args...), func> {
static int indexed_call_helper(lua_State* L, std::index_sequence<Indices...>) {
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<R, void>) {
(*func)(value_mirror<Args>::from_lua(L, Indices)...);
Expand All @@ -162,7 +162,7 @@ struct class_function_wrapper<R (*)(Args...), func> {
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<R, void>) {
(*func)(value_mirror<Args>::from_lua(L, Indices)...);
Expand Down

0 comments on commit 4620519

Please sign in to comment.