diff options
Diffstat (limited to 'ldebug.c')
-rw-r--r-- | ldebug.c | 55 |
1 files changed, 29 insertions, 26 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldebug.c,v 2.49 2009/04/30 17:42:21 roberto Exp roberto $ | 2 | ** $Id: ldebug.c,v 2.50 2009/05/04 18:26:21 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 | */ |
@@ -103,32 +103,34 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { | |||
103 | } | 103 | } |
104 | 104 | ||
105 | 105 | ||
106 | static Proto *getluaproto (CallInfo *ci) { | 106 | static const char *findlocal (lua_State *L, CallInfo *ci, int n, |
107 | return (isLua(ci) ? ci_func(ci)->l.p : NULL); | 107 | StkId *pos) { |
108 | } | 108 | const char *name = NULL; |
109 | 109 | StkId base; | |
110 | 110 | if (isLua(ci)) { | |
111 | static const char *findlocal (lua_State *L, CallInfo *ci, int n) { | 111 | base = ci->u.l.base; |
112 | const char *name; | 112 | name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci)); |
113 | Proto *fp = getluaproto(ci); | 113 | } |
114 | if (fp && (name = luaF_getlocalname(fp, n, currentpc(ci))) != NULL) | 114 | else |
115 | return name; /* is a local variable in a Lua function */ | 115 | base = ci->func + 1; |
116 | else { | 116 | if (name == NULL) { /* no 'standard' name? */ |
117 | StkId limit = (ci == L->ci) ? L->top : ci->next->func; | 117 | StkId limit = (ci == L->ci) ? L->top : ci->next->func; |
118 | if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ | 118 | if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ |
119 | return "(*temporary)"; | 119 | name = "(*temporary)"; /* generic name for any valid slot */ |
120 | else | 120 | else return NULL; /* no name */ |
121 | return NULL; | ||
122 | } | 121 | } |
122 | *pos = base + (n - 1); | ||
123 | return name; | ||
123 | } | 124 | } |
124 | 125 | ||
125 | 126 | ||
126 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { | 127 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { |
127 | CallInfo *ci = ar->i_ci; | 128 | CallInfo *ci = ar->i_ci; |
128 | const char *name = findlocal(L, ci, n); | 129 | StkId pos; |
130 | const char *name = findlocal(L, ci, n, &pos); | ||
129 | lua_lock(L); | 131 | lua_lock(L); |
130 | if (name) { | 132 | if (name) { |
131 | setobj2s(L, L->top, ci->base + (n - 1)); | 133 | setobj2s(L, L->top, pos); |
132 | api_incr_top(L); | 134 | api_incr_top(L); |
133 | } | 135 | } |
134 | lua_unlock(L); | 136 | lua_unlock(L); |
@@ -138,10 +140,11 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { | |||
138 | 140 | ||
139 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { | 141 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { |
140 | CallInfo *ci = ar->i_ci; | 142 | CallInfo *ci = ar->i_ci; |
141 | const char *name = findlocal(L, ci, n); | 143 | StkId pos; |
144 | const char *name = findlocal(L, ci, n, &pos); | ||
142 | lua_lock(L); | 145 | lua_lock(L); |
143 | if (name) | 146 | if (name) |
144 | setobjs2s(L, ci->base + (n - 1), L->top - 1); | 147 | setobjs2s(L, pos, L->top - 1); |
145 | L->top--; /* pop value */ | 148 | L->top--; /* pop value */ |
146 | lua_unlock(L); | 149 | lua_unlock(L); |
147 | return name; | 150 | return name; |
@@ -282,8 +285,7 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, | |||
282 | Proto *p; | 285 | Proto *p; |
283 | int lastpc, pc; | 286 | int lastpc, pc; |
284 | const char *what = NULL; | 287 | const char *what = NULL; |
285 | if (!isLua(ci)) /* is not a Lua function? */ | 288 | lua_assert(isLua(ci)); |
286 | return NULL; /* cannot find name for it */ | ||
287 | p = ci_func(ci)->l.p; | 289 | p = ci_func(ci)->l.p; |
288 | lastpc = currentpc(ci); | 290 | lastpc = currentpc(ci); |
289 | *name = luaF_getlocalname(p, reg + 1, lastpc); | 291 | *name = luaF_getlocalname(p, reg + 1, lastpc); |
@@ -418,17 +420,18 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { | |||
418 | /* only ANSI way to check whether a pointer points to an array */ | 420 | /* only ANSI way to check whether a pointer points to an array */ |
419 | static int isinstack (CallInfo *ci, const TValue *o) { | 421 | static int isinstack (CallInfo *ci, const TValue *o) { |
420 | StkId p; | 422 | StkId p; |
421 | for (p = ci->base; p < ci->top; p++) | 423 | for (p = ci->u.l.base; p < ci->top; p++) |
422 | if (o == p) return 1; | 424 | if (o == p) return 1; |
423 | return 0; | 425 | return 0; |
424 | } | 426 | } |
425 | 427 | ||
426 | 428 | ||
427 | void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { | 429 | void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { |
430 | CallInfo *ci = L->ci; | ||
428 | const char *name = NULL; | 431 | const char *name = NULL; |
429 | const char *t = luaT_typenames[ttype(o)]; | 432 | const char *t = luaT_typenames[ttype(o)]; |
430 | const char *kind = (isinstack(L->ci, o)) ? | 433 | const char *kind = (isLua(ci) && isinstack(ci, o)) ? |
431 | getobjname(L, L->ci, cast_int(o - L->base), &name) : | 434 | getobjname(L, ci, cast_int(o - ci->u.l.base), &name) : |
432 | NULL; | 435 | NULL; |
433 | if (kind) | 436 | if (kind) |
434 | luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", | 437 | luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", |
@@ -469,7 +472,7 @@ static void addinfo (lua_State *L, const char *msg) { | |||
469 | if (isLua(ci)) { /* is Lua code? */ | 472 | if (isLua(ci)) { /* is Lua code? */ |
470 | char buff[LUA_IDSIZE]; /* add file:line information */ | 473 | char buff[LUA_IDSIZE]; /* add file:line information */ |
471 | int line = currentline(ci); | 474 | int line = currentline(ci); |
472 | luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); | 475 | luaO_chunkid(buff, getstr(ci_func(ci)->l.p->source), LUA_IDSIZE); |
473 | luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); | 476 | luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); |
474 | } | 477 | } |
475 | } | 478 | } |