traceback function moved to auxlib

This commit is contained in:
Roberto Ierusalimschy
2007-06-22 12:33:54 -03:00
parent d26bfb5ae4
commit 3f78748ef3
4 changed files with 69 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: ldblib.c,v 1.105 2006/09/11 14:07:24 roberto Exp roberto $
** $Id: ldblib.c,v 1.106 2007/04/26 20:39:38 roberto Exp roberto $
** Interface from Lua to its debug API
** See Copyright Notice in lua.h
*/
@@ -315,55 +315,16 @@ static int db_debug (lua_State *L) {
}
#define LEVELS1 12 /* size of the first part of the stack */
#define LEVELS2 10 /* size of the second part of the stack */
static int db_errorfb (lua_State *L) {
lua_Debug ar;
int firstpart = 1; /* still before eventual `...' */
int arg;
lua_State *L1 = getthread(L, &arg);
const char *msg = lua_tostring(L, arg + 1);
int level = (lua_isnumber(L, arg + 2)) ?
(int)lua_tointeger(L, arg + 2) :
(L == L1) ? 1 : 0; /* level 0 may be this own function */
lua_settop(L, ++arg);
if (msg) lua_pushfstring(L, "%s\n", msg);
else if (!lua_isnil(L, arg)) /* is there a non-string 'msg'? */
return 1; /* return it untouched */
lua_pushliteral(L, "stack traceback:");
while (lua_getstack(L1, level++, &ar)) {
if (level > LEVELS1 && firstpart) {
/* no more than `LEVELS2' more levels? */
if (!lua_getstack(L1, level+LEVELS2, &ar))
level--; /* keep going */
else {
lua_pushliteral(L, "\n\t..."); /* too many levels */
while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
level++;
}
firstpart = 0;
continue;
}
lua_pushliteral(L, "\n\t");
lua_getinfo(L1, "Snl", &ar);
lua_pushfstring(L, "%s:", ar.short_src);
if (ar.currentline > 0)
lua_pushfstring(L, "%d:", ar.currentline);
if (*ar.namewhat != '\0') /* is there a name? */
lua_pushfstring(L, " in function " LUA_QS, ar.name);
else {
if (*ar.what == 'm') /* main? */
lua_pushfstring(L, " in main chunk");
else if (*ar.what == 'C' || *ar.what == 't')
lua_pushliteral(L, " ?"); /* C function or tail call */
else
lua_pushfstring(L, " in function <%s:%d>",
ar.short_src, ar.linedefined);
}
lua_concat(L, lua_gettop(L) - arg);
if (msg == NULL && !lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */
lua_pushvalue(L, arg + 1); /* return it untouched */
else {
int level = luaL_optint(L, arg + 2, (L == L1) ? 1 : 0);
luaL_traceback(L, L1, msg, level);
}
lua_concat(L, lua_gettop(L) - arg);
return 1;
}