Skip to content

Commit

Permalink
Output stack trace when uncaught exception is handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Johnson committed Apr 13, 2011
1 parent 1459d5f commit 92c594a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/wax.m
Expand Up @@ -38,6 +38,14 @@

void uncaughtExceptionHandler(NSException *e) {
NSLog(@"ERROR: Uncaught exception %@", [e description]);
lua_State *L = wax_currentLuaState();

if (L) {
wax_getStackTrace(L);
const char *stackTrace = luaL_checkstring(L, -1);
NSLog(@"%s", stackTrace);
lua_pop(L, -1); // remove the stackTrace
}
}

int wax_panic(lua_State *L) {
Expand Down
1 change: 1 addition & 0 deletions lib/wax_helpers.h
Expand Up @@ -78,6 +78,7 @@ void wax_printStackAt(lua_State *L, int i);
void wax_printTable(lua_State *L, int t);
void wax_log(int flag, NSString *format, ...);

int wax_getStackTrace(lua_State *L);
// Convertion Helpers
int wax_fromObjc(lua_State *L, const char *typeDescription, void *buffer);
void wax_fromInstance(lua_State *L, id instance);
Expand Down
18 changes: 18 additions & 0 deletions lib/wax_helpers.m
Expand Up @@ -84,6 +84,24 @@ void wax_log(int flag, NSString *format, ...) {
}
}

int wax_getStackTrace(lua_State *L) {
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}

lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
lua_remove(L, -2); // Remove debug

lua_call(L, 0, 1);
return 1;
}

int wax_fromObjc(lua_State *L, const char *typeDescription, void *buffer) {
BEGIN_STACK_MODIFY(L)

Expand Down

0 comments on commit 92c594a

Please sign in to comment.