1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Use a Lua error handler that calls tostring (#11913)

This commit is contained in:
Jude Melton-Houghton 2022-12-15 07:37:49 -05:00 committed by GitHub
parent 1f3b5e553b
commit 0fc97a1483
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 9 deletions

View file

@ -27,10 +27,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
std::string script_get_backtrace(lua_State *L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE);
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_call(L, 0, 1);
std::string result = luaL_checkstring(L, -1);
lua_pop(L, 1);
lua_pop(L, 2);
return result;
}
@ -46,6 +47,25 @@ int script_exception_wrapper(lua_State *L, lua_CFunction f)
return lua_error(L); // Rethrow as a Lua error.
}
int script_error_handler(lua_State *L)
{
lua_getglobal(L, "core");
lua_getfield(L, -1, "error_handler");
if (!lua_isnil(L, -1)) {
lua_pushvalue(L, 1);
} else {
// No Lua error handler available. Call debug.traceback(tostring(#1), level).
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_getglobal(L, "tostring");
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
}
lua_pushinteger(L, 2); // Stack level
lua_call(L, 2, 1);
return 1;
}
/*
* Note that we can't get tracebacks for LUA_ERRMEM or LUA_ERRERR (without
* hacking Lua internals). For LUA_ERRMEM, this is because memory errors will