From 59b889008760325845aeb04b7ac3fad5e1068c0f Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 22 Aug 2010 03:41:39 +0200 Subject: Print out a backtrace when a Lua error is raised The backtrace is printed by using debug.traceback as error handler when calling Lua functions. At the moment it still looks pretty ugly since Lua is not aware of the file names of the scripts (to be fixed). Reviewed-by: Jared Adams --- src/scripting/lua.cpp | 8 +++++++- src/scripting/luascript.cpp | 38 +++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index c4c215d..945392e 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2007-2010 The Mana World Development Team + * Copyright (C) 2010 The Mana Developers * * This file is part of The Mana Server. * @@ -1710,12 +1711,17 @@ LuaScript::LuaScript(): { NULL, NULL } }; luaL_register(mState, "mana", callbacks); + lua_pop(mState, 1); // pop the 'mana' table // Make script object available to callback functions. lua_pushlightuserdata(mState, (void *)®istryKey); lua_pushlightuserdata(mState, this); lua_settable(mState, LUA_REGISTRYINDEX); - lua_settop(mState, 0); + // Push the error handler to first index of the stack + lua_getglobal(mState, "debug"); + lua_getfield(mState, -1, "traceback"); + lua_remove(mState, 1); // remove the 'debug' table + loadFile("scripts/lua/libmana.lua"); } diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp index 8e19cd5..922c15d 100644 --- a/src/scripting/luascript.cpp +++ b/src/scripting/luascript.cpp @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2007-2010 The Mana World Development Team + * Copyright (C) 2010 The Mana Developers * * This file is part of The Mana Server. * @@ -63,11 +64,11 @@ void LuaScript::push(Thing *v) int LuaScript::execute() { assert(nbArgs >= 0); - int res = lua_pcall(mState, nbArgs, 1, 0); + int res = lua_pcall(mState, nbArgs, 1, 1); nbArgs = -1; - if (res || !(lua_isnil(mState, 1) || lua_isnumber(mState, 1))) + if (res || !(lua_isnil(mState, -1) || lua_isnumber(mState, -1))) { - const char *s = lua_tostring(mState, 1); + const char *s = lua_tostring(mState, -1); LOG_WARN("Lua Script Error" << std::endl << " Script : " << mScriptFile << std::endl @@ -76,7 +77,7 @@ int LuaScript::execute() lua_pop(mState, 1); return 0; } - res = lua_tointeger(mState, 1); + res = lua_tointeger(mState, -1); lua_pop(mState, 1); return res; mCurFunction = ""; @@ -85,26 +86,25 @@ int LuaScript::execute() void LuaScript::load(const char *prog) { int res = luaL_loadstring(mState, prog); + if (res) + { + switch (res) { + case LUA_ERRSYNTAX: + LOG_ERROR("Syntax error while loading Lua script: " + << lua_tostring(mState, -1)); + break; + case LUA_ERRMEM: + LOG_ERROR("Memory allocation error while loading Lua script"); + break; + } - switch (res) { - case LUA_ERRSYNTAX: - LOG_ERROR("Syntax error while loading Lua script: " - << lua_tostring(mState, -1)); - return; - case LUA_ERRMEM: - LOG_ERROR("Memory allocation error while loading Lua script"); - return; + lua_pop(mState, 1); } - - // A Lua chunk is like a function, so "execute" it in order to initialize - // it. - res = lua_pcall(mState, 0, 0, 0); - if (res) + else if (lua_pcall(mState, 0, 0, 1)) { LOG_ERROR("Failure while initializing Lua script: " << lua_tostring(mState, -1)); - lua_settop(mState, 0); - return; + lua_pop(mState, 1); } } -- cgit