diff options
author | Mike Pall <mike> | 2012-10-01 20:45:30 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2012-10-01 20:45:30 +0200 |
commit | fcddd5a3a0269f30df0c0da6e9ff89d3e70d0e2c (patch) | |
tree | 4bce654a35af804e16eb55324aa92f9ab550a984 /src/lib_debug.c | |
parent | 3ad61689cf11ae73fef5dfe12f7ccb5fe539622e (diff) | |
download | luajit2-fcddd5a3a0269f30df0c0da6e9ff89d3e70d0e2c.tar.gz |
From Lua 5.2: Add luaL_traceback().
Diffstat (limited to 'src/lib_debug.c')
-rw-r--r-- | src/lib_debug.c | 50 |
1 files changed, 4 insertions, 46 deletions
diff --git a/src/lib_debug.c b/src/lib_debug.c index 07b025af..06750266 100644 --- a/src/lib_debug.c +++ b/src/lib_debug.c @@ -383,55 +383,13 @@ LJLIB_CF(debug_debug) LJLIB_CF(debug_traceback) { - int level; - int firstpart = 1; /* still before eventual `...' */ int arg; lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - if (lua_isnumber(L, arg+2)) { - level = (int)lua_tointeger(L, arg+2); - lua_pop(L, 1); - } + const char *msg = lua_tostring(L, arg+1); + if (msg == NULL && L->top > L->base+arg) + L->top = L->base+arg+1; else - level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ - if (lua_gettop(L) == arg) - lua_pushliteral(L, ""); - else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ - else lua_pushliteral(L, "\n"); - 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 */ - /* This only works with LuaJIT 2.x. Avoids O(n^2) behaviour. */ - lua_getstack(L1, -10, &ar); - level = ar.i_ci - LEVELS2; - } - 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); - } - lua_concat(L, lua_gettop(L) - arg); + luaL_traceback(L, L1, msg, lj_lib_optint(L, arg+2, (L == L1))); return 1; } |