2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-03-03 10:02:12 +08:00
|
|
|
|
2017-05-15 22:41:50 +08:00
|
|
|
// Copyright (c) 2013-2017 Rapptz, ThePhD and contributors
|
2016-03-03 10:02:12 +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_PROTECTED_FUNCTION_HPP
|
|
|
|
#define SOL_PROTECTED_FUNCTION_HPP
|
|
|
|
|
|
|
|
#include "reference.hpp"
|
|
|
|
#include "stack.hpp"
|
|
|
|
#include "protected_function_result.hpp"
|
2017-07-10 00:00:57 +08:00
|
|
|
#include "unsafe_function.hpp"
|
2016-03-03 10:02:12 +08:00
|
|
|
#include <cstdint>
|
2016-06-04 09:40:23 +08:00
|
|
|
#include <algorithm>
|
2016-03-03 10:02:12 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-09-30 13:42:23 +08:00
|
|
|
namespace detail {
|
2017-09-13 07:15:23 +08:00
|
|
|
inline const char (&default_handler_name())[9] {
|
|
|
|
static const char name[9] = "sol.\xF0\x9F\x94\xA9";
|
2017-08-06 07:20:28 +08:00
|
|
|
return name;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2016-10-05 11:37:08 +08:00
|
|
|
|
2017-08-07 00:20:32 +08:00
|
|
|
template <bool b, typename target_t = reference>
|
2017-09-01 08:47:09 +08:00
|
|
|
struct protected_handler {
|
2017-09-14 12:35:40 +08:00
|
|
|
typedef is_stack_based<target_t> is_stack;
|
2017-08-07 00:20:32 +08:00
|
|
|
const target_t& target;
|
2016-10-05 11:37:08 +08:00
|
|
|
int stackindex;
|
2017-08-07 00:20:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
protected_handler(std::false_type, const target_t& target)
|
|
|
|
: target(target), stackindex(0) {
|
2017-08-07 00:20:32 +08:00
|
|
|
if (b) {
|
2016-10-05 11:37:08 +08:00
|
|
|
stackindex = lua_gettop(target.lua_state()) + 1;
|
|
|
|
target.push();
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 00:20:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
protected_handler(std::true_type, const target_t& target)
|
|
|
|
: target(target), stackindex(0) {
|
2017-08-07 00:20:32 +08:00
|
|
|
if (b) {
|
|
|
|
stackindex = target.stack_index();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
protected_handler(const target_t& target)
|
|
|
|
: protected_handler(is_stack(), target) {
|
|
|
|
}
|
2017-08-07 00:20:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
bool valid() const noexcept {
|
|
|
|
return b;
|
|
|
|
}
|
2017-08-07 00:20:32 +08:00
|
|
|
|
2017-09-01 08:47:09 +08:00
|
|
|
~protected_handler() {
|
2017-08-07 14:07:21 +08:00
|
|
|
if (!is_stack::value && stackindex != 0) {
|
2016-10-05 11:37:08 +08:00
|
|
|
lua_remove(target.lua_state(), stackindex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-08-08 02:22:56 +08:00
|
|
|
|
|
|
|
template <typename base_t, typename T>
|
|
|
|
basic_function<base_t> force_cast(T& p) {
|
|
|
|
return p;
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace detail
|
|
|
|
|
2017-08-07 00:20:32 +08:00
|
|
|
template <typename base_t, bool aligned = false, typename handler_t = reference>
|
2016-09-30 13:42:23 +08:00
|
|
|
class basic_protected_function : public base_t {
|
2016-06-20 05:59:40 +08:00
|
|
|
public:
|
2017-09-14 12:35:40 +08:00
|
|
|
typedef is_stack_based<handler_t> is_stack_handler;
|
2017-08-07 00:20:32 +08:00
|
|
|
|
|
|
|
static handler_t get_default_handler(lua_State* L) {
|
|
|
|
if (is_stack_handler::value || L == nullptr)
|
|
|
|
return handler_t(L, lua_nil);
|
2017-09-14 12:35:40 +08:00
|
|
|
L = is_main_threaded<base_t>::value ? main_thread(L, L) : L;
|
2017-08-06 07:20:28 +08:00
|
|
|
lua_getglobal(L, detail::default_handler_name());
|
|
|
|
auto pp = stack::pop_n(L, 1);
|
2017-08-07 00:20:32 +08:00
|
|
|
return handler_t(L, -1);
|
2016-09-30 13:42:23 +08:00
|
|
|
}
|
|
|
|
|
2017-08-07 00:20:32 +08:00
|
|
|
template <typename T>
|
|
|
|
static void set_default_handler(const T& ref) {
|
|
|
|
if (ref.lua_state() == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-14 12:35:40 +08:00
|
|
|
lua_State* L = ref.lua_state();
|
2017-08-07 00:20:32 +08:00
|
|
|
if (!ref.valid()) {
|
2017-09-14 12:35:40 +08:00
|
|
|
lua_pushnil(L);
|
|
|
|
lua_setglobal(L, detail::default_handler_name());
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2017-08-07 00:20:32 +08:00
|
|
|
else {
|
|
|
|
ref.push();
|
2017-09-14 12:35:40 +08:00
|
|
|
lua_setglobal(L, detail::default_handler_name());
|
2017-08-07 00:20:32 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-07 00:20:32 +08:00
|
|
|
template <bool b>
|
2017-09-01 08:47:09 +08:00
|
|
|
call_status luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, detail::protected_handler<b, handler_t>& h) const {
|
2017-09-10 16:02:50 +08:00
|
|
|
return static_cast<call_status>(lua_pcall(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stackindex));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <std::size_t... I, bool b, typename... Ret>
|
2017-09-01 08:47:09 +08:00
|
|
|
auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
|
2016-06-20 05:59:40 +08:00
|
|
|
luacall(n, sizeof...(Ret), h);
|
2017-08-06 07:20:28 +08:00
|
|
|
return stack::pop<std::tuple<Ret...>>(lua_state());
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <std::size_t I, bool b, typename Ret>
|
2017-09-01 08:47:09 +08:00
|
|
|
Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
|
2016-06-20 05:59:40 +08:00
|
|
|
luacall(n, 1, h);
|
2017-08-06 07:20:28 +08:00
|
|
|
return stack::pop<Ret>(lua_state());
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-08-07 00:20:32 +08:00
|
|
|
template <std::size_t I, bool b>
|
2017-09-01 08:47:09 +08:00
|
|
|
void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
|
2016-06-20 05:59:40 +08:00
|
|
|
luacall(n, 0, h);
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:20:32 +08:00
|
|
|
template <bool b>
|
2017-09-01 08:47:09 +08:00
|
|
|
protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
|
2017-08-06 07:20:28 +08:00
|
|
|
int stacksize = lua_gettop(lua_state());
|
2016-09-04 22:54:55 +08:00
|
|
|
int poststacksize = stacksize;
|
|
|
|
int firstreturn = 1;
|
2016-06-20 05:59:40 +08:00
|
|
|
int returncount = 0;
|
|
|
|
call_status code = call_status::ok;
|
2016-03-03 10:02:12 +08:00
|
|
|
#ifndef SOL_NO_EXCEPTIONS
|
2016-06-20 05:59:40 +08:00
|
|
|
auto onexcept = [&](const char* error) {
|
|
|
|
h.stackindex = 0;
|
2017-08-07 14:07:21 +08:00
|
|
|
if (b) {
|
2016-06-20 05:59:40 +08:00
|
|
|
h.target.push();
|
2017-08-06 07:20:28 +08:00
|
|
|
stack::push(lua_state(), error);
|
|
|
|
lua_call(lua_state(), 1, 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-08-06 07:20:28 +08:00
|
|
|
stack::push(lua_state(), error);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
2017-11-10 06:41:46 +08:00
|
|
|
#if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || defined(SOL_LUAJIT)
|
2016-06-20 05:59:40 +08:00
|
|
|
try {
|
2017-10-27 01:15:02 +08:00
|
|
|
#endif
|
2016-03-03 10:02:12 +08:00
|
|
|
#endif // No Exceptions
|
2016-10-05 11:37:08 +08:00
|
|
|
firstreturn = (std::max)(1, static_cast<int>(stacksize - n - static_cast<int>(h.valid())));
|
2016-09-01 20:49:13 +08:00
|
|
|
code = luacall(n, LUA_MULTRET, h);
|
2017-08-06 07:20:28 +08:00
|
|
|
poststacksize = lua_gettop(lua_state()) - static_cast<int>(h.valid());
|
2016-10-05 11:37:08 +08:00
|
|
|
returncount = poststacksize - (firstreturn - 1);
|
2016-03-03 10:02:12 +08:00
|
|
|
#ifndef SOL_NO_EXCEPTIONS
|
2017-11-10 06:41:46 +08:00
|
|
|
#if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || defined(SOL_LUAJIT)
|
2017-10-27 01:15:02 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
// Handle C++ errors thrown from C++ functions bound inside of lua
|
|
|
|
catch (const char* error) {
|
|
|
|
onexcept(error);
|
2017-08-06 07:20:28 +08:00
|
|
|
firstreturn = lua_gettop(lua_state());
|
|
|
|
return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
catch (const std::exception& error) {
|
|
|
|
onexcept(error.what());
|
2017-08-06 07:20:28 +08:00
|
|
|
firstreturn = lua_gettop(lua_state());
|
|
|
|
return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2017-11-10 06:41:46 +08:00
|
|
|
#if !defined(SOL_LUAJIT)
|
|
|
|
// 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
|
2016-06-20 05:59:40 +08:00
|
|
|
catch (...) {
|
|
|
|
onexcept("caught (...) unknown error during protected_function call");
|
2017-08-06 07:20:28 +08:00
|
|
|
firstreturn = lua_gettop(lua_state());
|
|
|
|
return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2017-11-10 06:41:46 +08:00
|
|
|
#endif // LuaJIT
|
2017-10-21 08:59:52 +08:00
|
|
|
#else
|
|
|
|
// do not handle exceptions: they can be propogated into C++ and keep all type information / rich information
|
|
|
|
#endif // about as safe as possible
|
2016-03-03 10:02:12 +08:00
|
|
|
#endif // No Exceptions
|
2017-08-06 07:20:28 +08:00
|
|
|
return protected_function_result(lua_state(), firstreturn, returncount, returncount, code);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-08-06 07:20:28 +08:00
|
|
|
using base_t::lua_state;
|
|
|
|
|
2017-08-07 00:20:32 +08:00
|
|
|
handler_t error_handler;
|
2016-06-20 05:59:40 +08:00
|
|
|
|
|
|
|
basic_protected_function() = default;
|
2017-09-14 20:45:44 +08:00
|
|
|
template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_protected_function>>, meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<T>>>, meta::neg<std::is_same<base_t, stack_reference>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(T&& r) noexcept
|
|
|
|
: base_t(std::forward<T>(r)), error_handler(get_default_handler(r.lua_state())) {
|
2016-10-01 13:27:40 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
|
|
|
if (!is_function<meta::unqualified_t<T>>::value) {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_protected_function>(lua_state(), -1, handler);
|
2016-10-01 13:27:40 +08:00
|
|
|
}
|
|
|
|
#endif // Safety
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
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;
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(const basic_function<base_t>& b)
|
|
|
|
: basic_protected_function(b, get_default_handler(b.lua_state())) {
|
|
|
|
}
|
|
|
|
basic_protected_function(basic_function<base_t>&& b)
|
|
|
|
: basic_protected_function(std::move(b), get_default_handler(b.lua_state())) {
|
|
|
|
}
|
|
|
|
basic_protected_function(const basic_function<base_t>& b, handler_t eh)
|
|
|
|
: base_t(b), error_handler(std::move(eh)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(basic_function<base_t>&& b, handler_t eh)
|
|
|
|
: base_t(std::move(b)), error_handler(std::move(eh)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(const stack_reference& r)
|
|
|
|
: basic_protected_function(r.lua_state(), r.stack_index(), get_default_handler(r.lua_state())) {
|
|
|
|
}
|
|
|
|
basic_protected_function(stack_reference&& r)
|
|
|
|
: basic_protected_function(r.lua_state(), r.stack_index(), get_default_handler(r.lua_state())) {
|
|
|
|
}
|
|
|
|
basic_protected_function(const stack_reference& r, handler_t eh)
|
|
|
|
: basic_protected_function(r.lua_state(), r.stack_index(), std::move(eh)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(stack_reference&& r, handler_t eh)
|
|
|
|
: basic_protected_function(r.lua_state(), r.stack_index(), std::move(eh)) {
|
|
|
|
}
|
2017-08-07 14:07:21 +08:00
|
|
|
|
2016-10-01 13:27:40 +08:00
|
|
|
template <typename Super>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(const proxy_base<Super>& p)
|
|
|
|
: basic_protected_function(p, get_default_handler(p.lua_state())) {
|
|
|
|
}
|
2016-10-01 13:27:40 +08:00
|
|
|
template <typename Super>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(proxy_base<Super>&& p)
|
|
|
|
: basic_protected_function(std::move(p), get_default_handler(p.lua_state())) {
|
|
|
|
}
|
|
|
|
template <typename Proxy, typename Handler, meta::enable<std::is_base_of<proxy_base_tag, meta::unqualified_t<Proxy>>, meta::neg<is_lua_index<meta::unqualified_t<Handler>>>> = meta::enabler>
|
|
|
|
basic_protected_function(Proxy&& p, Handler&& eh)
|
|
|
|
: basic_protected_function(detail::force_cast<base_t>(p), std::forward<Handler>(eh)) {
|
|
|
|
}
|
|
|
|
|
2017-09-14 12:35:40 +08:00
|
|
|
template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(lua_State* L, T&& r)
|
|
|
|
: basic_protected_function(L, std::forward<T>(r), get_default_handler(L)) {
|
|
|
|
}
|
2017-09-14 12:35:40 +08:00
|
|
|
template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(lua_State* L, T&& r, handler_t eh)
|
|
|
|
: base_t(L, std::forward<T>(r)), error_handler(std::move(eh)) {
|
2017-09-14 20:45:44 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
|
|
|
auto pp = stack::push_pop(*this);
|
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_protected_function>(lua_state(), -1, handler);
|
|
|
|
#endif // Safety
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
basic_protected_function(lua_State* L, int index = -1)
|
|
|
|
: basic_protected_function(L, index, get_default_handler(L)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(lua_State* L, int index, handler_t eh)
|
|
|
|
: base_t(L, index), error_handler(std::move(eh)) {
|
2017-08-06 07:20:28 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2017-09-14 20:45:44 +08:00
|
|
|
stack::check<basic_protected_function>(L, index, handler);
|
2017-08-06 07:20:28 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(lua_State* L, absolute_index index)
|
|
|
|
: basic_protected_function(L, index, get_default_handler(L)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(lua_State* L, absolute_index index, handler_t eh)
|
|
|
|
: base_t(L, index), error_handler(std::move(eh)) {
|
2017-08-06 07:20:28 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2017-09-14 20:45:44 +08:00
|
|
|
stack::check<basic_protected_function>(L, index, handler);
|
2017-08-06 07:20:28 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(lua_State* L, raw_index index)
|
|
|
|
: basic_protected_function(L, index, get_default_handler(L)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(lua_State* L, raw_index index, handler_t eh)
|
|
|
|
: base_t(L, index), error_handler(std::move(eh)) {
|
2016-04-04 14:28:39 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2017-09-14 20:45:44 +08:00
|
|
|
stack::check<basic_protected_function>(L, index, handler);
|
2016-04-04 14:28:39 +08:00
|
|
|
#endif // Safety
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_protected_function(lua_State* L, ref_index index)
|
|
|
|
: basic_protected_function(L, index, get_default_handler(L)) {
|
|
|
|
}
|
|
|
|
basic_protected_function(lua_State* L, ref_index index, handler_t eh)
|
|
|
|
: base_t(L, index), error_handler(std::move(eh)) {
|
2016-12-10 13:25:25 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
|
|
|
auto pp = stack::push_pop(*this);
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2017-09-14 20:45:44 +08:00
|
|
|
stack::check<basic_protected_function>(lua_state(), -1, handler);
|
2016-12-10 13:25:25 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
protected_function_result operator()(Args&&... args) const {
|
|
|
|
return call<>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Ret, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) operator()(types<Ret...>, Args&&... args) const {
|
|
|
|
return call<Ret...>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Ret, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) call(Args&&... args) const {
|
2017-08-06 07:20:28 +08:00
|
|
|
if (!aligned) {
|
2017-08-07 00:20:32 +08:00
|
|
|
// we do not expect the function to already be on the stack: push it
|
|
|
|
if (error_handler.valid()) {
|
2017-09-01 08:47:09 +08:00
|
|
|
detail::protected_handler<true, handler_t> h(error_handler);
|
2017-08-07 00:20:32 +08:00
|
|
|
base_t::push();
|
|
|
|
int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
|
|
|
|
return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
|
|
|
|
}
|
|
|
|
else {
|
2017-09-01 08:47:09 +08:00
|
|
|
detail::protected_handler<false, handler_t> h(error_handler);
|
2017-08-07 00:20:32 +08:00
|
|
|
base_t::push();
|
|
|
|
int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
|
|
|
|
return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// the function is already on the stack at the right location
|
|
|
|
if (error_handler.valid()) {
|
|
|
|
// the handler will be pushed onto the stack manually,
|
|
|
|
// since it's not already on the stack this means we need to push our own
|
|
|
|
// function on the stack too and swap things to be in-place
|
|
|
|
if (!is_stack_handler::value) {
|
|
|
|
// so, we need to remove the function at the top and then dump the handler out ourselves
|
|
|
|
base_t::push();
|
|
|
|
}
|
2017-09-01 08:47:09 +08:00
|
|
|
detail::protected_handler<true, handler_t> h(error_handler);
|
2017-08-07 00:20:32 +08:00
|
|
|
if (!is_stack_handler::value) {
|
|
|
|
lua_replace(lua_state(), -3);
|
|
|
|
h.stackindex = lua_absindex(lua_state(), -2);
|
|
|
|
}
|
|
|
|
int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
|
|
|
|
return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
|
|
|
|
}
|
|
|
|
else {
|
2017-09-01 08:47:09 +08:00
|
|
|
detail::protected_handler<false, handler_t> h(error_handler);
|
2017-08-07 00:20:32 +08:00
|
|
|
int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
|
|
|
|
return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
|
|
|
|
}
|
2017-08-06 07:20:28 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-03-03 10:02:12 +08:00
|
|
|
|
|
|
|
#endif // SOL_FUNCTION_HPP
|