Sfoglia il codice sorgente

From Lua 5.2: debug.getlocal() accepts function arg, too.

Mike Pall 13 anni fa
parent
commit
c687d01c46
2 ha cambiato i file con 15 aggiunte e 5 eliminazioni
  1. 7 1
      src/lib_debug.c
  2. 8 4
      src/lj_debug.c

+ 7 - 1
src/lib_debug.c

@@ -145,9 +145,15 @@ LJLIB_CF(debug_getlocal)
   lua_State *L1 = getthread(L, &arg);
   lua_Debug ar;
   const char *name;
+  int slot = lj_lib_checkint(L, arg+2);
+  if (tvisfunc(L->base+arg)) {
+    L->top = L->base+arg+1;
+    lua_pushstring(L, lua_getlocal(L, NULL, slot));
+    return 1;
+  }
   if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar))
     lj_err_arg(L, arg+1, LJ_ERR_LVLRNG);
-  name = lua_getlocal(L1, &ar, lj_lib_checkint(L, arg+2));
+  name = lua_getlocal(L1, &ar, slot);
   if (name) {
     lua_xmove(L1, L, 1);
     lua_pushstring(L, name);

+ 8 - 4
src/lj_debug.c

@@ -401,10 +401,14 @@ void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc)
 LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n)
 {
   const char *name = NULL;
-  TValue *o = debug_localname(L, ar, &name, (BCReg)n);
-  if (name) {
-    copyTV(L, L->top, o);
-    incr_top(L);
+  if (ar) {
+    TValue *o = debug_localname(L, ar, &name, (BCReg)n);
+    if (name) {
+      copyTV(L, L->top, o);
+      incr_top(L);
+    }
+  } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) {
+    name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1);
   }
   return name;
 }