Update redbean to include stack trace in Lua errors

This commit is contained in:
Paul Kulchenko 2021-08-09 22:50:47 -07:00
parent 31a37aeb1a
commit f6e0b2199d

View file

@ -5400,25 +5400,38 @@ static bool LuaRun(const char *path, bool mandatory) {
struct Asset *a; struct Asset *a;
const char *code; const char *code;
size_t pathlen, codelen; size_t pathlen, codelen;
int status;
pathlen = strlen(path); pathlen = strlen(path);
if ((a = GetAsset(path, pathlen))) { if ((a = GetAsset(path, pathlen))) {
if ((code = LoadAsset(a, &codelen))) { if ((code = LoadAsset(a, &codelen))) {
effectivepath.p = path; effectivepath.p = path;
effectivepath.n = pathlen; effectivepath.n = pathlen;
DEBUGF("LuaRun(%`'s)", path); DEBUGF("LuaRun(%`'s)", path);
if (luaL_loadbufferx(L, code, codelen, path, 0) || status = luaL_loadbuffer(L, code, codelen, path);
lua_pcall(L, 0, LUA_MULTRET, 0)) { if (status == LUA_OK) {
/* int nresults;
* TODO: There needs to be some reasonable way to get a // create a coroutine to retrieve traceback on failure
* backtrace. The best thing about Django was the lua_State *co = lua_newthread(L);
* fabulous backtrace page (and the admin panel). // pop the thread, so that the function is at the top
*/
/* luaL_traceback(L, L, lua_tostring(L, -1), 0); */
WARNF("script failed to run %s", lua_tostring(L, -1));
if (mandatory) exit(1);
lua_pop(L, 1); lua_pop(L, 1);
// move the function to the top of the coro stack
lua_xmove(L, co, 1);
// resume the coroutine thus executing the function
status = lua_resume(co, L, 0, &nresults);
if (status != LUA_OK && status != LUA_YIELD) {
// move the error message
lua_xmove(co, L, 1);
// replace the error with the traceback on failure
luaL_traceback(L, co, lua_tostring(L, -1), 0);
lua_remove(L, -2); // remove the error message
}
} }
free(code); free(code);
if (status != LUA_OK && status != LUA_YIELD) {
WARNF("script failed to run: %s", lua_tostring(L, -1));
lua_pop(L, 1);
if (mandatory) exit(1);
}
} }
} }
return !!a; return !!a;