diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-11-01 14:08:52 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-11-01 14:08:52 -0200 |
| commit | 930018e273e9abaff9475c156e1367011bc437af (patch) | |
| tree | 17915d050b9a9f53923533f1bf9d8572d424b28c | |
| parent | a160266c3d2d6fabbb06e9c77e6bf5a7c8ed06c2 (diff) | |
| download | lua-930018e273e9abaff9475c156e1367011bc437af.tar.gz lua-930018e273e9abaff9475c156e1367011bc437af.tar.bz2 lua-930018e273e9abaff9475c156e1367011bc437af.zip | |
lua_getlocal/setlocal work also for C locals and temporaries
| -rw-r--r-- | ldebug.c | 51 |
1 files changed, 25 insertions, 26 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldebug.c,v 2.26 2005/08/04 13:37:38 roberto Exp roberto $ | 2 | ** $Id: ldebug.c,v 2.27 2005/10/06 20:43:44 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 | */ |
| @@ -109,40 +109,39 @@ static Proto *getluaproto (CallInfo *ci) { | |||
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | 111 | ||
| 112 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { | 112 | static const char *findlocal (lua_State *L, CallInfo *ci, int n) { |
| 113 | const char *name; | 113 | const char *name; |
| 114 | CallInfo *ci; | 114 | Proto *fp = getluaproto(ci); |
| 115 | Proto *fp; | 115 | if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) |
| 116 | lua_lock(L); | 116 | return name; /* is a local variable in a Lua function */ |
| 117 | name = NULL; | 117 | else { |
| 118 | ci = L->base_ci + ar->i_ci; | 118 | StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; |
| 119 | fp = getluaproto(ci); | 119 | if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ |
| 120 | if (fp) { /* is a Lua function? */ | 120 | return "(*temporary)"; |
| 121 | name = luaF_getlocalname(fp, n, currentpc(L, ci)); | 121 | else |
| 122 | if (name) | 122 | return NULL; |
| 123 | luaA_pushobject(L, ci->base+(n-1)); /* push value */ | ||
| 124 | } | 123 | } |
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { | ||
| 128 | CallInfo *ci = L->base_ci + ar->i_ci; | ||
| 129 | const char *name = findlocal(L, ci, n); | ||
| 130 | lua_lock(L); | ||
| 131 | if (name) | ||
| 132 | luaA_pushobject(L, ci->base + (n - 1)); | ||
| 125 | lua_unlock(L); | 133 | lua_unlock(L); |
| 126 | return name; | 134 | return name; |
| 127 | } | 135 | } |
| 128 | 136 | ||
| 129 | 137 | ||
| 130 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { | 138 | LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { |
| 131 | const char *name; | 139 | CallInfo *ci = L->base_ci + ar->i_ci; |
| 132 | CallInfo *ci; | 140 | const char *name = findlocal(L, ci, n); |
| 133 | Proto *fp; | ||
| 134 | lua_lock(L); | 141 | lua_lock(L); |
| 135 | name = NULL; | 142 | if (name) |
| 136 | ci = L->base_ci + ar->i_ci; | 143 | setobjs2s(L, ci->base + (n - 1), L->top - 1); |
| 137 | fp = getluaproto(ci); | 144 | L->top--; /* pop value */ |
| 138 | L->top--; /* pop new value */ | ||
| 139 | if (fp) { /* is a Lua function? */ | ||
| 140 | name = luaF_getlocalname(fp, n, currentpc(L, ci)); | ||
| 141 | if (!name || name[0] == '(') /* `(' starts private locals */ | ||
| 142 | name = NULL; | ||
| 143 | else | ||
| 144 | setobjs2s(L, ci->base+(n-1), L->top); | ||
| 145 | } | ||
| 146 | lua_unlock(L); | 145 | lua_unlock(L); |
| 147 | return name; | 146 | return name; |
| 148 | } | 147 | } |
