getlocal/setlocal can access vararg parameters

This commit is contained in:
Roberto Ierusalimschy 2010-11-30 15:17:51 -02:00
parent 7ca1bd639f
commit 12779b2b71
1 changed files with 22 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 2.73 2010/09/07 19:21:39 roberto Exp roberto $ ** $Id: ldebug.c,v 2.74 2010/10/11 20:24:42 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -94,25 +94,38 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
} }
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
int nparams = clvalue(ci->func)->l.p->numparams;
if (n >= ci->u.l.base - ci->func - nparams)
return NULL; /* no such vararg */
else {
*pos = ci->func + nparams + n;
return "(*vararg)"; /* generic name for any vararg */
}
}
static const char *findlocal (lua_State *L, CallInfo *ci, int n, static const char *findlocal (lua_State *L, CallInfo *ci, int n,
StkId *pos) { StkId *pos) {
const char *name = NULL; const char *name = NULL;
StkId base; StkId base;
if (isLua(ci)) { if (isLua(ci)) {
if (n < 0) /* access to vararg values? */
return findvararg(ci, -n, pos);
else {
base = ci->u.l.base; base = ci->u.l.base;
name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci)); name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci));
} }
}
else else
base = ci->func + 1; base = ci->func + 1;
if (name == NULL) { /* no 'standard' name? */ if (name == NULL) { /* no 'standard' name? */
StkId limit = (ci == L->ci) ? L->top : ci->next->func; StkId limit = (ci == L->ci) ? L->top : ci->next->func;
if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
name = "(*temporary)"; /* generic name for any valid slot */ name = "(*temporary)"; /* generic name for any valid slot */
else { else
*pos = base; /* to avoid warnings */
return NULL; /* no name */ return NULL; /* no name */
} }
}
*pos = base + (n - 1); *pos = base + (n - 1);
return name; return name;
} }
@ -128,7 +141,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0); name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
} }
else { /* active function; get information through 'ar' */ else { /* active function; get information through 'ar' */
StkId pos; StkId pos = 0; /* to avoid warnings */
name = findlocal(L, ar->i_ci, n, &pos); name = findlocal(L, ar->i_ci, n, &pos);
if (name) { if (name) {
setobj2s(L, L->top, pos); setobj2s(L, L->top, pos);
@ -141,7 +154,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
StkId pos; StkId pos = 0; /* to avoid warnings */
const char *name = findlocal(L, ar->i_ci, n, &pos); const char *name = findlocal(L, ar->i_ci, n, &pos);
lua_lock(L); lua_lock(L);
if (name) if (name)