Forráskód Böngészése

Fixed buffers reuse absolute line information

Roberto Ierusalimschy 1 éve
szülő
commit
e7af9cdf0b
4 módosított fájl, 36 hozzáadás és 13 törlés
  1. 4 3
      ldump.c
  2. 1 1
      lfunc.c
  3. 17 9
      lundump.c
  4. 14 0
      testes/api.lua

+ 4 - 3
ldump.c

@@ -212,9 +212,10 @@ static void dumpDebug (DumpState *D, const Proto *f) {
     dumpVector(D, f->lineinfo, n);
   n = (D->strip) ? 0 : f->sizeabslineinfo;
   dumpInt(D, n);
-  for (i = 0; i < n; i++) {
-    dumpInt(D, f->abslineinfo[i].pc);
-    dumpInt(D, f->abslineinfo[i].line);
+  if (n > 0) {
+    /* 'abslineinfo' is an array of structures of int's */
+    dumpAlign(D, sizeof(int));
+    dumpVector(D, f->abslineinfo, n);
   }
   n = (D->strip) ? 0 : f->sizelocvars;
   dumpInt(D, n);

+ 1 - 1
lfunc.c

@@ -268,10 +268,10 @@ void luaF_freeproto (lua_State *L, Proto *f) {
   if (!(f->flag & PF_FIXED)) {
     luaM_freearray(L, f->code, f->sizecode);
     luaM_freearray(L, f->lineinfo, f->sizelineinfo);
+    luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
   }
   luaM_freearray(L, f->p, f->sizep);
   luaM_freearray(L, f->k, f->sizek);
-  luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
   luaM_freearray(L, f->locvars, f->sizelocvars);
   luaM_freearray(L, f->upvalues, f->sizeupvalues);
   luaM_free(L, f);

+ 17 - 9
lundump.c

@@ -36,7 +36,7 @@ typedef struct {
   ZIO *Z;
   const char *name;
   Table *h;  /* list for string reuse */
-  lu_mem offset;  /* current position relative to beginning of dump */
+  size_t offset;  /* current position relative to beginning of dump */
   lua_Integer nstr;  /* number of strings in the list */
   lu_byte fixed;  /* dump is fixed in memory */
 } LoadState;
@@ -73,8 +73,10 @@ static void loadAlign (LoadState *S, int align) {
 
 #define getaddr(S,n,t)	cast(t *, getaddr_(S,n,sizeof(t)))
 
-static const void *getaddr_ (LoadState *S, int n, int sz) {
-  const void *block = luaZ_getaddr(S->Z, n * sz);
+static const void *getaddr_ (LoadState *S, int n, size_t sz) {
+  size_t size = n * sz;
+  const void *block = luaZ_getaddr(S->Z, size);
+  S->offset += size;
   if (block == NULL)
     error(S, "truncated fixed buffer");
   return block;
@@ -143,7 +145,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) {
   TValue sv;
   size_t size = loadSize(S);
   if (size == 0) {  /* no string? */
-    *sl = NULL;
+    lua_assert(*sl == NULL);  /* must be prefilled */
     return;
   }
   else if (size == 1) {  /* previously saved string? */
@@ -287,11 +289,17 @@ static void loadDebug (LoadState *S, Proto *f) {
     loadVector(S, f->lineinfo, n);
   }
   n = loadInt(S);
-  f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
-  f->sizeabslineinfo = n;
-  for (i = 0; i < n; i++) {
-    f->abslineinfo[i].pc = loadInt(S);
-    f->abslineinfo[i].line = loadInt(S);
+  if (n > 0) {
+    loadAlign(S, sizeof(int));
+    if (S->fixed) {
+      f->abslineinfo = getaddr(S, n, AbsLineInfo);
+      f->sizeabslineinfo = n;
+    }
+    else {
+      f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
+      f->sizeabslineinfo = n;
+      loadVector(S, f->abslineinfo, n);
+    }
   }
   n = loadInt(S);
   f->locvars = luaM_newvectorchecked(S->L, n, LocVar);

+ 14 - 0
testes/api.lua

@@ -551,6 +551,20 @@ do
   assert(m2 > m1 and m2 - m1 < 350)
   X = 0; code(); assert(X == N and Y == string.rep("a", N))
   X = nil; Y = nil
+
+  -- testing debug info in fixed buffers
+  source = {"X = 0"}
+  for i = 2, 300 do source[i] = "X = X + 1" end
+  source[#source + 1] = "X = X + {}"   -- error in last line
+  source = table.concat(source, "\n")
+  source = load(source, "name1")
+  source = string.dump(source)
+  -- load dump using fixed buffer
+  local code = T.testC([[
+    loadstring 2 name B;
+    return 1
+  ]], source)
+  checkerr(":301:", code)    -- correct line information
 end