2019-11-09 12:53:48 +08:00
|
|
|
// sol3
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2017-09-01 08:47:09 +08:00
|
|
|
|
2019-03-13 17:18:06 +08:00
|
|
|
// Copyright (c) 2013-2019 Rapptz, ThePhD and contributors
|
2017-09-01 08:47:09 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_ERROR_HANDLER_HPP
|
|
|
|
#define SOL_ERROR_HANDLER_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/types.hpp>
|
|
|
|
#include <sol/demangle.hpp>
|
2017-09-01 08:47:09 +08:00
|
|
|
|
2019-11-09 12:53:48 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
2017-09-01 08:47:09 +08:00
|
|
|
namespace sol {
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
namespace detail {
|
2019-07-05 04:34:32 +08:00
|
|
|
constexpr const char* not_a_number = "not a numeric type";
|
|
|
|
constexpr const char* not_a_number_or_number_string = "not a numeric type or numeric string";
|
2020-05-07 10:17:02 +08:00
|
|
|
constexpr const char* not_a_number_integral = "not a numeric type that fits exactly an integer (number maybe has significant decimals)";
|
2019-07-05 04:34:32 +08:00
|
|
|
constexpr const char* not_a_number_or_number_string_integral
|
2020-05-07 10:17:02 +08:00
|
|
|
= "not a numeric type or a numeric string that fits exactly an integer (e.g. number maybe has significant decimals)";
|
2019-07-05 04:34:32 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
constexpr const char* not_enough_stack_space = "not enough space left on Lua stack";
|
|
|
|
constexpr const char* not_enough_stack_space_floating = "not enough space left on Lua stack for a floating point number";
|
|
|
|
constexpr const char* not_enough_stack_space_integral = "not enough space left on Lua stack for an integral number";
|
|
|
|
constexpr const char* not_enough_stack_space_string = "not enough space left on Lua stack for a string";
|
|
|
|
constexpr const char* not_enough_stack_space_meta_function_name = "not enough space left on Lua stack for the name of a meta_function";
|
2019-05-22 07:17:31 +08:00
|
|
|
constexpr const char* not_enough_stack_space_userdata = "not enough space left on Lua stack to create a sol3 userdata";
|
2018-12-20 12:17:15 +08:00
|
|
|
constexpr const char* not_enough_stack_space_generic = "not enough space left on Lua stack to push valuees";
|
|
|
|
constexpr const char* not_enough_stack_space_environment = "not enough space left on Lua stack to retrieve environment";
|
2019-01-14 10:46:53 +08:00
|
|
|
constexpr const char* protected_function_error = "caught (...) unknown error during protected_function call";
|
|
|
|
|
2019-11-09 12:53:48 +08:00
|
|
|
inline void accumulate_and_mark(const std::string& n, std::string& aux_message, int& marker) {
|
2019-01-14 10:46:53 +08:00
|
|
|
if (marker > 0) {
|
2019-11-09 12:53:48 +08:00
|
|
|
aux_message += ", ";
|
2019-01-14 10:46:53 +08:00
|
|
|
}
|
2019-11-09 12:53:48 +08:00
|
|
|
aux_message += n;
|
2019-01-14 10:46:53 +08:00
|
|
|
++marker;
|
|
|
|
}
|
2019-11-09 12:53:48 +08:00
|
|
|
} // namespace detail
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2017-10-27 00:53:52 +08:00
|
|
|
inline std::string associated_type_name(lua_State* L, int index, type t) {
|
|
|
|
switch (t) {
|
|
|
|
case type::poly:
|
|
|
|
return "anything";
|
2019-11-09 12:53:48 +08:00
|
|
|
case type::userdata: {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
luaL_checkstack(L, 2, "not enough space to push get the type name");
|
|
|
|
#endif // make sure stack doesn't overflow
|
2017-10-27 00:53:52 +08:00
|
|
|
if (lua_getmetatable(L, index) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lua_pushlstring(L, "__name", 6);
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
size_t sz;
|
|
|
|
const char* name = lua_tolstring(L, -1, &sz);
|
|
|
|
std::string tn(name, static_cast<std::string::size_type>(sz));
|
|
|
|
lua_pop(L, 2);
|
2019-03-23 04:28:17 +08:00
|
|
|
return tn;
|
2017-10-27 00:53:52 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return lua_typename(L, static_cast<int>(t));
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:53:48 +08:00
|
|
|
inline int push_type_panic_string(lua_State* L, int index, type expected, type actual, string_view message, string_view aux_message) noexcept {
|
|
|
|
const char* err = message.size() == 0
|
|
|
|
? (aux_message.size() == 0 ? "stack index %d, expected %s, received %s" : "stack index %d, expected %s, received %s: %s")
|
|
|
|
: "stack index %d, expected %s, received %s: %s %s";
|
|
|
|
const char* type_name = expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(expected));
|
|
|
|
{
|
|
|
|
std::string actual_name = associated_type_name(L, index, actual);
|
|
|
|
lua_pushfstring(L, err, index, type_name, actual_name.c_str(), message.data(), aux_message.data());
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int type_panic_string(lua_State* L, int index, type expected, type actual, string_view message = "") noexcept(false) {
|
|
|
|
push_type_panic_string(L, index, expected, actual, message, "");
|
|
|
|
return lua_error(L);
|
2017-09-01 08:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int type_panic_c_str(lua_State* L, int index, type expected, type actual, const char* message = nullptr) noexcept(false) {
|
2019-11-09 12:53:48 +08:00
|
|
|
push_type_panic_string(L, index, expected, actual, message == nullptr ? "" : message, "");
|
|
|
|
return lua_error(L);
|
2017-09-01 08:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct type_panic_t {
|
|
|
|
int operator()(lua_State* L, int index, type expected, type actual) const noexcept(false) {
|
|
|
|
return type_panic_c_str(L, index, expected, actual, nullptr);
|
|
|
|
}
|
2019-11-09 12:53:48 +08:00
|
|
|
int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
|
|
|
|
return type_panic_c_str(L, index, expected, actual, message.data());
|
2017-09-01 08:47:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const type_panic_t type_panic = {};
|
|
|
|
|
|
|
|
struct constructor_handler {
|
2019-11-09 12:53:48 +08:00
|
|
|
int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
|
|
|
|
push_type_panic_string(L, index, expected, actual, message, "(type check failed in constructor)");
|
|
|
|
return lua_error(L);
|
2017-09-01 08:47:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-06 01:58:17 +08:00
|
|
|
template <typename F = void>
|
2017-09-01 08:47:09 +08:00
|
|
|
struct argument_handler {
|
2019-11-09 12:53:48 +08:00
|
|
|
int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
|
|
|
|
push_type_panic_string(L, index, expected, actual, message, "(bad argument to variable or function call)");
|
|
|
|
return lua_error(L);
|
2017-09-01 08:47:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-06 01:58:17 +08:00
|
|
|
template <typename R, typename... Args>
|
|
|
|
struct argument_handler<types<R, Args...>> {
|
2019-11-09 12:53:48 +08:00
|
|
|
int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
|
|
|
|
{
|
|
|
|
std::string aux_message = "(bad argument into '";
|
|
|
|
aux_message += detail::demangle<R>();
|
|
|
|
aux_message += "(";
|
|
|
|
int marker = 0;
|
2020-05-07 10:17:02 +08:00
|
|
|
(void)detail::swallow { int(), (detail::accumulate_and_mark(detail::demangle<Args>(), aux_message, marker), int())... };
|
2019-11-09 12:53:48 +08:00
|
|
|
aux_message += ")')";
|
|
|
|
push_type_panic_string(L, index, expected, actual, message, aux_message);
|
|
|
|
}
|
|
|
|
return lua_error(L);
|
2017-09-06 01:58:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-01 08:47:09 +08:00
|
|
|
// Specify this function as the handler for lua::check if you know there's nothing wrong
|
|
|
|
inline int no_panic(lua_State*, int, type, type, const char* = nullptr) noexcept {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void type_error(lua_State* L, int expected, int actual) noexcept(false) {
|
|
|
|
luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void type_error(lua_State* L, type expected, type actual) noexcept(false) {
|
|
|
|
type_error(L, static_cast<int>(expected), static_cast<int>(actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void type_assert(lua_State* L, int index, type expected, type actual) noexcept(false) {
|
|
|
|
if (expected != type::poly && expected != actual) {
|
|
|
|
type_panic_c_str(L, index, expected, actual, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void type_assert(lua_State* L, int index, type expected) {
|
|
|
|
type actual = type_of(L, index);
|
|
|
|
type_assert(L, index, expected, actual);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2017-09-01 08:47:09 +08:00
|
|
|
|
|
|
|
#endif // SOL_ERROR_HANDLER_HPP
|