From e266a0e0889382031ff16231336ba8907bfb5ed9 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 8 Jun 2010 22:18:38 +0100 Subject: [PATCH] Print Lua stack trace on error --- scripts/init.lua | 2 +- scripts/trading.lua | 2 +- scripts/uidemo.lua | 6 +++--- src/LuaWrap.cpp | 38 ++++++++++++++++++++++++++++++++++++-- src/Main.cpp | 4 ---- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/scripts/init.lua b/scripts/init.lua index 4fef375..ccfb8d0 100644 --- a/scripts/init.lua +++ b/scripts/init.lua @@ -4,7 +4,7 @@ _hooks = {} function _default_hook() - debug("_default_hook") + print("_default_hook") end function hook(tag, callback) diff --git a/scripts/trading.lua b/scripts/trading.lua index 48c1d6a..590fcfe 100644 --- a/scripts/trading.lua +++ b/scripts/trading.lua @@ -1,2 +1,2 @@ -debug("Hello from Lua!") +print("Hello from Lua!") diff --git a/scripts/uidemo.lua b/scripts/uidemo.lua index 61d60d0..5d8b770 100644 --- a/scripts/uidemo.lua +++ b/scripts/uidemo.lua @@ -1,11 +1,11 @@ -debug("Lua UI demo!") +print("Lua UI demo!") function btn2Click(btn) - debug("yahyahyah :D") + print("yahyahyah :D") end function layoutLoaded(layout) - debug("layout loaded") + print("layout loaded") layout:get("/wnd/btn2"):connect( gui.Widget.SIG_CLICK, btn2Click) diff --git a/src/LuaWrap.cpp b/src/LuaWrap.cpp index 780bbee..d7e345f 100644 --- a/src/LuaWrap.cpp +++ b/src/LuaWrap.cpp @@ -44,8 +44,33 @@ static int lua_log(lua_State *L) return 0; } +static int lua_traceback(lua_State *L) +{ + if (!lua_isstring(L, 1)) + return 1; + + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if (!lua_istable(L, -1)) { + error() << "debug not table"; + + lua_pop(L, 1); + return 1; + } + + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + + lua_pushvalue(L, 1); + lua_pushinteger(L, 2); + lua_call(L, 2, 1); + return 1; +} + static luaL_Reg luaFuncs[] = { - { "debug", lua_debug }, + { "print", lua_debug }, { "log", lua_log }, { NULL, NULL } }; @@ -76,6 +101,7 @@ LuaWrap::LuaWrap() luaopen_base(L); luaopen_string(L); luaopen_table(L); + luaopen_debug(L); lua_pushvalue(L, LUA_GLOBALSINDEX); luaL_register(L, NULL, luaFuncs); @@ -91,18 +117,26 @@ LuaWrap::~LuaWrap() void LuaWrap::pcall(int nargs, int nresults) { - int result = lua_pcall(L, nargs, nresults, 0); + // Insert traceback function underneath arguments + int base = lua_gettop(L) - nargs; + lua_pushcfunction(L, lua_traceback); + lua_insert(L, base); + + int result = lua_pcall(L, nargs, nresults, base); switch (result) { case LUA_ERRRUN: case LUA_ERRERR: error() << lua_tolstring(L, -1, NULL); lua_pop(L, 1); + lua_gc(L, LUA_GCCOLLECT, 0); break; case LUA_ERRMEM: error() << "Lua out of memory!"; break; } + + lua_remove(L, base); // Remove traceback function } void LuaWrap::eval(const string& code) diff --git a/src/Main.cpp b/src/Main.cpp index 022587c..59a8410 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -20,7 +20,6 @@ #include "GameScreens.hpp" #include "IResource.hpp" #include "IConfig.hpp" -#include "ILua.hpp" #include #include @@ -104,9 +103,6 @@ int main(int argc, char** argv) IConfigPtr cfg = getConfig(); ::window = makeSDLWindow(); - - ILuaPtr lua = getLua(); - lua->eval("debug(\"yahyahyah\")"); IScreenPtr screen; if (::action == "edit") { -- 2.39.2