From 17c1dde68eb101b07a6fa8ceded2ed93bb8a83fa Mon Sep 17 00:00:00 2001 From: ThePhD Date: Wed, 2 Mar 2016 09:36:42 -0500 Subject: [PATCH] Do exceptions better --- sol/function.hpp | 9 ++++-- sol/function_types_member.hpp | 22 +++----------- sol/types.hpp | 56 +++++++++++++++++------------------ 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 9b756aa7..6fcb7555 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -163,16 +163,19 @@ private: int firstreturn = std::max(1, stacksize - static_cast(n) - 1); int returncount = 0; call_status code = call_status::ok; -#ifdef SOL_NO_EXCEPTIONS +#ifdef SOL_NO_EXCEPTIONS + code = static_cast(luacall(n, LUA_MULTRET, h)); + int poststacksize = lua_gettop(lua_state()); + returncount = poststacksize - firstreturn; +#else try { -#endif // No Exceptions code = static_cast(luacall(n, LUA_MULTRET, h)); int poststacksize = lua_gettop(lua_state()); returncount = poststacksize - firstreturn; -#ifdef SOL_NO_EXCEPTIONS } // Handle C++ errors thrown from C++ functions bound inside of lua catch (const char* error) { + h.stackindex = 0; stack::push(lua_state(), error); firstreturn = lua_gettop(lua_state()); return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime); diff --git a/sol/function_types_member.hpp b/sol/function_types_member.hpp index cd102a27..7c0dec3f 100644 --- a/sol/function_types_member.hpp +++ b/sol/function_types_member.hpp @@ -36,24 +36,9 @@ struct functor_function : public base_function { template functor_function(Args&&... args): fx(std::forward(args)...) {} - template - int operator()(types tr, types ta, lua_State* L) { - stack::call(tr, ta, L, 0, fx); - int nargs = static_cast(sizeof...(Args)); - lua_pop(L, nargs); - return 0; - } - - template - int operator()(types tr, types ta, lua_State* L) { - return_type r = stack::call(tr, ta, L, 0, fx); - int nargs = static_cast(sizeof...(Args)); - lua_pop(L, nargs); - return stack::push(L, r); - } - virtual int operator()(lua_State* L) override { - return (*this)(types(), args_type(), L); + auto f = [&](lua_State* L) -> int { return stack::call_into_lua(meta::tuple_types(), args_types(), fx, L, 1);}; + return detail::trampoline(L, f); } }; @@ -80,7 +65,8 @@ struct member_function : public base_function { member_function(Tm&& m, Args&&... args): fx(std::forward(m), std::forward(args)...) {} virtual int operator()(lua_State* L) override { - return stack::call_into_lua(meta::tuple_types(), args_types(), fx, L, 1); + auto f = [&](lua_State* L) -> int { return stack::call_into_lua(meta::tuple_types(), args_types(), fx, L, 1);}; + return detail::trampoline(L, f); } }; } // function_detail diff --git a/sol/types.hpp b/sol/types.hpp index 5fba75be..7c5093c1 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -31,36 +31,12 @@ namespace detail { #ifdef SOL_NO_EXCEPTIONS template inline int static_trampoline (lua_State* L) { - try { - return f(L); - } - catch (const char *s) { // Catch and convert exceptions. - lua_pushstring(L, s); - } - catch (const std::exception& e) { - lua_pushstring(L, e.what()); - } - catch (...) { - lua_pushstring(L, "caught (...) exception"); - } - return lua_error(L); + return f(L); } template inline int trampoline(lua_State* L, Fx&& f) { - try { - return f(L); - } - catch (const char *s) { // Catch and convert exceptions. - lua_pushstring(L, s); - } - catch (const std::exception& e) { - lua_pushstring(L, e.what()); - } - catch (...) { - lua_pushstring(L, "caught (...) exception"); - } - return lua_error(L); + return f(L); } inline int c_trampoline(lua_State* L, lua_CFunction f) { @@ -69,12 +45,36 @@ inline int c_trampoline(lua_State* L, lua_CFunction f) { #else template inline int static_trampoline (lua_State* L) { - return f(L); + try { + return f(L); + } + catch (const char *s) { // Catch and convert exceptions. + lua_pushstring(L, s); + } + catch (const std::exception& e) { + lua_pushstring(L, e.what()); + } + catch (...) { + lua_pushstring(L, "caught (...) exception"); + } + return lua_error(L); } template inline int trampoline(lua_State* L, Fx&& f) { - return f(L); + try { + return f(L); + } + catch (const char *s) { // Catch and convert exceptions. + lua_pushstring(L, s); + } + catch (const std::exception& e) { + lua_pushstring(L, e.what()); + } + catch (...) { + lua_pushstring(L, "caught (...) exception"); + } + return lua_error(L); } inline int c_trampoline(lua_State* L, lua_CFunction f) {