Fix 'f' and 'L' options for debug.getinfo() and lua_getinfo().

This commit is contained in:
Mike Pall 2012-09-20 15:19:48 +02:00
parent 2d3c1967c7
commit 965694b0af
2 changed files with 60 additions and 47 deletions

View File

@ -102,7 +102,7 @@ static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)
LJLIB_CF(debug_getinfo) LJLIB_CF(debug_getinfo)
{ {
lua_Debug ar; lua_Debug ar;
int arg; int arg, opt_f = 0, opt_L = 0;
lua_State *L1 = getthread(L, &arg); lua_State *L1 = getthread(L, &arg);
const char *options = luaL_optstring(L, arg+2, "flnSu"); const char *options = luaL_optstring(L, arg+2, "flnSu");
if (lua_isnumber(L, arg+1)) { if (lua_isnumber(L, arg+1)) {
@ -118,27 +118,34 @@ LJLIB_CF(debug_getinfo)
} }
if (!lua_getinfo(L1, options, &ar)) if (!lua_getinfo(L1, options, &ar))
lj_err_arg(L, arg+2, LJ_ERR_INVOPT); lj_err_arg(L, arg+2, LJ_ERR_INVOPT);
lua_createtable(L, 0, 16); lua_createtable(L, 0, 16); /* Create result table. */
if (strchr(options, 'S')) { for (; *options; options++) {
settabss(L, "source", ar.source); switch (*options) {
settabss(L, "short_src", ar.short_src); case 'S':
settabsi(L, "linedefined", ar.linedefined); settabss(L, "source", ar.source);
settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "short_src", ar.short_src);
settabss(L, "what", ar.what); settabsi(L, "linedefined", ar.linedefined);
settabsi(L, "lastlinedefined", ar.lastlinedefined);
settabss(L, "what", ar.what);
break;
case 'l':
settabsi(L, "currentline", ar.currentline);
break;
case 'u':
settabsi(L, "nups", ar.nups);
break;
case 'n':
settabss(L, "name", ar.name);
settabss(L, "namewhat", ar.namewhat);
break;
case 'f': opt_f = 1; break;
case 'L': opt_L = 1; break;
default: break;
}
} }
if (strchr(options, 'l')) if (opt_L) treatstackoption(L, L1, "activelines");
settabsi(L, "currentline", ar.currentline); if (opt_f) treatstackoption(L, L1, "func");
if (strchr(options, 'u')) return 1; /* Return result table. */
settabsi(L, "nups", ar.nups);
if (strchr(options, 'n')) {
settabss(L, "name", ar.name);
settabss(L, "namewhat", ar.namewhat);
}
if (strchr(options, 'L'))
treatstackoption(L, L1, "activelines");
if (strchr(options, 'f'))
treatstackoption(L, L1, "func");
return 1; /* return table */
} }
LJLIB_CF(debug_getlocal) LJLIB_CF(debug_getlocal)

View File

@ -425,7 +425,7 @@ LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
{ {
int status = 1; int opt_f = 0, opt_L = 0;
TValue *frame = NULL; TValue *frame = NULL;
TValue *nextframe = NULL; TValue *nextframe = NULL;
GCfunc *fn; GCfunc *fn;
@ -478,35 +478,41 @@ LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
ar->name = NULL; ar->name = NULL;
} }
} else if (*what == 'f') { } else if (*what == 'f') {
setfuncV(L, L->top, fn); opt_f = 1;
incr_top(L);
} else if (*what == 'L') { } else if (*what == 'L') {
if (isluafunc(fn)) { opt_L = 1;
GCtab *t = lj_tab_new(L, 0, 0);
GCproto *pt = funcproto(fn);
const void *lineinfo = proto_lineinfo(pt);
if (lineinfo) {
BCLine first = pt->firstline;
int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4;
MSize i, szl = pt->sizebc-1;
for (i = 0; i < szl; i++) {
BCLine line = first +
(sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] :
sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] :
(BCLine)((const uint32_t *)lineinfo)[i]);
setboolV(lj_tab_setint(L, t, line), 1);
}
}
settabV(L, L->top, t);
} else {
setnilV(L->top);
}
incr_top(L);
} else { } else {
status = 0; /* Bad option. */ return 0; /* Bad option. */
} }
} }
return status; if (opt_f) {
setfuncV(L, L->top, fn);
incr_top(L);
}
if (opt_L) {
if (isluafunc(fn)) {
GCtab *t = lj_tab_new(L, 0, 0);
GCproto *pt = funcproto(fn);
const void *lineinfo = proto_lineinfo(pt);
if (lineinfo) {
BCLine first = pt->firstline;
int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4;
MSize i, szl = pt->sizebc-1;
for (i = 0; i < szl; i++) {
BCLine line = first +
(sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] :
sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] :
(BCLine)((const uint32_t *)lineinfo)[i]);
setboolV(lj_tab_setint(L, t, line), 1);
}
}
settabV(L, L->top, t);
} else {
setnilV(L->top);
}
incr_top(L);
}
return 1; /* Ok. */
} }
LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)