Do exceptions better

This commit is contained in:
ThePhD 2016-03-02 09:36:42 -05:00
parent b24d17df31
commit 17c1dde68e
3 changed files with 38 additions and 49 deletions

View File

@ -163,16 +163,19 @@ private:
int firstreturn = std::max(1, stacksize - static_cast<int>(n) - 1);
int returncount = 0;
call_status code = call_status::ok;
#ifdef SOL_NO_EXCEPTIONS
#ifdef SOL_NO_EXCEPTIONS
code = static_cast<call_status>(luacall(n, LUA_MULTRET, h));
int poststacksize = lua_gettop(lua_state());
returncount = poststacksize - firstreturn;
#else
try {
#endif // No Exceptions
code = static_cast<call_status>(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);

View File

@ -36,24 +36,9 @@ struct functor_function : public base_function {
template<typename... Args>
functor_function(Args&&... args): fx(std::forward<Args>(args)...) {}
template<typename... Args>
int operator()(types<void> tr, types<Args...> ta, lua_State* L) {
stack::call(tr, ta, L, 0, fx);
int nargs = static_cast<int>(sizeof...(Args));
lua_pop(L, nargs);
return 0;
}
template<typename... Ret, typename... Args>
int operator()(types<Ret...> tr, types<Args...> ta, lua_State* L) {
return_type r = stack::call(tr, ta, L, 0, fx);
int nargs = static_cast<int>(sizeof...(Args));
lua_pop(L, nargs);
return stack::push(L, r);
}
virtual int operator()(lua_State* L) override {
return (*this)(types<return_type>(), args_type(), L);
auto f = [&](lua_State* L) -> int { return stack::call_into_lua(meta::tuple_types<return_type>(), 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<Tm>(m), std::forward<Args>(args)...) {}
virtual int operator()(lua_State* L) override {
return stack::call_into_lua(meta::tuple_types<return_type>(), args_types(), fx, L, 1);
auto f = [&](lua_State* L) -> int { return stack::call_into_lua(meta::tuple_types<return_type>(), args_types(), fx, L, 1);};
return detail::trampoline(L, f);
}
};
} // function_detail

View File

@ -31,36 +31,12 @@ namespace detail {
#ifdef SOL_NO_EXCEPTIONS
template <lua_CFunction f>
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 <typename Fx>
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 <lua_CFunction f>
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 <typename Fx>
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) {