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; lua_State* L;
int index; int index;
int returncount; int returncount;
int error; call_error error;
template <typename T, std::size_t I> template <typename T, std::size_t I>
stack::get_return<T> get(types<T>, indices<I>) const { stack::get_return<T> get(types<T>, indices<I>) const {
@ -53,16 +53,36 @@ private:
public: public:
function_result() = default; 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(const function_result&) = default;
function_result& operator=(const function_result&) = default; function_result& operator=(const function_result&) = default;
function_result(function_result&&) = default; function_result(function_result&& o) : L(o.L), index(o.index), returncount(o.returncount), error(o.error) {
function_result& operator=(function_result&&) = default; // 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 { bool valid() const {
return error == LUA_OK; return error == call_error::ok;
} }
template<typename T> template<typename T>
@ -81,23 +101,42 @@ public:
} }
~function_result() { ~function_result() {
lua_pop(L, returncount); stack::remove(L, index, error == call_error::ok ? returncount : 1);
} }
}; };
class function : public reference { class function : public reference {
public:
static reference default_handler;
private: private:
int luacodecall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const { struct handler {
return lua_pcallk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, 0, nullptr); 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); lua_callk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, nullptr);
} }
template<std::size_t... I, typename... Ret> template<std::size_t... I, typename... Ret>
std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::ptrdiff_t n) const { std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::ptrdiff_t n, const handler& h) const {
luacall(n, sizeof...(Ret)); luacall(n, sizeof...(Ret), h);
const int nreturns = static_cast<int>(sizeof...(Ret)); const int nreturns = static_cast<int>(sizeof...(Ret));
const int stacksize = lua_gettop(state()); const int stacksize = lua_gettop(state());
const int firstreturn = std::max(0, stacksize - nreturns) + 1; const int firstreturn = std::max(0, stacksize - nreturns) + 1;
@ -107,56 +146,60 @@ private:
} }
template<std::size_t I, typename Ret> template<std::size_t I, typename Ret>
Ret invoke(indices<I>, types<Ret>, std::ptrdiff_t n) const { Ret invoke(indices<I>, types<Ret>, std::ptrdiff_t n, const handler& h) const {
luacall(n, 1); luacall(n, 1, h);
return stack::pop<Ret>(state()); return stack::pop<Ret>(state());
} }
template <std::size_t I> template <std::size_t I>
void invoke(indices<I>, types<void>, std::ptrdiff_t n) const { void invoke(indices<I>, types<void>, std::ptrdiff_t n, const handler& h) const {
luacall(n, 0); 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 stacksize = lua_gettop(state());
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1); const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
int code = LUA_OK; int code = LUA_OK;
try { try {
code = luacodecall( n, LUA_MULTRET ); code = luacodecall(n, LUA_MULTRET, h);
} }
// Handle C++ errors thrown from C++ functions bound inside of lua // Handle C++ errors thrown from C++ functions bound inside of lua
catch ( const std::exception& error ) { catch (const std::exception& error) {
code = LUA_ERRRUN; code = LUA_ERRRUN;
stack::push( state(), error.what() ); stack::push(state(), error.what());
} }
// TODO: handle idiots? // TODO: handle idiots?
/*catch ( const char* error ) { /*catch (const char* error) {
code = LUA_ERRRUN; code = LUA_ERRRUN;
stack::push( state(), error ); stack::push(state(), error);
} }
catch ( const std::string& error ) { catch (const std::string& error) {
code = LUA_ERRRUN; code = LUA_ERRRUN;
stack::push( state(), error ); stack::push(state(), error);
} }
catch ( ... ) { catch (...) {
code = LUA_ERRRUN; code = LUA_ERRRUN;
stack::push( state(), "[sol] an unknownable runtime exception occurred" ); stack::push( state(), "[sol] an unknownable runtime exception occurred" );
}*/ }*/
catch ( ... ) { catch (...) {
throw; throw;
} }
const int poststacksize = lua_gettop(state()); const int poststacksize = lua_gettop(state());
const int returncount = poststacksize - firstreturn; 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: public:
sol::reference error_handler;
function() = default; function() = default;
function(lua_State* L, int index = -1): reference(L, index) { function(lua_State* L, int index = -1): reference(L, index) {
type_assert(L, index, type::function); type_assert(L, index, type::function);
} }
function(const function&) = default; function(const function&) = default;
function& operator=(const function&) = default; function& operator=(const function&) = default;
function( function&& ) = default;
function& operator=( function&& ) = default;
template<typename... Args> template<typename... Args>
function_result operator()(Args&&... args) const { function_result operator()(Args&&... args) const {
@ -171,14 +214,17 @@ public:
template<typename... Ret, typename... Args> template<typename... Ret, typename... Args>
auto call(Args&&... args) const 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(); push();
int pushcount = stack::push_args(state(), std::forward<Args>(args)...); int pushcount = stack::push_args(state(), std::forward<Args>(args)...);
auto tr = types<Ret...>(); auto tr = types<Ret...>();
return invoke(tr, tr, pushcount); return invoke(tr, tr, pushcount, h);
} }
}; };
sol::reference function::default_handler;
namespace stack { namespace stack {
template<typename... Sigs> template<typename... Sigs>
struct pusher<function_sig_t<Sigs...>> { struct pusher<function_sig_t<Sigs...>> {

View File

@ -31,6 +31,8 @@ private:
int ref = LUA_NOREF; int ref = LUA_NOREF;
int copy() const { int copy() const {
if (ref == LUA_NOREF)
return LUA_NOREF;
push(); push();
return luaL_ref(L, LUA_REGISTRYINDEX); return luaL_ref(L, LUA_REGISTRYINDEX);
} }
@ -46,11 +48,6 @@ public:
luaL_unref(L, LUA_REGISTRYINDEX, ref); luaL_unref(L, LUA_REGISTRYINDEX, ref);
} }
int push() const noexcept {
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
return 1;
}
reference(reference&& o) noexcept { reference(reference&& o) noexcept {
L = o.L; L = o.L;
ref = o.ref; ref = o.ref;
@ -80,6 +77,19 @@ public:
return *this; 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 { type get_type() const {
push(); push();
int result = lua_type(L, -1); 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 } // 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> 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) { 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)...); 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 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 { enum class type : int {
none = LUA_TNONE, none = LUA_TNONE,
nil = LUA_TNIL, nil = LUA_TNIL,