2020-08-10 05:17:06 +08:00
|
|
|
// sol3
|
2018-03-06 11:54:31 +08:00
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2020-08-13 22:42:37 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2018-03-06 11:54:31 +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_STATE_DEFAULT_HPP
|
|
|
|
#define SOL_STATE_DEFAULT_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/trampoline.hpp>
|
|
|
|
#include <sol/stack.hpp>
|
|
|
|
#include <sol/error.hpp>
|
|
|
|
#include <sol/function.hpp>
|
|
|
|
#include <sol/object.hpp>
|
|
|
|
#include <sol/lua_value.hpp>
|
2018-03-06 11:54:31 +08:00
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
|
2018-03-10 11:27:49 +08:00
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
2018-03-06 11:54:31 +08:00
|
|
|
namespace sol {
|
|
|
|
inline void register_main_thread(lua_State* L) {
|
|
|
|
#if SOL_LUA_VERSION < 502
|
|
|
|
if (L == nullptr) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_setglobal(L, detail::default_main_thread_name());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lua_pushthread(L);
|
|
|
|
lua_setglobal(L, detail::default_main_thread_name());
|
|
|
|
#else
|
|
|
|
(void)L;
|
|
|
|
#endif
|
2018-04-18 08:08:07 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 11:54:31 +08:00
|
|
|
inline int default_at_panic(lua_State* L) {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
|
2018-03-06 11:54:31 +08:00
|
|
|
(void)L;
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
size_t messagesize;
|
|
|
|
const char* message = lua_tolstring(L, -1, &messagesize);
|
|
|
|
if (message) {
|
|
|
|
std::string err(message, messagesize);
|
|
|
|
lua_settop(L, 0);
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
|
2019-05-22 07:17:31 +08:00
|
|
|
std::cerr << "[sol3] An error occurred and panic has been invoked: ";
|
2018-03-10 11:27:49 +08:00
|
|
|
std::cerr << err;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2018-03-06 11:54:31 +08:00
|
|
|
throw error(err);
|
|
|
|
}
|
|
|
|
lua_settop(L, 0);
|
2018-04-18 00:29:03 +08:00
|
|
|
throw error(std::string("An unexpected error occurred and panic has been invoked"));
|
|
|
|
#endif // Printing Errors
|
2018-03-06 11:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int default_traceback_error_handler(lua_State* L) {
|
|
|
|
std::string msg = "An unknown error has triggered the default error handler";
|
2019-03-10 09:57:49 +08:00
|
|
|
optional<string_view> maybetopmsg = stack::unqualified_check_get<string_view>(L, 1, no_panic);
|
2018-03-06 11:54:31 +08:00
|
|
|
if (maybetopmsg) {
|
|
|
|
const string_view& topmsg = maybetopmsg.value();
|
|
|
|
msg.assign(topmsg.data(), topmsg.size());
|
|
|
|
}
|
|
|
|
luaL_traceback(L, L, msg.c_str(), 1);
|
2019-03-10 09:57:49 +08:00
|
|
|
optional<string_view> maybetraceback = stack::unqualified_check_get<string_view>(L, -1, no_panic);
|
2018-03-06 11:54:31 +08:00
|
|
|
if (maybetraceback) {
|
2018-03-10 11:27:49 +08:00
|
|
|
const string_view& traceback = maybetraceback.value();
|
2018-03-06 11:54:31 +08:00
|
|
|
msg.assign(traceback.data(), traceback.size());
|
|
|
|
}
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
|
|
|
|
// std::cerr << "[sol3] An error occurred and was caught in traceback: ";
|
|
|
|
// std::cerr << msg;
|
|
|
|
// std::cerr << std::endl;
|
2018-04-18 00:29:03 +08:00
|
|
|
#endif // Printing
|
2018-03-06 11:54:31 +08:00
|
|
|
return stack::push(L, msg);
|
2018-04-18 08:08:07 +08:00
|
|
|
}
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
inline void set_default_state(lua_State* L, lua_CFunction panic_function = &default_at_panic,
|
|
|
|
lua_CFunction traceback_function = c_call<decltype(&default_traceback_error_handler), &default_traceback_error_handler>,
|
|
|
|
exception_handler_function exf = detail::default_exception_handler) {
|
2018-03-06 11:54:31 +08:00
|
|
|
lua_atpanic(L, panic_function);
|
|
|
|
protected_function::set_default_handler(object(L, in_place, traceback_function));
|
2018-03-10 11:27:49 +08:00
|
|
|
set_default_exception_handler(L, exf);
|
2018-03-06 11:54:31 +08:00
|
|
|
register_main_thread(L);
|
2018-04-18 08:08:07 +08:00
|
|
|
stack::luajit_exception_handler(L);
|
2019-03-18 19:41:51 +08:00
|
|
|
lua_value::set_lua_state(L);
|
2018-03-06 11:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::size_t total_memory_used(lua_State* L) {
|
|
|
|
std::size_t kb = lua_gc(L, LUA_GCCOUNT, 0);
|
|
|
|
kb *= 1024;
|
|
|
|
kb += lua_gc(L, LUA_GCCOUNTB, 0);
|
|
|
|
return kb;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline protected_function_result script_pass_on_error(lua_State*, protected_function_result result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
inline protected_function_result script_throw_on_error(lua_State* L, protected_function_result result) {
|
2018-03-06 11:54:31 +08:00
|
|
|
type t = type_of(L, result.stack_index());
|
|
|
|
std::string err = "sol: ";
|
|
|
|
err += to_string(result.status());
|
|
|
|
err += " error";
|
2018-04-18 00:29:03 +08:00
|
|
|
#if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS)
|
2018-03-06 11:54:31 +08:00
|
|
|
std::exception_ptr eptr = std::current_exception();
|
|
|
|
if (eptr) {
|
|
|
|
err += " with a ";
|
|
|
|
try {
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex) {
|
|
|
|
err += "std::exception -- ";
|
|
|
|
err.append(ex.what());
|
|
|
|
}
|
|
|
|
catch (const std::string& message) {
|
|
|
|
err += "thrown message -- ";
|
|
|
|
err.append(message);
|
|
|
|
}
|
|
|
|
catch (const char* message) {
|
|
|
|
err += "thrown message -- ";
|
|
|
|
err.append(message);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
err.append("thrown but unknown type, cannot serialize into error message");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // serialize exception information if possible
|
|
|
|
if (t == type::string) {
|
|
|
|
err += ": ";
|
2019-03-10 09:57:49 +08:00
|
|
|
string_view serr = stack::unqualified_get<string_view>(L, result.stack_index());
|
2018-03-06 11:54:31 +08:00
|
|
|
err.append(serr.data(), serr.size());
|
|
|
|
}
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
|
2019-05-22 07:17:31 +08:00
|
|
|
std::cerr << "[sol3] An error occurred and has been passed to an error handler: ";
|
2018-03-10 11:27:49 +08:00
|
|
|
std::cerr << err;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2018-03-06 11:54:31 +08:00
|
|
|
// replacing information of stack error into pfr
|
|
|
|
int target = result.stack_index();
|
|
|
|
if (result.pop_count() > 0) {
|
|
|
|
stack::remove(L, target, result.pop_count());
|
|
|
|
}
|
2018-04-18 01:04:35 +08:00
|
|
|
stack::push(L, err);
|
2018-03-06 11:54:31 +08:00
|
|
|
int top = lua_gettop(L);
|
|
|
|
int towards = top - target;
|
|
|
|
if (towards != 0) {
|
|
|
|
lua_rotate(L, top, towards);
|
|
|
|
}
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
|
2018-04-18 08:08:07 +08:00
|
|
|
return result;
|
2018-03-06 11:54:31 +08:00
|
|
|
#else
|
|
|
|
// just throw our error
|
|
|
|
throw error(detail::direct_error, err);
|
2018-04-18 08:08:07 +08:00
|
|
|
#endif // If exceptions are allowed
|
2018-03-06 11:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline protected_function_result script_default_on_error(lua_State* L, protected_function_result pfr) {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_DEFAULT_PASS_ON_ERROR_I_)
|
2018-03-06 11:54:31 +08:00
|
|
|
return script_pass_on_error(L, std::move(pfr));
|
|
|
|
#else
|
|
|
|
return script_throw_on_error(L, std::move(pfr));
|
|
|
|
#endif
|
|
|
|
}
|
2019-05-27 03:44:38 +08:00
|
|
|
|
|
|
|
namespace stack {
|
|
|
|
inline error get_traceback_or_errors(lua_State* L) {
|
|
|
|
int p = default_traceback_error_handler(L);
|
|
|
|
sol::error err = stack::get<sol::error>(L, -p);
|
2019-05-27 13:49:31 +08:00
|
|
|
lua_pop(L, p);
|
|
|
|
return err;
|
2019-05-27 03:44:38 +08:00
|
|
|
}
|
2020-08-10 05:17:06 +08:00
|
|
|
} // namespace stack
|
2018-03-06 11:54:31 +08:00
|
|
|
} // namespace sol
|
|
|
|
|
|
|
|
#endif // SOL_STATE_DEFAULT_HPP
|