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. --- includes/BLua.h | 3 ++- src/BLua.cc | 25 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/includes/BLua.h b/includes/BLua.h index 40e89bd..a464a77 100644 --- a/includes/BLua.h +++ b/includes/BLua.h @@ -133,7 +133,7 @@ class Lua { int gettop() { return lua_gettop(L); } void getglobal(const char * name) throw (GeneralException); void pushLuaContext(); - void error(const char * msg); + void error(const char * msg) throw (GeneralException); void error(const String & msg) { error(msg.to_charp()); } int type(int i = -1) { return lua_type(L, i); } @@ -197,6 +197,7 @@ class Lua { void dumpvars_r(IO out, int idx, int depth = 0) throw (GeneralException); bool resumeC(); bool yieldC() throw (GeneralException); + void processException(GeneralException & e); lua_State * L; 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