From a9baead59f0521b289ba39e4d56ca92b0359e31e Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Thu, 20 Sep 2012 15:50:20 +0200 Subject: [PATCH] From Lua 5.2: debug.getinfo(..., "u") returns nparams and isvararg. --- src/Makefile.dep | 2 +- src/lib_debug.c | 15 ++++++++++++--- src/lj_debug.c | 17 ++++++++++++++++- src/lj_debug.h | 20 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Makefile.dep b/src/Makefile.dep index 94a963ba..e00ba2f6 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -8,7 +8,7 @@ lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_lib.h lj_libdef.h lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ - lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h + lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h lj_libdef.h lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h \ lj_ctype.h lj_cparse.h lj_cdata.h lj_cconv.h lj_carith.h lj_ccall.h \ diff --git a/src/lib_debug.c b/src/lib_debug.c index ab504de7..d90c422f 100644 --- a/src/lib_debug.c +++ b/src/lib_debug.c @@ -15,6 +15,7 @@ #include "lj_obj.h" #include "lj_err.h" +#include "lj_debug.h" #include "lj_lib.h" /* ------------------------------------------------------------------------ */ @@ -77,6 +78,12 @@ static void settabsi(lua_State *L, const char *i, int v) lua_setfield(L, -2, i); } +static void settabsb(lua_State *L, const char *i, int v) +{ + lua_pushboolean(L, v); + lua_setfield(L, -2, i); +} + static lua_State *getthread(lua_State *L, int *arg) { if (L->base < L->top && tvisthread(L->base)) { @@ -101,12 +108,12 @@ static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) LJLIB_CF(debug_getinfo) { - lua_Debug ar; + lj_Debug ar; int arg, opt_f = 0, opt_L = 0; lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnSu"); if (lua_isnumber(L, arg+1)) { - if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { + if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), (lua_Debug *)&ar)) { setnilV(L->top-1); return 1; } @@ -116,7 +123,7 @@ LJLIB_CF(debug_getinfo) } else { lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL); } - if (!lua_getinfo(L1, options, &ar)) + if (!lj_debug_getinfo(L1, options, &ar, 1)) lj_err_arg(L, arg+2, LJ_ERR_INVOPT); lua_createtable(L, 0, 16); /* Create result table. */ for (; *options; options++) { @@ -133,6 +140,8 @@ LJLIB_CF(debug_getinfo) break; case 'u': settabsi(L, "nups", ar.nups); + settabsi(L, "nparams", ar.nparams); + settabsb(L, "isvararg", ar.isvararg); break; case 'n': settabss(L, "name", ar.name); diff --git a/src/lj_debug.c b/src/lj_debug.c index 7dba8072..0e26cd7c 100644 --- a/src/lj_debug.c +++ b/src/lj_debug.c @@ -423,7 +423,7 @@ LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) return name; } -LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) +int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext) { int opt_f = 0, opt_L = 0; TValue *frame = NULL; @@ -471,6 +471,16 @@ LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1; } else if (*what == 'u') { ar->nups = fn->c.nupvalues; + if (ext) { + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + ar->nparams = pt->numparams; + ar->isvararg = !!(pt->flags & PROTO_VARARG); + } else { + ar->nparams = 0; + ar->isvararg = 1; + } + } } else if (*what == 'n') { ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL; if (ar->namewhat == NULL) { @@ -515,6 +525,11 @@ LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) return 1; /* Ok. */ } +LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) +{ + return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0); +} + LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) { int size; diff --git a/src/lj_debug.h b/src/lj_debug.h index e00769ae..48e6a229 100644 --- a/src/lj_debug.h +++ b/src/lj_debug.h @@ -8,6 +8,24 @@ #include "lj_obj.h" +typedef struct lj_Debug { + /* Common fields. Must be in the same order as in lua.h. */ + int event; + const char *name; + const char *namewhat; + const char *what; + const char *source; + int currentline; + int nups; + int linedefined; + int lastlinedefined; + char short_src[LUA_IDSIZE]; + int i_ci; + /* Extended fields. Only valid if lj_debug_getinfo() is called with ext = 1.*/ + int nparams; + int isvararg; +} lj_Debug; + LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size); LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc); LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx); @@ -20,6 +38,8 @@ LJ_FUNC void lj_debug_shortname(char *out, GCstr *str); LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg, cTValue *frame, cTValue *nextframe); LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc); +LJ_FUNC int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, + int ext); /* Fixed internal variable names. */ #define VARNAMEDEF(_) \