'getlocal' gets information about parameters of Lua functions

This commit is contained in:
Roberto Ierusalimschy
2010-06-21 13:30:12 -03:00
parent bef5980744
commit ca3865cf1b
3 changed files with 37 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldblib.c,v 1.120 2010/02/18 19:18:41 roberto Exp roberto $ ** $Id: ldblib.c,v 1.121 2010/03/26 20:58:11 roberto Exp roberto $
** Interface from Lua to its debug API ** Interface from Lua to its debug API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -157,20 +157,28 @@ static int db_getlocal (lua_State *L) {
lua_State *L1 = getthread(L, &arg); lua_State *L1 = getthread(L, &arg);
lua_Debug ar; lua_Debug ar;
const char *name; const char *name;
int nvar = luaL_checkint(L, arg+2); /* local-variable index */
if (lua_isfunction(L, arg + 1)) { /* function argument? */
lua_pushvalue(L, arg + 1); /* push function */
lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
return 1;
}
else { /* stack-level argument */
if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
return luaL_argerror(L, arg+1, "level out of range"); return luaL_argerror(L, arg+1, "level out of range");
name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); name = lua_getlocal(L1, &ar, nvar);
if (name) { if (name) {
lua_xmove(L1, L, 1); lua_xmove(L1, L, 1); /* push local value */
lua_pushstring(L, name); lua_pushstring(L, name); /* push name */
lua_pushvalue(L, -2); lua_pushvalue(L, -2); /* re-order */
return 2; return 2;
} }
else { else {
lua_pushnil(L); lua_pushnil(L); /* no name (nor value) */
return 1; return 1;
} }
} }
}
static int db_setlocal (lua_State *L) { static int db_setlocal (lua_State *L) {

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 2.70 2010/04/13 20:48:12 roberto Exp roberto $ ** $Id: ldebug.c,v 2.71 2010/06/16 13:44:36 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -119,13 +119,22 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
StkId pos; const char *name;
const char *name = findlocal(L, ar->i_ci, n, &pos);
lua_lock(L); lua_lock(L);
if (ar == NULL) { /* information about non-active function? */
if (!isLfunction(L->top - 1)) /* not a Lua function? */
name = NULL;
else /* consider live variables at function start (parameters) */
name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
}
else { /* active function; get information through 'ar' */
StkId pos;
name = findlocal(L, ar->i_ci, n, &pos);
if (name) { if (name) {
setobj2s(L, L->top, pos); setobj2s(L, L->top, pos);
api_incr_top(L); api_incr_top(L);
} }
}
lua_unlock(L); lua_unlock(L);
return name; return name;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.86 2010/05/15 13:32:02 roberto Exp roberto $ ** $Id: lparser.c,v 2.87 2010/05/31 16:08:55 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -402,8 +402,8 @@ static void close_func (LexState *ls) {
lua_State *L = ls->L; lua_State *L = ls->L;
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
Proto *f = fs->f; Proto *f = fs->f;
removevars(fs, 0);
luaK_ret(fs, 0, 0); /* final return */ luaK_ret(fs, 0, 0); /* final return */
removevars(fs, 0);
luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
f->sizecode = fs->pc; f->sizecode = fs->pc;
luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);