Allow for usage of function error handlers with lua

TODO: make this the default mode, with a short-cutting mode (sol::no_fail_function?)
This commit is contained in:
ThePhD 2015-10-22 11:20:32 -04:00
parent 7f4d8d8f89
commit ad039c8cc2
4 changed files with 119 additions and 33 deletions

View File

@ -38,7 +38,7 @@ private:
lua_State* L;
int index;
int returncount;
int error;
call_error error;
template <typename T, std::size_t I>
stack::get_return<T> get(types<T>, indices<I>) const {
@ -53,16 +53,36 @@ private:
public:
function_result() = default;
function_result(lua_State* L, int index = -1, int returncount = 0, int code = LUA_OK): L(L), index(index), returncount(returncount), error(code) {
function_result(lua_State* L, int index = -1, int returncount = 0, call_error error = call_error::ok): L(L), index(index), returncount(returncount), error(error) {
}
function_result(const function_result&) = default;
function_result& operator=(const function_result&) = default;
function_result(function_result&&) = default;
function_result& operator=(function_result&&) = default;
function_result(function_result&& o) : L(o.L), index(o.index), returncount(o.returncount), error(o.error) {
// Must be manual, otherwise destructor will screw us
// return count being 0 is enough to keep things clean
// but will be thorough
o.L = nullptr;
o.index = 0;
o.returncount = 0;
o.error = call_error::runtime;
}
function_result& operator=(function_result&& o) {
L = o.L;
index = o.index;
returncount = o.returncount;
error = o.error;
// Must be manual, otherwise destructor will screw us
// return count being 0 is enough to keep things clean
// but will be thorough
o.L = nullptr;
o.index = 0;
o.returncount = 0;
o.error = call_error::runtime;
}
bool valid() const {
return error == LUA_OK;
return error == call_error::ok;
}
template<typename T>
@ -81,23 +101,42 @@ public:
}
~function_result() {
lua_pop(L, returncount);
stack::remove(L, index, error == call_error::ok ? returncount : 1);
}
};
class function : public reference {
public:
static reference default_handler;
private:
int luacodecall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
return lua_pcallk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, 0, nullptr);
struct handler {
const reference& target;
int stack;
handler(const reference& target) : target(target), stack(0) {
if (target.valid()) {
stack = lua_gettop(target.state()) + 1;
target.push();
}
}
~handler() {
if (target.valid()) {
lua_remove(target.state(), stack);
}
}
};
int luacodecall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, const handler& h) const {
return lua_pcallk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stack, 0, nullptr);
}
void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, const handler& h) const {
lua_callk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, nullptr);
}
template<std::size_t... I, typename... Ret>
std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::ptrdiff_t n) const {
luacall(n, sizeof...(Ret));
std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::ptrdiff_t n, const handler& h) const {
luacall(n, sizeof...(Ret), h);
const int nreturns = static_cast<int>(sizeof...(Ret));
const int stacksize = lua_gettop(state());
const int firstreturn = std::max(0, stacksize - nreturns) + 1;
@ -107,22 +146,22 @@ private:
}
template<std::size_t I, typename Ret>
Ret invoke(indices<I>, types<Ret>, std::ptrdiff_t n) const {
luacall(n, 1);
Ret invoke(indices<I>, types<Ret>, std::ptrdiff_t n, const handler& h) const {
luacall(n, 1, h);
return stack::pop<Ret>(state());
}
template <std::size_t I>
void invoke(indices<I>, types<void>, std::ptrdiff_t n) const {
luacall(n, 0);
void invoke(indices<I>, types<void>, std::ptrdiff_t n, const handler& h) const {
luacall(n, 0, h);
}
function_result invoke(indices<>, types<>, std::ptrdiff_t n) const {
function_result invoke(indices<>, types<>, std::ptrdiff_t n, const handler& h) const {
const int stacksize = lua_gettop(state());
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
int code = LUA_OK;
try {
code = luacodecall( n, LUA_MULTRET );
code = luacodecall(n, LUA_MULTRET, h);
}
// Handle C++ errors thrown from C++ functions bound inside of lua
catch (const std::exception& error) {
@ -147,16 +186,20 @@ private:
}
const int poststacksize = lua_gettop(state());
const int returncount = poststacksize - firstreturn;
return function_result(state(), firstreturn + 1, returncount, code);
return function_result(state(), firstreturn + ( error_handler.valid() ? 0 : 1 ), returncount, static_cast<call_error>(code));
}
public:
sol::reference error_handler;
function() = default;
function(lua_State* L, int index = -1): reference(L, index) {
type_assert(L, index, type::function);
}
function(const function&) = default;
function& operator=(const function&) = default;
function( function&& ) = default;
function& operator=( function&& ) = default;
template<typename... Args>
function_result operator()(Args&&... args) const {
@ -171,14 +214,17 @@ public:
template<typename... Ret, typename... Args>
auto call(Args&&... args) const
-> decltype(invoke(types<Ret...>(), types<Ret...>(), 0)) {
-> decltype(invoke(types<Ret...>(), types<Ret...>(), 0, std::declval<handler>())) {
handler h(error_handler);
push();
int pushcount = stack::push_args(state(), std::forward<Args>(args)...);
auto tr = types<Ret...>();
return invoke(tr, tr, pushcount);
return invoke(tr, tr, pushcount, h);
}
};
sol::reference function::default_handler;
namespace stack {
template<typename... Sigs>
struct pusher<function_sig_t<Sigs...>> {

View File

@ -31,6 +31,8 @@ private:
int ref = LUA_NOREF;
int copy() const {
if (ref == LUA_NOREF)
return LUA_NOREF;
push();
return luaL_ref(L, LUA_REGISTRYINDEX);
}
@ -46,11 +48,6 @@ public:
luaL_unref(L, LUA_REGISTRYINDEX, ref);
}
int push() const noexcept {
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
return 1;
}
reference(reference&& o) noexcept {
L = o.L;
ref = o.ref;
@ -80,6 +77,19 @@ public:
return *this;
}
int push() const noexcept {
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
return 1;
}
int get_index() const {
return ref;
}
bool valid () const {
return !(ref == LUA_NOREF);
}
type get_type() const {
push();
int result = lua_type(L, -1);

View File

@ -575,6 +575,28 @@ inline void call(lua_State* L, int start, indices<I...>, types<void>, types<Args
}
} // detail
void remove( lua_State* L, int index, int count ) {
if ( count < 1 )
return;
int top = lua_gettop( L );
if ( index == -1 || top == index ) {
// Slice them right off the top
lua_pop( L, static_cast<int>(count) );
return;
}
// Remove each item one at a time using stack operations
// Probably slower, maybe, haven't benchmarked,
// but necessary
if ( index < 0 ) {
index = lua_gettop( L ) + (index + 1);
}
int last = index + count;
for ( int i = index; i < last; ++i ) {
lua_remove( L, i );
}
}
template <bool checkargs = detail::default_check_arguments, typename R, typename... Args, typename Fx, typename... FxArgs, typename = typename std::enable_if<!std::is_void<R>::value>::type>
inline R call(lua_State* L, int start, types<R> tr, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
return detail::call<checkargs>(L, start, ta, tr, ta, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);

View File

@ -62,6 +62,14 @@ enum class call_syntax {
colon = 1
};
enum class call_error : int {
ok = LUA_OK,
runtime = LUA_ERRRUN,
memory = LUA_ERRMEM,
handler = LUA_ERRERR,
gc = LUA_ERRGCMM
};
enum class type : int {
none = LUA_TNONE,
nil = LUA_TNIL,