aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Sandulenko2019-06-06 00:58:13 +0200
committerEugene Sandulenko2019-09-03 17:16:43 +0200
commitefa460599d37b40a8b9d707096943a543be6db78 (patch)
tree0ff059ebbbe209398a987f0f9cf67bf574a2c0b9
parent472d5f210428da05b9964c2192bd56e99863c0c6 (diff)
downloadscummvm-rg350-efa460599d37b40a8b9d707096943a543be6db78.tar.gz
scummvm-rg350-efa460599d37b40a8b9d707096943a543be6db78.tar.bz2
scummvm-rg350-efa460599d37b40a8b9d707096943a543be6db78.zip
HDB: Added quick & dirty Lua tracing
-rw-r--r--engines/hdb/lua-script.cpp48
1 files 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;
}