From efa460599d37b40a8b9d707096943a543be6db78 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Thu, 6 Jun 2019 00:58:13 +0200 Subject: HDB: Added quick & dirty Lua tracing --- engines/hdb/lua-script.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp index 184ff474dd..dd6c8b0f17 100644 --- a/engines/hdb/lua-script.cpp +++ b/engines/hdb/lua-script.cpp @@ -696,17 +696,59 @@ bool LuaScript::executeFile(const Common::String &filename) { } #endif +namespace { +int panicCB(lua_State *L) { + error("Lua panic. Error message: %s", lua_isnil(L, -1) ? "" : lua_tostring(L, -1)); + return 0; +} + +void debugHook(lua_State *L, lua_Debug *ar) { + if (!lua_getinfo(L, "Sn", ar)) + return; + + debug("LUA: %s %s: %s %d", ar->namewhat, ar->name, ar->short_src, ar->currentline); +} +} + bool LuaScript::executeChunk(const char *chunk, uint chunkSize, const Common::String &chunkName) const { - + if (!_systemInit) { return false; } - + + lua_atpanic(_state, panicCB); + + const char errorHandlerCode[] = + "local function ErrorHandler(message) " + " return message .. '\\n' .. debug.traceback('', 2) " + "end " + "return ErrorHandler"; + + if (luaL_loadbuffer(_state, errorHandlerCode, strlen(errorHandlerCode), "PCALL ERRORHANDLER") != 0) { + // An error occurred, so dislay the reason and exit + error("Couldn't compile luaL_pcall errorhandler:\n%s", lua_tostring(_state, -1)); + lua_pop(_state, 1); + + return false; + } + + + // Running the code, the error handler function sets the top of the stack + if (lua_pcall(_state, 0, 1, 0) != 0) { + // An error occurred, so dislay the reason and exit + error("Couldn't prepare luaL_pcall errorhandler:\n%s", lua_tostring(_state, -1)); + lua_pop(_state, 1); + + return false; + } + + lua_sethook(_state, debugHook, LUA_MASKCALL | LUA_MASKLINE, 0); + // Compile Chunk if (luaL_loadbuffer(_state, chunk, chunkSize, chunkName.c_str())) { error("Couldn't compile \"%s\": %s", chunkName.c_str(), lua_tostring(_state, -1)); lua_pop(_state, -1); - + return false; } -- cgit v1.2.3