Merge pull request #39 from krmeet/lua-errors

Improve Lua error handling
This commit is contained in:
Agent X 2023-12-27 23:10:43 -05:00 committed by GitHub
commit 0821162349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 19 deletions

View File

@ -26,11 +26,12 @@ void smlua_mod_error(void) {
djui_lua_error(txt);
}
int smlua_error_handler(UNUSED lua_State* L) {
int smlua_error_handler(lua_State* L) {
if (lua_type(L, -1) == LUA_TSTRING) {
LOG_LUA("%s", lua_tostring(L, -1));
}
smlua_logline();
smlua_dump_stack();
return 0;
}
@ -176,7 +177,13 @@ static void smlua_load_script(struct Mod* mod, struct ModFile* file, u16 remoteI
gSmLuaConvertSuccess = true;
gLuaInitializingScript = 1;
LOG_INFO("Loading lua script '%s'", file->cachedPath);
bool failed = (luaL_loadfile(L, file->cachedPath) != LUA_OK);
if (luaL_loadfile(L, file->cachedPath) != LUA_OK) { // only run on success
LOG_LUA("Failed to load lua script '%s'.", file->cachedPath);
LOG_LUA("%s", smlua_to_string(L, lua_gettop(L)));
gLuaInitializingScript = 0;
return;
}
// check if this is the first time this mod has been loaded
lua_getfield(L, LUA_REGISTRYINDEX, mod->relativePath);
@ -214,22 +221,10 @@ static void smlua_load_script(struct Mod* mod, struct ModFile* file, u16 remoteI
smlua_cobject_init_per_file_globals(mod->relativePath);
}
// only run on success
if (failed) {
LOG_LUA("Failed to load lua script '%s'.", file->cachedPath);
LOG_LUA("%s", smlua_to_string(L, lua_gettop(L)));
gLuaInitializingScript = 0;
return;
}
// run chunks
LOG_INFO("Executing '%s'", file->relativePath);
if (smlua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) {
LOG_LUA("Failed to execute lua script '%s'.", file->cachedPath);
LOG_LUA("%s", smlua_to_string(L, lua_gettop(L)));
smlua_dump_stack();
gLuaInitializingScript = 0;
return;
}
gLuaInitializingScript = 0;
}

View File

@ -20,8 +20,8 @@
#include "pc/debuglog.h"
#include "pc/djui/djui_console.h"
#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } }
#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } }
#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { printf("[LUA] "), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } }
#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { printf("[LUA] "), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } }
#ifdef DEVELOPMENT
#define LUA_STACK_CHECK_BEGIN() int __LUA_STACK_TOP = lua_gettop(gLuaState)
@ -48,4 +48,4 @@ void smlua_init(void);
void smlua_update(void);
void smlua_shutdown(void);
#endif
#endif

View File

@ -774,9 +774,9 @@ void smlua_logline(void) {
int level = 0;
while (lua_getstack(L, level, &info)) {
lua_getinfo(L, "nSl", &info);
LOG_LUA(" [%d] %s:%d -- %s [%s]",
LOG_LUA("\t[%d] %s:%d -- %s [%s]",
level, info.short_src, info.currentline,
(info.name ? info.name : "<unknown>"), info.what);
++level;
}
}
}