summaryrefslogtreecommitdiff
path: root/ldebug.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-11-30 15:17:51 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-11-30 15:17:51 -0200
commit12779b2b7199caf0c9ddc9ca4abd8f70b6ddc17c (patch)
treea31eeac13c5c3ef8f4fa0e34d4c69aa77ab7f532 /ldebug.c
parent7ca1bd639fe0fcff7bcb0c3db34e35a6cab144e1 (diff)
downloadlua-12779b2b7199caf0c9ddc9ca4abd8f70b6ddc17c.tar.gz
lua-12779b2b7199caf0c9ddc9ca4abd8f70b6ddc17c.tar.bz2
lua-12779b2b7199caf0c9ddc9ca4abd8f70b6ddc17c.zip
getlocal/setlocal can access vararg parameters
Diffstat (limited to 'ldebug.c')
-rw-r--r--ldebug.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/ldebug.c b/ldebug.c
index b4f7010b..cd506ac2 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.73 2010/09/07 19:21:39 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.74 2010/10/11 20:24:42 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*/
@@ -94,13 +94,28 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
94} 94}
95 95
96 96
97static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
98 int nparams = clvalue(ci->func)->l.p->numparams;
99 if (n >= ci->u.l.base - ci->func - nparams)
100 return NULL; /* no such vararg */
101 else {
102 *pos = ci->func + nparams + n;
103 return "(*vararg)"; /* generic name for any vararg */
104 }
105}
106
107
97static const char *findlocal (lua_State *L, CallInfo *ci, int n, 108static const char *findlocal (lua_State *L, CallInfo *ci, int n,
98 StkId *pos) { 109 StkId *pos) {
99 const char *name = NULL; 110 const char *name = NULL;
100 StkId base; 111 StkId base;
101 if (isLua(ci)) { 112 if (isLua(ci)) {
102 base = ci->u.l.base; 113 if (n < 0) /* access to vararg values? */
103 name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci)); 114 return findvararg(ci, -n, pos);
115 else {
116 base = ci->u.l.base;
117 name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci));
118 }
104 } 119 }
105 else 120 else
106 base = ci->func + 1; 121 base = ci->func + 1;
@@ -108,10 +123,8 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
108 StkId limit = (ci == L->ci) ? L->top : ci->next->func; 123 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
109 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ 124 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
110 name = "(*temporary)"; /* generic name for any valid slot */ 125 name = "(*temporary)"; /* generic name for any valid slot */
111 else { 126 else
112 *pos = base; /* to avoid warnings */
113 return NULL; /* no name */ 127 return NULL; /* no name */
114 }
115 } 128 }
116 *pos = base + (n - 1); 129 *pos = base + (n - 1);
117 return name; 130 return name;
@@ -128,7 +141,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
128 name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0); 141 name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
129 } 142 }
130 else { /* active function; get information through 'ar' */ 143 else { /* active function; get information through 'ar' */
131 StkId pos; 144 StkId pos = 0; /* to avoid warnings */
132 name = findlocal(L, ar->i_ci, n, &pos); 145 name = findlocal(L, ar->i_ci, n, &pos);
133 if (name) { 146 if (name) {
134 setobj2s(L, L->top, pos); 147 setobj2s(L, L->top, pos);
@@ -141,11 +154,11 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
141 154
142 155
143LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { 156LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
144 StkId pos; 157 StkId pos = 0; /* to avoid warnings */
145 const char *name = findlocal(L, ar->i_ci, n, &pos); 158 const char *name = findlocal(L, ar->i_ci, n, &pos);
146 lua_lock(L); 159 lua_lock(L);
147 if (name) 160 if (name)
148 setobjs2s(L, pos, L->top - 1); 161 setobjs2s(L, pos, L->top - 1);
149 L->top--; /* pop value */ 162 L->top--; /* pop value */
150 lua_unlock(L); 163 lua_unlock(L);
151 return name; 164 return name;