From 7f4d8d8f89c9ad92fd4a1310fcbd7312fc2ff9a0 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Thu, 22 Oct 2015 06:49:53 -0400 Subject: [PATCH] Allow for failed function calls from lua. --- sol/function.hpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/sol/function.hpp b/sol/function.hpp index 4ea2bed9..e55e0fe8 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -120,7 +120,31 @@ private: function_result invoke(indices<>, types<>, std::ptrdiff_t n) const { const int stacksize = lua_gettop(state()); const int firstreturn = std::max(0, stacksize - static_cast(n) - 1); - int code = luacodecall(n, LUA_MULTRET); + int code = LUA_OK; + try { + code = luacodecall( n, LUA_MULTRET ); + } + // Handle C++ errors thrown from C++ functions bound inside of lua + catch ( const std::exception& error ) { + code = LUA_ERRRUN; + stack::push( state(), error.what() ); + } + // TODO: handle idiots? + /*catch ( const char* error ) { + code = LUA_ERRRUN; + stack::push( state(), error ); + } + catch ( const std::string& error ) { + code = LUA_ERRRUN; + stack::push( state(), error ); + } + catch ( ... ) { + code = LUA_ERRRUN; + stack::push( state(), "[sol] an unknownable runtime exception occurred" ); + }*/ + catch ( ... ) { + throw; + } const int poststacksize = lua_gettop(state()); const int returncount = poststacksize - firstreturn; return function_result(state(), firstreturn + 1, returncount, code); @@ -263,7 +287,7 @@ struct pusher> { base_function* target = luafunc.release(); void* userdata = reinterpret_cast(target); lua_CFunction freefunc = &base_function::call; - + int metapushed = luaL_newmetatable(L, metatablename); if(metapushed == 1) { lua_pushstring(L, "__gc");