From 8714cc02d6367039d3d76279f024d30326371277 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy <roberto@inf.puc-rio.br> Date: Fri, 12 May 2000 16:49:18 -0300 Subject: `getinfo' gets information about non-active functions, too. --- ldblib.c | 78 ++++++++++++++++++++++++++++++++++++-------------------------- ldebug.c | 18 +++++++++------ manual.tex | 50 ++++++++++++++++++++++++++++------------ 3 files changed, 91 insertions(+), 55 deletions(-) diff --git a/ldblib.c b/ldblib.c index ac3ad11a..c3ca748e 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,10 +1,11 @@ /* -** $Id: ldblib.c,v 1.13 2000/04/14 17:44:20 roberto Exp roberto $ +** $Id: ldblib.c,v 1.14 2000/05/08 13:21:35 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -41,40 +42,51 @@ static void settabso (lua_State *L, lua_Object t, const char *i, lua_Object v) { } -static void getstack (lua_State *L) { +static void getinfo (lua_State *L) { lua_Debug ar; - if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */ - return; - else { - const char *options = luaL_opt_string(L, 2, "flnSu"); - lua_Object res = lua_createtable(L); - if (!lua_getinfo(L, options, &ar)) - luaL_argerror(L, 2, "invalid option"); - for (; *options; options++) { - switch (*options) { - case 'S': - settabss(L, res, "source", ar.source); - settabsi(L, res, "linedefined", ar.linedefined); - settabss(L, res, "what", ar.what); - break; - case 'l': - settabsi(L, res, "currentline", ar.currentline); - break; - case 'u': - settabsi(L, res, "nups", ar.nups); - break; - case 'n': - settabss(L, res, "name", ar.name); - settabss(L, res, "namewhat", ar.namewhat); - break; - case 'f': - settabso(L, res, "func", ar.func); - break; - - } + lua_Object res; + lua_Object func = lua_getparam(L, 1); + const char *options = luaL_opt_string(L, 2, "flnSu"); + char buff[20]; + if (lua_isnumber(L, func)) { + if (!lua_getstack(L, lua_getnumber(L, func), &ar)) { + lua_pushnil(L); /* level out of range */ + return; + } + } + else if (lua_isfunction(L, func)) { + ar.func = func; + sprintf(buff, ">%.10s", options); + options = buff; + } + else + luaL_argerror(L, 1, "function or level expected"); + res = lua_createtable(L); + if (!lua_getinfo(L, options, &ar)) + luaL_argerror(L, 2, "invalid option"); + for (; *options; options++) { + switch (*options) { + case 'S': + settabss(L, res, "source", ar.source); + settabsi(L, res, "linedefined", ar.linedefined); + settabss(L, res, "what", ar.what); + break; + case 'l': + settabsi(L, res, "currentline", ar.currentline); + break; + case 'u': + settabsi(L, res, "nups", ar.nups); + break; + case 'n': + settabss(L, res, "name", ar.name); + settabss(L, res, "namewhat", ar.namewhat); + break; + case 'f': + settabso(L, res, "func", ar.func); + break; } - lua_pushobject(L, res); } + lua_pushobject(L, res); } @@ -163,7 +175,7 @@ static void setlinehook (lua_State *L) { static const struct luaL_reg dblib[] = { {"getlocal", getlocal}, - {"getstack", getstack}, + {"getinfo", getinfo}, {"setcallhook", setcallhook}, {"setlinehook", setlinehook}, {"setlocal", setlocal} diff --git a/ldebug.c b/ldebug.c index d3eadab8..3f2e5f78 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 1.17 2000/05/08 19:32:53 roberto Exp roberto $ +** $Id: ldebug.c,v 1.18 2000/05/08 20:49:05 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -132,8 +132,7 @@ int lua_setlocal (lua_State *L, const lua_Debug *ar, lua_Localvar *v) { } -static void lua_funcinfo (lua_Debug *ar) { - StkId func = ar->_func; +static void lua_funcinfo (lua_Debug *ar, StkId func) { switch (ttype(func)) { case TAG_LCLOSURE: case TAG_LMARK: ar->source = clvalue(func)->f.l->source->str; @@ -164,7 +163,7 @@ static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) { /* try to find a name for given function */ setnormalized(L->top, f); /* to be used by `checkfunc' */ for (i=0; i<=g->size; i++) { - if (checkfunc(L, val(node(g,i))) && ttype(key(node(g,i))) == TAG_STRING) { + if (ttype(key(node(g,i))) == TAG_STRING && checkfunc(L, val(node(g,i)))) { ar->name = tsvalue(key(node(g,i)))->str; ar->namewhat = "global"; return; @@ -178,12 +177,17 @@ static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) { int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { - StkId func = ar->_func; - LUA_ASSERT(L, is_T_MARK(ttype(func)), "invalid activation record"); + StkId func; + if (*what != '>') + func = ar->_func; + else { + what++; /* skip the '>' */ + func = ar->func; + } for (; *what; what++) { switch (*what) { case 'S': - lua_funcinfo(ar); + lua_funcinfo(ar, func); break; case 'l': ar->currentline = lua_currentline(L, func); diff --git a/manual.tex b/manual.tex index 7d0494e9..80ed4504 100644 --- a/manual.tex +++ b/manual.tex @@ -1,4 +1,4 @@ -% $Id: manual.tex,v 1.36 2000/04/17 19:23:48 roberto Exp roberto $ +% $Id: manual.tex,v 1.37 2000/05/12 19:19:18 roberto Exp roberto $ \documentclass[11pt]{article} \usepackage{fullpage,bnf} @@ -122,7 +122,7 @@ Waldemar Celes \tecgraf\ --- Computer Science Department --- PUC-Rio } -\date{{\small \tt\$Date: 2000/04/17 19:23:48 $ $}} +\date{{\small \tt\$Date: 2000/05/12 19:19:18 $ $}} \maketitle @@ -3329,7 +3329,22 @@ selects some fields of \verb|ar| to be filled, as indicated by the letter in parentheses in the definition of \verb|lua_Debug|; that is, an \verb|S| fills the fields \verb|source| and \verb|linedefined|, and \verb|l| fills the field \verb|currentline|, etc. -We describe each field below: + +To get information about a function that is not active (that is, +it is not in the stack), +you set the \verb|func| field of the \verb|lua_Debug| structure +with the function, +and start the \verb|what| string with the character \verb|>|. +For instance, to know in which line a function \verb|f| was defined, +you can write +\begin{verbatim} + lua_Debug ar; + ar.func = lua_getglobal(L, "f"); + lua_getinfo(L, ">S", &ar); + printf("%d\n", ar.linedefined); +\end{verbatim} + +The fields of \verb|lua_Debug| have the following meaning: \begin{description} \item[source] @@ -3523,21 +3538,26 @@ As a general rule, if your program does not need this library, do not open it. -\subsubsection*{\ff \T{getstack (level, [what])}}\Deffunc{getstack} +\subsubsection*{\ff \T{getinfo (function, [what])}}\Deffunc{getinfo} + +This function returns a table with information about a function. +You can give the function directly, +or you can give a number as the value of \verb|function|, +which means the function running at level \verb|function| of the stack: +Level 0 is the current function (\verb|getinfo| itself); +level 1 is the function that called \verb|getinfo|; +and so on. +If \verb|function| is a number larger than the number of active functions, +\verb|getinfo| returns \nil. -This function returns a table with information about the function -running at level \verb|level| of the stack. -Level 0 is the current function (\verb|getstack| itself); -level 1 is the function that called \verb|getstack|. -If \verb|level| is larger than the number of active functions, -the function returns \nil. -The table contains all the fields returned by \verb|lua_getinfo|, +The returned table contains all the fields returned by \verb|lua_getinfo|, with the string \verb|what| describing what to get. -The default for \rerb|what| is to get all information available. +The default for \verb|what| is to get all information available. -For instance, the expression \verb|getstack(1,"n").name| returns -the name of the current function, -if a reasonable name can be found. +For instance, the expression \verb|getinfo(1,"n").name| returns +the name of the current function, if a reasonable name can be found, +and \verb|getinfo(print)| returns a table with all available information +about the \verb|print| function. \subsubsection*{\ff \T{getlocal (level, local)}}\Deffunc{getlocal} -- cgit v1.2.3-55-g6feb