2016-03-14 21:53:24 +08:00
|
|
|
// The MIT License (MIT)
|
2016-03-03 10:02:12 +08:00
|
|
|
|
|
|
|
// Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
|
|
|
|
|
|
|
|
// 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_PROTECTED_FUNCTION_HPP
|
|
|
|
#define SOL_PROTECTED_FUNCTION_HPP
|
|
|
|
|
|
|
|
#include "reference.hpp"
|
|
|
|
#include "stack.hpp"
|
|
|
|
#include "protected_function_result.hpp"
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace sol {
|
2016-04-07 17:21:49 +08:00
|
|
|
template <typename base_t>
|
|
|
|
class basic_protected_function : public base_t {
|
2016-03-03 10:02:12 +08:00
|
|
|
private:
|
|
|
|
static reference& handler_storage() {
|
|
|
|
static sol::reference h;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static const reference& get_default_handler () {
|
|
|
|
return handler_storage();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_default_handler( reference& ref ) {
|
|
|
|
handler_storage() = ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct handler {
|
|
|
|
const reference& target;
|
|
|
|
int stackindex;
|
|
|
|
handler(const reference& target) : target(target), stackindex(0) {
|
|
|
|
if (target.valid()) {
|
|
|
|
stackindex = lua_gettop(target.lua_state()) + 1;
|
|
|
|
target.push();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~handler() {
|
|
|
|
if (stackindex > 0) {
|
|
|
|
lua_remove(target.lua_state(), stackindex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
return lua_pcallk(base_t::lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stackindex, 0, nullptr);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<std::size_t... I, typename... Ret>
|
|
|
|
auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n, handler& h) const {
|
|
|
|
luacall(n, sizeof...(Ret), h);
|
2016-04-07 17:21:49 +08:00
|
|
|
return stack::pop<std::tuple<Ret...>>(base_t::lua_state());
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<std::size_t I, typename Ret>
|
|
|
|
Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n, handler& h) const {
|
|
|
|
luacall(n, 1, h);
|
2016-04-07 17:21:49 +08:00
|
|
|
return stack::pop<Ret>(base_t::lua_state());
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t I>
|
|
|
|
void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n, handler& h) const {
|
|
|
|
luacall(n, 0, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n, handler& h) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
int stacksize = lua_gettop(base_t::lua_state());
|
2016-03-03 10:02:12 +08:00
|
|
|
int firstreturn = std::max(1, stacksize - static_cast<int>(n) - 1);
|
|
|
|
int returncount = 0;
|
|
|
|
call_status code = call_status::ok;
|
|
|
|
#ifndef SOL_NO_EXCEPTIONS
|
2016-03-12 00:34:44 +08:00
|
|
|
auto onexcept = [&](const char* error) {
|
2016-03-03 10:02:12 +08:00
|
|
|
h.stackindex = 0;
|
|
|
|
if (h.target.valid()) {
|
|
|
|
h.target.push();
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::push(base_t::lua_state(), error);
|
|
|
|
lua_call(base_t::lua_state(), 1, 1);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
2016-03-12 00:34:44 +08:00
|
|
|
else {
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::push(base_t::lua_state(), error);
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
|
|
|
};
|
2016-03-03 10:02:12 +08:00
|
|
|
try {
|
|
|
|
#endif // No Exceptions
|
|
|
|
code = static_cast<call_status>(luacall(n, LUA_MULTRET, h));
|
2016-04-07 17:21:49 +08:00
|
|
|
int poststacksize = lua_gettop(base_t::lua_state());
|
2016-04-01 04:16:07 +08:00
|
|
|
returncount = poststacksize - (stacksize - 1);
|
2016-03-03 10:02:12 +08:00
|
|
|
#ifndef SOL_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
// Handle C++ errors thrown from C++ functions bound inside of lua
|
|
|
|
catch (const char* error) {
|
|
|
|
onexcept(error);
|
2016-04-07 17:21:49 +08:00
|
|
|
firstreturn = lua_gettop(base_t::lua_state());
|
|
|
|
return protected_function_result(base_t::lua_state(), firstreturn, 0, 1, call_status::runtime);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
catch (const std::exception& error) {
|
|
|
|
onexcept(error.what());
|
2016-04-07 17:21:49 +08:00
|
|
|
firstreturn = lua_gettop(base_t::lua_state());
|
|
|
|
return protected_function_result(base_t::lua_state(), firstreturn, 0, 1, call_status::runtime);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
catch (...) {
|
2016-03-03 16:16:59 +08:00
|
|
|
onexcept("caught (...) unknown error during protected_function call");
|
2016-04-07 17:21:49 +08:00
|
|
|
firstreturn = lua_gettop(base_t::lua_state());
|
|
|
|
return protected_function_result(base_t::lua_state(), firstreturn, 0, 1, call_status::runtime);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
#endif // No Exceptions
|
2016-04-07 17:21:49 +08:00
|
|
|
return protected_function_result(base_t::lua_state(), firstreturn, returncount, returncount, code);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-03-12 00:34:44 +08:00
|
|
|
reference error_handler;
|
2016-03-03 10:02:12 +08:00
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_protected_function() = default;
|
|
|
|
basic_protected_function(const basic_protected_function&) = default;
|
|
|
|
basic_protected_function& operator=(const basic_protected_function&) = default;
|
|
|
|
basic_protected_function(basic_protected_function&& ) = default;
|
|
|
|
basic_protected_function& operator=(basic_protected_function&& ) = default;
|
|
|
|
basic_protected_function(lua_State* L, int index = -1): base_t(L, index), error_handler(get_default_handler()) {
|
2016-04-04 14:28:39 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
2016-04-11 13:02:43 +08:00
|
|
|
stack::check<basic_protected_function>(L, index, type_panic);
|
2016-04-04 14:28:39 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2016-03-03 10:02:12 +08:00
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
protected_function_result operator()(Args&&... args) const {
|
|
|
|
return call<>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
decltype(auto) operator()(types<Ret...>, Args&&... args) const {
|
|
|
|
return call<Ret...>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Ret, typename... Args>
|
|
|
|
decltype(auto) call(Args&&... args) const {
|
|
|
|
handler h(error_handler);
|
2016-04-07 17:21:49 +08:00
|
|
|
base_t::push();
|
|
|
|
int pushcount = stack::multi_push(base_t::lua_state(), std::forward<Args>(args)...);
|
2016-04-01 00:12:04 +08:00
|
|
|
return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
|
2016-03-03 10:02:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_FUNCTION_HPP
|