From a4312c1e04f33fd8b5f2b2649ae6aff6d31e763d Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Mon, 5 Aug 2013 09:35:31 +0200 Subject: Improving the output of GeneralExceptions in Lua; also you can't call lua_error on a yielded co-routine. --- src/BLua.cc | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/BLua.cc') diff --git a/src/BLua.cc b/src/BLua.cc index 48e77d4..bed33a0 100644 --- a/src/BLua.cc +++ b/src/BLua.cc @@ -257,7 +257,7 @@ int Balau::LuaStatics::callwrap(lua_State * __L, lua_CFunction func) { L.error("The C callback from Lua has thrown an EAgain - you can't do that, you have to wrap it using Lua::yield. The application will now crash, because this task will stop, but the event in the EAgain will later try to wake it up."); } catch (GeneralException & e) { - L.error(String("GeneralException: ") + e.getMsg()); + L.processException(e); } // LuaJIT sucks sometime. // catch (...) { @@ -479,10 +479,17 @@ void Balau::Lua::pushLuaContext() { } while (!got_error); } -void Balau::Lua::error(const char * msg) { +void Balau::Lua::error(const char * msg) throw (GeneralException) { push(msg); - lua_error(L); + if (yielded()) { + pushLuaContext(); + showerror(); + + throw LuaException("Runtime error while running yielded C code."); + } else { + lua_error(L); + } } bool Balau::Lua::isobject(int i) { @@ -845,7 +852,7 @@ bool Balau::Lua::resumeC() { yieldedAgain = true; } catch (Balau::GeneralException & e) { - error(String("GeneralException: ") + e.getMsg()); + processException(e); } if (!yieldedAgain) resume(nargs); @@ -882,6 +889,16 @@ bool Balau::Lua::yieldC() throw (GeneralException) { return yieldC(); } +void Balau::Lua::processException(GeneralException & e) { + const char * details = e.getDetails(); + if (details) + push(details); + auto trace = e.getTrace(); + for (String & str : trace) + push(str); + error(String("General Exception: ") + e.getMsg()); +} + void Balau::Lua::showstack(int level) { int n = lua_gettop(L); int i; -- cgit v1.2.3