summaryrefslogtreecommitdiff
path: root/src/scripting
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-08-22 03:41:39 +0200
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-08-22 13:11:38 +0200
commit59b889008760325845aeb04b7ac3fad5e1068c0f (patch)
tree77f42c8258f39d001cef07e5f7d039e53cec512e /src/scripting
parent6f122fd544e0c77cc6c38a294a353d7bd4accf9d (diff)
downloadmanaserv-59b889008760325845aeb04b7ac3fad5e1068c0f.tar.gz
manaserv-59b889008760325845aeb04b7ac3fad5e1068c0f.tar.bz2
manaserv-59b889008760325845aeb04b7ac3fad5e1068c0f.tar.xz
manaserv-59b889008760325845aeb04b7ac3fad5e1068c0f.zip
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
Diffstat (limited to 'src/scripting')
-rw-r--r--src/scripting/lua.cpp8
-rw-r--r--src/scripting/luascript.cpp38
2 files changed, 26 insertions, 20 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index c4c215d3..945392e1 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 *)&registryKey);
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 8e19cd52..922c15d0 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);
}
}