2021-03-06 23:14:48 +08:00
|
|
|
// sol2
|
2018-03-10 11:27:49 +08:00
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
// Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
|
2018-03-10 11:27:49 +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_TRAMPOLINE_HPP
|
|
|
|
#define SOL_TRAMPOLINE_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/types.hpp>
|
|
|
|
#include <sol/traits.hpp>
|
2018-03-10 11:27:49 +08:00
|
|
|
#include <exception>
|
|
|
|
#include <cstring>
|
|
|
|
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_PRINT_ERRORS)
|
2018-03-10 11:27:49 +08:00
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
// must push a single object to be the error object
|
|
|
|
// NOTE: the VAST MAJORITY of all Lua libraries -- C or otherwise -- expect a string for the type of error
|
|
|
|
// break this convention at your own risk
|
2020-09-26 18:47:20 +08:00
|
|
|
using exception_handler_function = int (*)(lua_State*, optional<const std::exception&>, string_view);
|
2018-03-10 11:27:49 +08:00
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
namespace detail {
|
2020-09-26 18:47:20 +08:00
|
|
|
inline const char (&default_exception_handler_name())[11] {
|
2018-03-10 11:27:49 +08:00
|
|
|
static const char name[11] = "sol.\xE2\x98\xA2\xE2\x98\xA2";
|
|
|
|
return name;
|
|
|
|
}
|
2018-09-28 13:27:38 +08:00
|
|
|
|
|
|
|
// must push at least 1 object on the stack
|
2018-03-10 11:27:49 +08:00
|
|
|
inline int default_exception_handler(lua_State* L, optional<const std::exception&>, string_view what) {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_PRINT_ERRORS)
|
2021-03-06 23:14:48 +08:00
|
|
|
std::cerr << "[sol2] An exception occurred: ";
|
2020-12-17 04:25:35 +08:00
|
|
|
std::cerr.write(what.data(), static_cast<std::streamsize>(what.size()));
|
2018-03-10 11:27:49 +08:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
lua_pushlstring(L, what.data(), what.size());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int call_exception_handler(lua_State* L, optional<const std::exception&> maybe_ex, string_view what) {
|
|
|
|
lua_getglobal(L, default_exception_handler_name());
|
|
|
|
type t = static_cast<type>(lua_type(L, -1));
|
|
|
|
if (t != type::lightuserdata) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return default_exception_handler(L, std::move(maybe_ex), std::move(what));
|
|
|
|
}
|
|
|
|
void* vfunc = lua_touserdata(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
if (vfunc == nullptr) {
|
|
|
|
return default_exception_handler(L, std::move(maybe_ex), std::move(what));
|
|
|
|
}
|
|
|
|
exception_handler_function exfunc = reinterpret_cast<exception_handler_function>(vfunc);
|
|
|
|
return exfunc(L, std::move(maybe_ex), std::move(what));
|
|
|
|
}
|
|
|
|
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_OFF(SOL_EXCEPTIONS)
|
2018-03-10 11:27:49 +08:00
|
|
|
template <lua_CFunction f>
|
|
|
|
int static_trampoline(lua_State* L) noexcept {
|
|
|
|
return f(L);
|
|
|
|
}
|
|
|
|
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
|
2018-03-10 11:27:49 +08:00
|
|
|
template <lua_CFunction_noexcept f>
|
|
|
|
int static_trampoline_noexcept(lua_State* L) noexcept {
|
|
|
|
return f(L);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
template <lua_CFunction f>
|
|
|
|
int static_trampoline_noexcept(lua_State* L) noexcept {
|
|
|
|
return f(L);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <typename Fx, typename... Args>
|
|
|
|
int trampoline(lua_State* L, Fx&& f, Args&&... args) noexcept {
|
|
|
|
return f(L, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int c_trampoline(lua_State* L, lua_CFunction f) noexcept {
|
|
|
|
return trampoline(L, f);
|
|
|
|
}
|
|
|
|
#else
|
2020-06-20 12:18:13 +08:00
|
|
|
|
2020-09-26 18:47:20 +08:00
|
|
|
inline int lua_cfunction_trampoline(lua_State* L, lua_CFunction f) {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_PROPAGATE_EXCEPTIONS)
|
2018-03-10 11:27:49 +08:00
|
|
|
return f(L);
|
|
|
|
#else
|
|
|
|
try {
|
|
|
|
return f(L);
|
|
|
|
}
|
|
|
|
catch (const char* cs) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
|
|
|
|
}
|
|
|
|
catch (const std::string& s) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
|
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(e), e.what());
|
|
|
|
}
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_EXCEPTIONS_CATCH_ALL)
|
2018-03-10 11:27:49 +08:00
|
|
|
// LuaJIT cannot have the catchall when the safe propagation is on
|
2020-06-20 12:18:13 +08:00
|
|
|
// but LuaJIT will swallow all C++ errors
|
2018-03-10 11:27:49 +08:00
|
|
|
// if we don't at least catch std::exception ones
|
|
|
|
catch (...) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
|
|
|
|
}
|
|
|
|
#endif // LuaJIT cannot have the catchall, but we must catch std::exceps for it
|
|
|
|
return lua_error(L);
|
|
|
|
#endif // Safe exceptions
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:18:13 +08:00
|
|
|
template <lua_CFunction f>
|
|
|
|
int static_trampoline(lua_State* L) {
|
2020-09-26 18:47:20 +08:00
|
|
|
return lua_cfunction_trampoline(L, f);
|
2020-06-20 12:18:13 +08:00
|
|
|
}
|
|
|
|
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
|
2018-03-10 11:27:49 +08:00
|
|
|
template <lua_CFunction_noexcept f>
|
|
|
|
int static_trampoline_noexcept(lua_State* L) noexcept {
|
|
|
|
return f(L);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
template <lua_CFunction f>
|
|
|
|
int static_trampoline_noexcept(lua_State* L) noexcept {
|
|
|
|
return f(L);
|
|
|
|
}
|
2020-09-26 18:47:20 +08:00
|
|
|
#endif
|
2018-03-10 11:27:49 +08:00
|
|
|
|
|
|
|
template <typename Fx, typename... Args>
|
|
|
|
int trampoline(lua_State* L, Fx&& f, Args&&... args) {
|
2020-09-26 18:47:20 +08:00
|
|
|
if constexpr (meta::bind_traits<meta::unqualified_t<Fx>>::is_noexcept) {
|
2018-03-10 11:27:49 +08:00
|
|
|
return f(L, std::forward<Args>(args)...);
|
|
|
|
}
|
2020-09-26 18:47:20 +08:00
|
|
|
else {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_PROPAGATE_EXCEPTIONS)
|
2018-03-10 11:27:49 +08:00
|
|
|
return f(L, std::forward<Args>(args)...);
|
2020-09-26 18:47:20 +08:00
|
|
|
#else
|
|
|
|
try {
|
|
|
|
return f(L, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
catch (const char* cs) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
|
|
|
|
}
|
|
|
|
catch (const std::string& s) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
|
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(e), e.what());
|
|
|
|
}
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_EXCEPTIONS_CATCH_ALL)
|
2020-09-26 18:47:20 +08:00
|
|
|
// LuaJIT cannot have the catchall when the safe propagation is on
|
|
|
|
// but LuaJIT will swallow all C++ errors
|
|
|
|
// if we don't at least catch std::exception ones
|
|
|
|
catch (...) {
|
|
|
|
call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
|
|
|
|
}
|
2018-03-10 11:27:49 +08:00
|
|
|
#endif
|
2020-09-26 18:47:20 +08:00
|
|
|
return lua_error(L);
|
2018-03-10 11:27:49 +08:00
|
|
|
#endif
|
2020-09-26 18:47:20 +08:00
|
|
|
}
|
2018-03-10 11:27:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int c_trampoline(lua_State* L, lua_CFunction f) {
|
|
|
|
return trampoline(L, f);
|
|
|
|
}
|
|
|
|
#endif // Exceptions vs. No Exceptions
|
|
|
|
|
|
|
|
template <typename F, F fx>
|
|
|
|
inline int typed_static_trampoline(lua_State* L) {
|
2021-01-04 18:52:30 +08:00
|
|
|
#if 0
|
|
|
|
// TODO: you must evaluate the get/check_get of every
|
|
|
|
// argument, to ensure it doesn't throw
|
|
|
|
// (e.g., for the sol_lua_check_access extension point!)
|
|
|
|
// This incluudes properly noexcept-ing all the above
|
|
|
|
// trampolines / safety nets
|
2020-09-26 18:47:20 +08:00
|
|
|
if constexpr (meta::bind_traits<F>::is_noexcept) {
|
|
|
|
return static_trampoline_noexcept<fx>(L);
|
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{ return static_trampoline<fx>(L); }
|
2018-03-10 11:27:49 +08:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
inline void set_default_exception_handler(lua_State* L, exception_handler_function exf = &detail::default_exception_handler) {
|
2020-09-26 18:47:20 +08:00
|
|
|
static_assert(sizeof(void*) >= sizeof(exception_handler_function),
|
|
|
|
"void* storage is too small to transport the exception handler: please file a bug on the sol2 issue tracker to get this looked at!");
|
2018-03-10 11:27:49 +08:00
|
|
|
void* storage;
|
|
|
|
std::memcpy(&storage, &exf, sizeof(exception_handler_function));
|
|
|
|
lua_pushlightuserdata(L, storage);
|
|
|
|
lua_setglobal(L, detail::default_exception_handler_name());
|
|
|
|
}
|
2020-09-26 18:47:20 +08:00
|
|
|
} // namespace sol
|
2018-03-10 11:27:49 +08:00
|
|
|
|
|
|
|
#endif // SOL_TRAMPOLINE_HPP
|