aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-06-21 13:30:12 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-06-21 13:30:12 -0300
commitca3865cf1b1e0195de9d1c71e48305146798cb95 (patch)
tree7a2e39ad9fff66956365b69c45b2e967080afceb
parentbef5980744d260246df81a5cf824c43b9b8080f8 (diff)
downloadlua-ca3865cf1b1e0195de9d1c71e48305146798cb95.tar.gz
lua-ca3865cf1b1e0195de9d1c71e48305146798cb95.tar.bz2
lua-ca3865cf1b1e0195de9d1c71e48305146798cb95.zip
'getlocal' gets information about parameters of Lua functions
-rw-r--r--ldblib.c32
-rw-r--r--ldebug.c21
-rw-r--r--lparser.c4
3 files changed, 37 insertions, 20 deletions
diff --git a/ldblib.c b/ldblib.c
index 38bc40ac..d6d58233 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.120 2010/02/18 19:18:41 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.121 2010/03/26 20:58:11 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -157,19 +157,27 @@ static int db_getlocal (lua_State *L) {
157 lua_State *L1 = getthread(L, &arg); 157 lua_State *L1 = getthread(L, &arg);
158 lua_Debug ar; 158 lua_Debug ar;
159 const char *name; 159 const char *name;
160 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ 160 int nvar = luaL_checkint(L, arg+2); /* local-variable index */
161 return luaL_argerror(L, arg+1, "level out of range"); 161 if (lua_isfunction(L, arg + 1)) { /* function argument? */
162 name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); 162 lua_pushvalue(L, arg + 1); /* push function */
163 if (name) { 163 lua_pushstring(L, lua_getlocal(L, NULL, nvar)); /* push local name */
164 lua_xmove(L1, L, 1);
165 lua_pushstring(L, name);
166 lua_pushvalue(L, -2);
167 return 2;
168 }
169 else {
170 lua_pushnil(L);
171 return 1; 164 return 1;
172 } 165 }
166 else { /* stack-level argument */
167 if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */
168 return luaL_argerror(L, arg+1, "level out of range");
169 name = lua_getlocal(L1, &ar, nvar);
170 if (name) {
171 lua_xmove(L1, L, 1); /* push local value */
172 lua_pushstring(L, name); /* push name */
173 lua_pushvalue(L, -2); /* re-order */
174 return 2;
175 }
176 else {
177 lua_pushnil(L); /* no name (nor value) */
178 return 1;
179 }
180 }
173} 181}
174 182
175 183
diff --git a/ldebug.c b/ldebug.c
index 304e5708..fcead7ce 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.70 2010/04/13 20:48:12 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.71 2010/06/16 13:44:36 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -119,12 +119,21 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
119 119
120 120
121LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { 121LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
122 StkId pos; 122 const char *name;
123 const char *name = findlocal(L, ar->i_ci, n, &pos);
124 lua_lock(L); 123 lua_lock(L);
125 if (name) { 124 if (ar == NULL) { /* information about non-active function? */
126 setobj2s(L, L->top, pos); 125 if (!isLfunction(L->top - 1)) /* not a Lua function? */
127 api_incr_top(L); 126 name = NULL;
127 else /* consider live variables at function start (parameters) */
128 name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
129 }
130 else { /* active function; get information through 'ar' */
131 StkId pos;
132 name = findlocal(L, ar->i_ci, n, &pos);
133 if (name) {
134 setobj2s(L, L->top, pos);
135 api_incr_top(L);
136 }
128 } 137 }
129 lua_unlock(L); 138 lua_unlock(L);
130 return name; 139 return name;
diff --git a/lparser.c b/lparser.c
index a45b53ce..5b0bad6b 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 2.86 2010/05/15 13:32:02 roberto Exp roberto $ 2** $Id: lparser.c,v 2.87 2010/05/31 16:08:55 roberto Exp roberto $
3** Lua Parser 3** Lua Parser
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -402,8 +402,8 @@ static void close_func (LexState *ls) {
402 lua_State *L = ls->L; 402 lua_State *L = ls->L;
403 FuncState *fs = ls->fs; 403 FuncState *fs = ls->fs;
404 Proto *f = fs->f; 404 Proto *f = fs->f;
405 removevars(fs, 0);
406 luaK_ret(fs, 0, 0); /* final return */ 405 luaK_ret(fs, 0, 0); /* final return */
406 removevars(fs, 0);
407 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); 407 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
408 f->sizecode = fs->pc; 408 f->sizecode = fs->pc;
409 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); 409 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);