فهرست منبع

allows different 'source' for each prototype, but inherits it from
parent when they are equal (only possible case for chunks created
by the parser)

Roberto Ierusalimschy 11 سال پیش
والد
کامیت
7021cc9bc8
2فایلهای تغییر یافته به همراه34 افزوده شده و 21 حذف شده
  1. 17 10
      ldump.c
  2. 17 11
      lundump.c

+ 17 - 10
ldump.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldump.c,v 2.30 2014/06/18 13:21:12 roberto Exp roberto $
+** $Id: ldump.c,v 2.31 2014/06/18 13:54:31 roberto Exp roberto $
 ** save precompiled Lua chunks
 ** See Copyright Notice in lua.h
 */
@@ -89,7 +89,7 @@ static void DumpCode (const Proto *f, DumpState *D) {
 }
 
 
-static void DumpFunction(const Proto *f, DumpState *D);
+static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
 
 static void DumpConstants (const Proto *f, DumpState *D) {
   int i;
@@ -118,12 +118,15 @@ static void DumpConstants (const Proto *f, DumpState *D) {
       lua_assert(0);
     }
   }
-  n = f->sizep;
+}
+
+
+static void DumpProtos (const Proto *f, DumpState *D) {
+  int i;
+  int n = f->sizep;
   DumpInt(n, D);
-  for (i = 0; i < n; i++) {
-    lua_assert(f->source == f->p[i]->source);  /* same source for all protos */
-    DumpFunction(f->p[i], D);
-  }
+  for (i = 0; i < n; i++)
+    DumpFunction(f->p[i], f->source, D);
 }
 
 
@@ -156,7 +159,11 @@ static void DumpDebug (const Proto *f, DumpState *D) {
 }
 
 
-static void DumpFunction (const Proto *f, DumpState *D) {
+static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
+  if (D->strip || f->source == psource)
+    DumpString(NULL, D);  /* no debug info or same source as its parent */
+  else
+    DumpString(f->source, D);
   DumpInt(f->linedefined, D);
   DumpInt(f->lastlinedefined, D);
   DumpByte(f->numparams, D);
@@ -165,6 +172,7 @@ static void DumpFunction (const Proto *f, DumpState *D) {
   DumpCode(f, D);
   DumpConstants(f, D);
   DumpUpvalues(f, D);
+  DumpProtos(f, D);
   DumpDebug(f, D);
 }
 
@@ -197,8 +205,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
   D.status = 0;
   DumpHeader(&D);
   DumpByte(f->sizeupvalues, &D);
-  DumpString((D.strip) ? NULL : f->source, &D);
-  DumpFunction(f, &D);
+  DumpFunction(f, NULL, &D);
   return D.status;
 }
 

+ 17 - 11
lundump.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lundump.c,v 2.37 2014/04/29 18:14:16 roberto Exp roberto $
+** $Id: lundump.c,v 2.38 2014/06/18 13:19:17 roberto Exp roberto $
 ** load precompiled Lua chunks
 ** See Copyright Notice in lua.h
 */
@@ -31,7 +31,6 @@ typedef struct {
   ZIO *Z;
   Mbuffer *b;
   const char *name;
-  TString *source;  /* source (the same for all prototypes) */
 } LoadState;
 
 
@@ -106,12 +105,12 @@ static void LoadCode (LoadState *S, Proto *f) {
 }
 
 
-static void LoadFunction(LoadState *S, Proto *f);
+static void LoadFunction(LoadState *S, Proto *f, TString *psource);
 
 
 static void LoadConstants (LoadState *S, Proto *f) {
-  int i, n;
-  n = LoadInt(S);
+  int i;
+  int n = LoadInt(S);
   f->k = luaM_newvector(S->L, n, TValue);
   f->sizek = n;
   for (i = 0; i < n; i++)
@@ -140,14 +139,19 @@ static void LoadConstants (LoadState *S, Proto *f) {
       lua_assert(0);
     }
   }
-  n = LoadInt(S);
+}
+
+
+static void LoadProtos (LoadState *S, Proto *f) {
+  int i;
+  int n = LoadInt(S);
   f->p = luaM_newvector(S->L, n, Proto *);
   f->sizep = n;
   for (i = 0; i < n; i++)
     f->p[i] = NULL;
   for (i = 0; i < n; i++) {
     f->p[i] = luaF_newproto(S->L);
-    LoadFunction(S, f->p[i]);
+    LoadFunction(S, f->p[i], f->source);
   }
 }
 
@@ -168,7 +172,6 @@ static void LoadUpvalues (LoadState *S, Proto *f) {
 
 static void LoadDebug (LoadState *S, Proto *f) {
   int i, n;
-  f->source = S->source;
   n = LoadInt(S);
   f->lineinfo = luaM_newvector(S->L, n, int);
   f->sizelineinfo = n;
@@ -189,7 +192,10 @@ static void LoadDebug (LoadState *S, Proto *f) {
 }
 
 
-static void LoadFunction (LoadState *S, Proto *f) {
+static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
+  f->source = LoadString(S);
+  if (f->source == NULL)  /* no source in dump? */
+    f->source = psource;  /* reuse parent's source */
   f->linedefined = LoadInt(S);
   f->lastlinedefined = LoadInt(S);
   f->numparams = LoadByte(S);
@@ -198,6 +204,7 @@ static void LoadFunction (LoadState *S, Proto *f) {
   LoadCode(S, f);
   LoadConstants(S, f);
   LoadUpvalues(S, f);
+  LoadProtos(S, f);
   LoadDebug(S, f);
 }
 
@@ -259,8 +266,7 @@ Closure *luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff,
   setclLvalue(L, L->top, cl);
   incr_top(L);
   cl->l.p = luaF_newproto(L);
-  S.source = cl->l.p->source = LoadString(&S);  /* read source */
-  LoadFunction(&S, cl->l.p);
+  LoadFunction(&S, cl->l.p, NULL);
   lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
   luai_verifycode(L, buff, cl->l.p);
   return cl;