diff options
Diffstat (limited to 'src/lundump.c')
-rw-r--r-- | src/lundump.c | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/src/lundump.c b/src/lundump.c index 151a8507..d79dd898 100644 --- a/src/lundump.c +++ b/src/lundump.c @@ -1,14 +1,18 @@ /* -** $Id: lundump.c,v 1.49 2003/04/07 20:34:20 lhf Exp $ +** $Id: lundump.c,v 1.51 2004/03/24 00:25:08 lhf Exp $ ** load pre-compiled Lua chunks ** See Copyright Notice in lua.h */ +#include <stdarg.h> +#include <stddef.h> + #define lundump_c #include "lua.h" #include "ldebug.h" +#include "ldo.h" #include "lfunc.h" #include "lmem.h" #include "lopcodes.h" @@ -26,22 +30,28 @@ typedef struct { const char* name; } LoadState; -static void unexpectedEOZ (LoadState* S) +static void error (LoadState* S, const char* fmt, ...) { - luaG_runerror(S->L,"unexpected end of file in %s",S->name); + const char *msg; + va_list argp; + va_start(argp,fmt); + msg=luaO_pushvfstring(S->L,fmt,argp); + va_end(argp); + luaO_pushfstring(S->L,"%s: %s",S->name,msg); + luaD_throw(S->L,LUA_ERRSYNTAX); } static int ezgetc (LoadState* S) { int c=zgetc(S->Z); - if (c==EOZ) unexpectedEOZ(S); + if (c==EOZ) error(S,"unexpected end of file"); return c; } static void ezread (LoadState* S, void* b, int n) { int r=luaZ_read(S->Z,b,n); - if (r!=0) unexpectedEOZ(S); + if (r!=0) error(S,"unexpected end of file"); } static void LoadBlock (LoadState* S, void* b, size_t size) @@ -77,7 +87,7 @@ static int LoadInt (LoadState* S) { int x; LoadBlock(S,&x,sizeof(x)); - if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name); + if (x<0) error(S,"bad integer"); return x; } @@ -113,7 +123,7 @@ static void LoadCode (LoadState* S, Proto* f) int size=LoadInt(S); f->code=luaM_newvector(S->L,size,Instruction); f->sizecode=size; - LoadVector(S,f->code,size,sizeof(*f->code)); + LoadVector(S,f->code,size,sizeof(Instruction)); } static void LoadLocals (LoadState* S, Proto* f) @@ -122,6 +132,7 @@ static void LoadLocals (LoadState* S, Proto* f) n=LoadInt(S); f->locvars=luaM_newvector(S->L,n,LocVar); f->sizelocvars=n; + for (i=0; i<n; i++) f->locvars[i].varname=NULL; for (i=0; i<n; i++) { f->locvars[i].varname=LoadString(S); @@ -135,18 +146,18 @@ static void LoadLines (LoadState* S, Proto* f) int size=LoadInt(S); f->lineinfo=luaM_newvector(S->L,size,int); f->sizelineinfo=size; - LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo)); + LoadVector(S,f->lineinfo,size,sizeof(int)); } static void LoadUpvalues (LoadState* S, Proto* f) { int i,n; n=LoadInt(S); - if (n!=0 && n!=f->nups) - luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d", - S->name,n,f->nups); + if (n!=0 && n!=f->nups) + error(S,"bad nupvalues (read %d; expected %d)",n,f->nups); f->upvalues=luaM_newvector(S->L,n,TString*); f->sizeupvalues=n; + for (i=0; i<n; i++) f->upvalues[i]=NULL; for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); } @@ -156,11 +167,12 @@ static void LoadConstants (LoadState* S, Proto* f) { int i,n; n=LoadInt(S); - f->k=luaM_newvector(S->L,n,TObject); + f->k=luaM_newvector(S->L,n,TValue); f->sizek=n; + for (i=0; i<n; i++) setnilvalue(&f->k[i]); for (i=0; i<n; i++) { - TObject* o=&f->k[i]; + TValue* o=&f->k[i]; int t=LoadByte(S); switch (t) { @@ -168,25 +180,27 @@ static void LoadConstants (LoadState* S, Proto* f) setnvalue(o,LoadNumber(S)); break; case LUA_TSTRING: - setsvalue2n(o,LoadString(S)); + setsvalue2n(S->L,o,LoadString(S)); break; case LUA_TNIL: setnilvalue(o); break; default: - luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name); + error(S,"bad constant type (%d)",t); 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); } static Proto* LoadFunction (LoadState* S, TString* p) { 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->nups=LoadByte(S); @@ -199,8 +213,9 @@ static Proto* LoadFunction (LoadState* S, TString* p) LoadConstants(S,f); LoadCode(S,f); #ifndef TRUST_BINARIES - if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name); + if (!luaG_checkcode(f)) error(S,"bad code"); #endif + S->L->top--; return f; } @@ -209,18 +224,16 @@ static void LoadSignature (LoadState* S) const char* s=LUA_SIGNATURE; while (*s!=0 && ezgetc(S)==*s) ++s; - if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name); + if (*s!=0) error(S,"bad signature"); } static void TestSize (LoadState* S, int s, const char* what) { int r=LoadByte(S); if (r!=s) - luaG_runerror(S->L,"virtual machine mismatch in %s: " - "size of %s is %d but read %d",S->name,what,s,r); + error(S,"bad size of %s (read %d; expected %d)",what,r,s); } -#define TESTSIZE(s,w) TestSize(S,s,w) #define V(v) v/16,v%16 static void LoadHeader (LoadState* S) @@ -230,40 +243,27 @@ static void LoadHeader (LoadState* S) LoadSignature(S); version=LoadByte(S); if (version>VERSION) - luaG_runerror(S->L,"%s too new: " - "read version %d.%d; expected at most %d.%d", - S->name,V(version),V(VERSION)); + error(S,"bad version (read %d.%d; expected at %s %d.%d)", + V(version),"most",V(VERSION)); if (version<VERSION0) /* check last major change */ - luaG_runerror(S->L,"%s too old: " - "read version %d.%d; expected at least %d.%d", - S->name,V(version),V(VERSION0)); + error(S,"bad version (read %d.%d; expected at %s %d.%d)", + V(version),"least",V(VERSION0)); S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */ - TESTSIZE(sizeof(int),"int"); - TESTSIZE(sizeof(size_t), "size_t"); - TESTSIZE(sizeof(Instruction), "Instruction"); - TESTSIZE(SIZE_OP, "OP"); - TESTSIZE(SIZE_A, "A"); - TESTSIZE(SIZE_B, "B"); - TESTSIZE(SIZE_C, "C"); - TESTSIZE(sizeof(lua_Number), "number"); + TestSize(S,sizeof(int),"int"); + TestSize(S,sizeof(size_t),"size_t"); + TestSize(S,sizeof(Instruction),"instruction"); + TestSize(S,sizeof(lua_Number),"number"); x=LoadNumber(S); if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ - luaG_runerror(S->L,"unknown number format in %s",S->name); -} - -static Proto* LoadChunk (LoadState* S) -{ - LoadHeader(S); - return LoadFunction(S,NULL); + error(S,"unknown number format"); } /* ** load precompiled chunk */ -Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff) +Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* s) { LoadState S; - const char* s=zname(Z); if (*s=='@' || *s=='=') S.name=s+1; else if (*s==LUA_SIGNATURE[0]) @@ -273,7 +273,8 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff) S.L=L; S.Z=Z; S.b=buff; - return LoadChunk(&S); + LoadHeader(&S); + return LoadFunction(&S,NULL); } /* |