Răsfoiți Sursa

Fix 'f' and 'L' options for debug.getinfo() and lua_getinfo().

Mike Pall 13 ani în urmă
părinte
comite
965694b0af
2 a modificat fișierele cu 58 adăugiri și 45 ștergeri
  1. 28 21
      src/lib_debug.c
  2. 30 24
      src/lj_debug.c

+ 28 - 21
src/lib_debug.c

@@ -102,7 +102,7 @@ static void treatstackoption(lua_State *L, lua_State *L1, const char *fname)
 LJLIB_CF(debug_getinfo)
 {
   lua_Debug ar;
-  int arg;
+  int arg, opt_f = 0, opt_L = 0;
   lua_State *L1 = getthread(L, &arg);
   const char *options = luaL_optstring(L, arg+2, "flnSu");
   if (lua_isnumber(L, arg+1)) {
@@ -118,27 +118,34 @@ LJLIB_CF(debug_getinfo)
   }
   if (!lua_getinfo(L1, options, &ar))
     lj_err_arg(L, arg+2, LJ_ERR_INVOPT);
-  lua_createtable(L, 0, 16);
-  if (strchr(options, 'S')) {
-    settabss(L, "source", ar.source);
-    settabss(L, "short_src", ar.short_src);
-    settabsi(L, "linedefined", ar.linedefined);
-    settabsi(L, "lastlinedefined", ar.lastlinedefined);
-    settabss(L, "what", ar.what);
-  }
-  if (strchr(options, 'l'))
-    settabsi(L, "currentline", ar.currentline);
-  if (strchr(options, 'u'))
-    settabsi(L, "nups", ar.nups);
-  if (strchr(options, 'n')) {
-    settabss(L, "name", ar.name);
-    settabss(L, "namewhat", ar.namewhat);
+  lua_createtable(L, 0, 16);  /* Create result table. */
+  for (; *options; options++) {
+    switch (*options) {
+    case 'S':
+      settabss(L, "source", ar.source);
+      settabss(L, "short_src", ar.short_src);
+      settabsi(L, "linedefined", ar.linedefined);
+      settabsi(L, "lastlinedefined", ar.lastlinedefined);
+      settabss(L, "what", ar.what);
+      break;
+    case 'l':
+      settabsi(L, "currentline", ar.currentline);
+      break;
+    case 'u':
+      settabsi(L, "nups", ar.nups);
+      break;
+    case 'n':
+      settabss(L, "name", ar.name);
+      settabss(L, "namewhat", ar.namewhat);
+      break;
+    case 'f': opt_f = 1; break;
+    case 'L': opt_L = 1; break;
+    default: break;
+    }
   }
-  if (strchr(options, 'L'))
-    treatstackoption(L, L1, "activelines");
-  if (strchr(options, 'f'))
-    treatstackoption(L, L1, "func");
-  return 1;  /* return table */
+  if (opt_L) treatstackoption(L, L1, "activelines");
+  if (opt_f) treatstackoption(L, L1, "func");
+  return 1;  /* Return result table. */
 }
 
 LJLIB_CF(debug_getlocal)

+ 30 - 24
src/lj_debug.c

@@ -425,7 +425,7 @@ LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n)
 
 LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
 {
-  int status = 1;
+  int opt_f = 0, opt_L = 0;
   TValue *frame = NULL;
   TValue *nextframe = NULL;
   GCfunc *fn;
@@ -478,35 +478,41 @@ LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar)
 	ar->name = NULL;
       }
     } else if (*what == 'f') {
-      setfuncV(L, L->top, fn);
-      incr_top(L);
+      opt_f = 1;
     } else if (*what == 'L') {
-      if (isluafunc(fn)) {
-	GCtab *t = lj_tab_new(L, 0, 0);
-	GCproto *pt = funcproto(fn);
-	const void *lineinfo = proto_lineinfo(pt);
-	if (lineinfo) {
-	  BCLine first = pt->firstline;
-	  int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4;
-	  MSize i, szl = pt->sizebc-1;
-	  for (i = 0; i < szl; i++) {
-	    BCLine line = first +
-	      (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] :
-	       sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] :
-	       (BCLine)((const uint32_t *)lineinfo)[i]);
-	    setboolV(lj_tab_setint(L, t, line), 1);
-	  }
+      opt_L = 1;
+    } else {
+      return 0;  /* Bad option. */
+    }
+  }
+  if (opt_f) {
+    setfuncV(L, L->top, fn);
+    incr_top(L);
+  }
+  if (opt_L) {
+    if (isluafunc(fn)) {
+      GCtab *t = lj_tab_new(L, 0, 0);
+      GCproto *pt = funcproto(fn);
+      const void *lineinfo = proto_lineinfo(pt);
+      if (lineinfo) {
+	BCLine first = pt->firstline;
+	int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4;
+	MSize i, szl = pt->sizebc-1;
+	for (i = 0; i < szl; i++) {
+	  BCLine line = first +
+	    (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] :
+	     sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] :
+	     (BCLine)((const uint32_t *)lineinfo)[i]);
+	  setboolV(lj_tab_setint(L, t, line), 1);
 	}
-	settabV(L, L->top, t);
-      } else {
-	setnilV(L->top);
       }
-      incr_top(L);
+      settabV(L, L->top, t);
     } else {
-      status = 0;  /* Bad option. */
+      setnilV(L->top);
     }
+    incr_top(L);
   }
-  return status;
+  return 1;  /* Ok. */
 }
 
 LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar)