Browse Source

version from lhf

Roberto Ierusalimschy 14 năm trước cách đây
mục cha
commit
1475cb59bf
2 tập tin đã thay đổi với 20 bổ sung35 xóa
  1. 6 9
      ldump.c
  2. 14 26
      lundump.c

+ 6 - 9
ldump.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $
+** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $
 ** save precompiled Lua chunks
 ** See Copyright Notice in lua.h
 */
@@ -75,7 +75,7 @@ static void DumpString(const TString* s, DumpState* D)
 
 #define DumpCode(f,D)	 DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
 
-static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
+static void DumpFunction(const Proto* f, DumpState* D);
 
 static void DumpConstants(const Proto* f, DumpState* D)
 {
@@ -98,14 +98,11 @@ static void DumpConstants(const Proto* f, DumpState* D)
    case LUA_TSTRING:
 	DumpString(rawtsvalue(o),D);
 	break;
-   default:
-	lua_assert(0);			/* cannot happen */
-	break;
   }
  }
  n=f->sizep;
  DumpInt(n,D);
- for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
+ for (i=0; i<n; i++) DumpFunction(f->p[i],D);
 }
 
 static void DumpUpvalues(const Proto* f, DumpState* D)
@@ -122,6 +119,7 @@ static void DumpUpvalues(const Proto* f, DumpState* D)
 static void DumpDebug(const Proto* f, DumpState* D)
 {
  int i,n;
+ DumpString((D->strip) ? NULL : f->source,D);
  n= (D->strip) ? 0 : f->sizelineinfo;
  DumpVector(f->lineinfo,n,sizeof(int),D);
  n= (D->strip) ? 0 : f->sizelocvars;
@@ -137,9 +135,8 @@ static void DumpDebug(const Proto* f, DumpState* D)
  for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
 }
 
-static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
+static void DumpFunction(const Proto* f, DumpState* D)
 {
- DumpString((f->source==p || D->strip) ? NULL : f->source,D);
  DumpInt(f->linedefined,D);
  DumpInt(f->lastlinedefined,D);
  DumpChar(f->numparams,D);
@@ -170,6 +167,6 @@ int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip
  D.strip=strip;
  D.status=0;
  DumpHeader(&D);
- DumpFunction(f,NULL,&D);
+ DumpFunction(f,&D);
  return D.status;
 }

+ 14 - 26
lundump.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lundump.c,v 2.12 2009/09/30 15:38:37 roberto Exp roberto $
+** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp $
 ** load precompiled Lua chunks
 ** See Copyright Notice in lua.h
 */
@@ -27,27 +27,20 @@ typedef struct {
  const char* name;
 } LoadState;
 
-#ifdef LUAC_TRUST_BINARIES
-#define IF(c,s)
-#else
-#define IF(c,s)		if (c) error(S,s)
-
 static void error(LoadState* S, const char* why)
 {
- luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
+ luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
  luaD_throw(S->L,LUA_ERRSYNTAX);
 }
-#endif
 
 #define LoadMem(S,b,n,size)	LoadBlock(S,b,(n)*(size))
-#define	LoadByte(S)		(lu_byte)LoadChar(S)
+#define LoadByte(S)		(lu_byte)LoadChar(S)
 #define LoadVar(S,x)		LoadMem(S,&x,1,sizeof(x))
 #define LoadVector(S,b,n,size)	LoadMem(S,b,n,size)
 
 static void LoadBlock(LoadState* S, void* b, size_t size)
 {
- size_t r=luaZ_read(S->Z,b,size);
- IF (r!=0, "unexpected end");
+ if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
 }
 
 static int LoadChar(LoadState* S)
@@ -61,7 +54,6 @@ static int LoadInt(LoadState* S)
 {
  int x;
  LoadVar(S,x);
- IF (x<0, "bad integer");
  return x;
 }
 
@@ -94,7 +86,7 @@ static void LoadCode(LoadState* S, Proto* f)
  LoadVector(S,f->code,n,sizeof(Instruction));
 }
 
-static Proto* LoadFunction(LoadState* S, TString* p);
+static Proto* LoadFunction(LoadState* S);
 
 static void LoadConstants(LoadState* S, Proto* f)
 {
@@ -113,7 +105,7 @@ static void LoadConstants(LoadState* S, Proto* f)
 	setnilvalue(o);
 	break;
    case LUA_TBOOLEAN:
-	setbvalue(o,LoadChar(S)!=0);
+	setbvalue(o,LoadChar(S));
 	break;
    case LUA_TNUMBER:
 	setnvalue(o,LoadNumber(S));
@@ -121,16 +113,13 @@ static void LoadConstants(LoadState* S, Proto* f)
    case LUA_TSTRING:
 	setsvalue2n(S->L,o,LoadString(S));
 	break;
-   default:
-	IF (1, "bad constant");
-	break;
   }
  }
  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]=LoadFunction(S,f->source);
+ for (i=0; i<n; i++) f->p[i]=LoadFunction(S);
 }
 
 static void LoadUpvalues(LoadState* S, Proto* f)
@@ -150,6 +139,7 @@ static void LoadUpvalues(LoadState* S, Proto* f)
 static void LoadDebug(LoadState* S, Proto* f)
 {
  int i,n;
+ f->source=LoadString(S);
  n=LoadInt(S);
  f->lineinfo=luaM_newvector(S->L,n,int);
  f->sizelineinfo=n;
@@ -168,13 +158,10 @@ static void LoadDebug(LoadState* S, Proto* f)
  for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
 }
 
-static Proto* LoadFunction(LoadState* S, TString* p)
+static Proto* LoadFunction(LoadState* S)
 {
- Proto* f;
- if (++G(S->L)->nCcalls > LUAI_MAXCCALLS) error(S, "function nest too deep");
- f=luaF_newproto(S->L);
+ Proto* f=luaF_newproto(S->L);
  setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
- f->source=LoadString(S); if (f->source==NULL) f->source=p;
  f->linedefined=LoadInt(S);
  f->lastlinedefined=LoadInt(S);
  f->numparams=LoadByte(S);
@@ -185,7 +172,6 @@ static Proto* LoadFunction(LoadState* S, TString* p)
  LoadUpvalues(S,f);
  LoadDebug(S,f);
  S->L->top--;
- G(S->L)->nCcalls--;
  return f;
 }
 
@@ -195,7 +181,7 @@ static void LoadHeader(LoadState* S)
  char s[LUAC_HEADERSIZE];
  luaU_header(h);
  LoadBlock(S,s,LUAC_HEADERSIZE);
- IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
+ if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible");
 }
 
 /*
@@ -214,11 +200,13 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  S.Z=Z;
  S.b=buff;
  LoadHeader(&S);
- return LoadFunction(&S,luaS_newliteral(L,"=?"));
+ return LoadFunction(&S);
 }
 
 /*
 * make header
+* if you make any changes in the header or in LUA_SIGNATURE,
+* be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
 */
 void luaU_header (char* h)
 {