Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion wutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,32 @@ int push_bool(lua_State *L, int bval) {
}
}

/// Prints the traceback to stderr.
// @param L the state
// @return 1; a boolean true value
static int traceback(lua_State *L) {
// call the Lua function debug.traceback(2)
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
lua_getfield(L, -1, "traceback");
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);

// print its results to stderr
fprintf(stderr, "%s\n", lua_tostring(L, -1));

// clean up before leaving.
// while this may often be used right before teardown, this isn't guaranteed.
lua_pop(L, 2);

return 1;
}

BOOL call_lua_direct(lua_State *L, Ref ref, int idx, const char *text, int flags) {
BOOL res,ipush = 1;
// push the error handler.
lua_pushcfunction(L, traceback);

// push the function
push_ref(L,ref);

Expand All @@ -136,7 +160,11 @@ BOOL call_lua_direct(lua_State *L, Ref ref, int idx, const char *text, int flags
free((char*)text);
}

lua_call(L, ipush, 1);
int err = lua_pcall(L, ipush, 1, -ipush - 2);
if (err) {
lua_pop(L, 2);
exit(-1);
}
res = lua_toboolean(L,-1);

// optionally dispose of the function
Expand Down