diff options
-rw-r--r-- | engines/hdb/lua-script.cpp | 13 | ||||
-rw-r--r-- | engines/hdb/lua-script.h | 2 |
2 files changed, 14 insertions, 1 deletions
diff --git a/engines/hdb/lua-script.cpp b/engines/hdb/lua-script.cpp index 3ca4e73e57..b719b6b73a 100644 --- a/engines/hdb/lua-script.cpp +++ b/engines/hdb/lua-script.cpp @@ -635,14 +635,18 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, int32 length) { */ + // Register panic callback function lua_atpanic(_state, panicCB); + // Error handler for lua_pcall calls + // The code below contains a local error handler function const char errorHandlerCode[] = "local function ErrorHandler(message) " " return message .. '\\n' .. debug.traceback('', 2) " "end " "return ErrorHandler"; + // Compile the code 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)); @@ -651,7 +655,6 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, int32 length) { 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 @@ -661,6 +664,10 @@ bool LuaScript::initScript(Common::SeekableReadStream *stream, int32 length) { return false; } + // Place the error handler function in the Lua registry, and remember the index + _pcallErrorhandlerRegistryIndex = luaL_ref(_state, LUA_REGISTRYINDEX); + + // Initialize debugging callback lua_sethook(_state, debugHook, LUA_MASKCALL | LUA_MASKLINE, 0); // Load GLOBAL_LUA and execute it @@ -755,6 +762,10 @@ bool LuaScript::executeChunk(const char *chunk, uint chunkSize, const Common::St return false; } + // Error handling function to be executed after the function is put on the stack + lua_rawgeti(_state, LUA_REGISTRYINDEX, _pcallErrorhandlerRegistryIndex); + lua_insert(_state, -2); + // Execute Chunk if (lua_pcall(_state, 0, 0, 0)) { error("An error occured while executing \"%s\": %s.", chunkName.c_str(), lua_tostring(_state, -1)); diff --git a/engines/hdb/lua-script.h b/engines/hdb/lua-script.h index 5ad9d2c409..5cdb9fb466 100644 --- a/engines/hdb/lua-script.h +++ b/engines/hdb/lua-script.h @@ -44,6 +44,8 @@ public: private: lua_State *_state; + int _pcallErrorhandlerRegistryIndex; + Common::SeekableReadStream* _globalLuaStream; int32 _globalLuaLength; bool _systemInit; |