version from lhf

This commit is contained in:
Roberto Ierusalimschy
2010-10-25 12:33:38 -02:00
parent 9e8e60dd5f
commit 1475cb59bf
2 changed files with 20 additions and 35 deletions

15
ldump.c
View File

@@ -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 ** save precompiled Lua chunks
** See Copyright Notice in lua.h ** 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) #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) static void DumpConstants(const Proto* f, DumpState* D)
{ {
@@ -98,14 +98,11 @@ static void DumpConstants(const Proto* f, DumpState* D)
case LUA_TSTRING: case LUA_TSTRING:
DumpString(rawtsvalue(o),D); DumpString(rawtsvalue(o),D);
break; break;
default:
lua_assert(0); /* cannot happen */
break;
} }
} }
n=f->sizep; n=f->sizep;
DumpInt(n,D); 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) 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) static void DumpDebug(const Proto* f, DumpState* D)
{ {
int i,n; int i,n;
DumpString((D->strip) ? NULL : f->source,D);
n= (D->strip) ? 0 : f->sizelineinfo; n= (D->strip) ? 0 : f->sizelineinfo;
DumpVector(f->lineinfo,n,sizeof(int),D); DumpVector(f->lineinfo,n,sizeof(int),D);
n= (D->strip) ? 0 : f->sizelocvars; 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); 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->linedefined,D);
DumpInt(f->lastlinedefined,D); DumpInt(f->lastlinedefined,D);
DumpChar(f->numparams,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.strip=strip;
D.status=0; D.status=0;
DumpHeader(&D); DumpHeader(&D);
DumpFunction(f,NULL,&D); DumpFunction(f,&D);
return D.status; return D.status;
} }

View File

@@ -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 ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -27,27 +27,20 @@ typedef struct {
const char* name; const char* name;
} LoadState; } 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) 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); luaD_throw(S->L,LUA_ERRSYNTAX);
} }
#endif
#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) #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 LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
static void LoadBlock(LoadState* S, void* b, size_t size) static void LoadBlock(LoadState* S, void* b, size_t size)
{ {
size_t r=luaZ_read(S->Z,b,size); if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
IF (r!=0, "unexpected end");
} }
static int LoadChar(LoadState* S) static int LoadChar(LoadState* S)
@@ -61,7 +54,6 @@ static int LoadInt(LoadState* S)
{ {
int x; int x;
LoadVar(S,x); LoadVar(S,x);
IF (x<0, "bad integer");
return x; return x;
} }
@@ -94,7 +86,7 @@ static void LoadCode(LoadState* S, Proto* f)
LoadVector(S,f->code,n,sizeof(Instruction)); 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) static void LoadConstants(LoadState* S, Proto* f)
{ {
@@ -113,7 +105,7 @@ static void LoadConstants(LoadState* S, Proto* f)
setnilvalue(o); setnilvalue(o);
break; break;
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
setbvalue(o,LoadChar(S)!=0); setbvalue(o,LoadChar(S));
break; break;
case LUA_TNUMBER: case LUA_TNUMBER:
setnvalue(o,LoadNumber(S)); setnvalue(o,LoadNumber(S));
@@ -121,16 +113,13 @@ static void LoadConstants(LoadState* S, Proto* f)
case LUA_TSTRING: case LUA_TSTRING:
setsvalue2n(S->L,o,LoadString(S)); setsvalue2n(S->L,o,LoadString(S));
break; break;
default:
IF (1, "bad constant");
break;
} }
} }
n=LoadInt(S); n=LoadInt(S);
f->p=luaM_newvector(S->L,n,Proto*); f->p=luaM_newvector(S->L,n,Proto*);
f->sizep=n; f->sizep=n;
for (i=0; i<n; i++) f->p[i]=NULL; 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) 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) static void LoadDebug(LoadState* S, Proto* f)
{ {
int i,n; int i,n;
f->source=LoadString(S);
n=LoadInt(S); n=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,n,int); f->lineinfo=luaM_newvector(S->L,n,int);
f->sizelineinfo=n; 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); 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; Proto* f=luaF_newproto(S->L);
if (++G(S->L)->nCcalls > LUAI_MAXCCALLS) error(S, "function nest too deep");
f=luaF_newproto(S->L);
setptvalue2s(S->L,S->L->top,f); incr_top(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->linedefined=LoadInt(S);
f->lastlinedefined=LoadInt(S); f->lastlinedefined=LoadInt(S);
f->numparams=LoadByte(S); f->numparams=LoadByte(S);
@@ -185,7 +172,6 @@ static Proto* LoadFunction(LoadState* S, TString* p)
LoadUpvalues(S,f); LoadUpvalues(S,f);
LoadDebug(S,f); LoadDebug(S,f);
S->L->top--; S->L->top--;
G(S->L)->nCcalls--;
return f; return f;
} }
@@ -195,7 +181,7 @@ static void LoadHeader(LoadState* S)
char s[LUAC_HEADERSIZE]; char s[LUAC_HEADERSIZE];
luaU_header(h); luaU_header(h);
LoadBlock(S,s,LUAC_HEADERSIZE); 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.Z=Z;
S.b=buff; S.b=buff;
LoadHeader(&S); LoadHeader(&S);
return LoadFunction(&S,luaS_newliteral(L,"=?")); return LoadFunction(&S);
} }
/* /*
* make header * 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) void luaU_header (char* h)
{ {