2 ** $Id: lundump.c,v 2.7.1.2 2008/01/18 16:39:11 roberto Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
30 #ifdef LUAC_TRUST_BINARIES
34 #define IF(c,s) if (c) error(S,s)
36 static void error(LoadState* S, const char* why)
38 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
39 luaD_throw(S->L,LUA_ERRSYNTAX);
43 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
44 #define LoadByte(S) (lu_byte)LoadChar(S)
45 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
46 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
48 static void LoadBlock(LoadState* S, void* b, size_t size)
50 size_t r=luaZ_read(S->Z,b,size);
52 IF (r!=0, "unexpected end");
55 static int LoadChar(LoadState* S)
62 static int LoadInt(LoadState* S)
66 IF (x<0, "bad integer");
70 static lua_Number LoadNumber(LoadState* S)
77 static TString* LoadString(LoadState* S)
85 char* s=luaZ_openspace(S->L,S->b,size);
87 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
91 static void LoadCode(LoadState* S, Proto* f)
94 f->code=luaM_newvector(S->L,n,Instruction);
96 LoadVector(S,f->code,n,sizeof(Instruction));
99 static Proto* LoadFunction(LoadState* S, TString* p);
101 static void LoadConstants(LoadState* S, Proto* f)
105 f->k=luaM_newvector(S->L,n,TValue);
107 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
118 setbvalue(o,LoadChar(S));
121 setnvalue(o,LoadNumber(S));
124 setsvalue2n(S->L,o,LoadString(S));
127 error(S,"bad constant");
132 f->p=luaM_newvector(S->L,n,Proto*);
134 for (i=0; i<n; i++) f->p[i]=NULL;
135 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
138 static void LoadDebug(LoadState* S, Proto* f)
142 f->lineinfo=luaM_newvector(S->L,n,int);
144 LoadVector(S,f->lineinfo,n,sizeof(int));
146 f->locvars=luaM_newvector(S->L,n,LocVar);
148 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
151 f->locvars[i].varname=LoadString(S);
152 f->locvars[i].startpc=LoadInt(S);
153 f->locvars[i].endpc=LoadInt(S);
156 f->upvalues=luaM_newvector(S->L,n,TString*);
158 for (i=0; i<n; i++) f->upvalues[i]=NULL;
159 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
162 static Proto* LoadFunction(LoadState* S, TString* p)
164 Proto* f=luaF_newproto(S->L);
165 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
166 f->source=LoadString(S); if (f->source==NULL) f->source=p;
167 f->linedefined=LoadInt(S);
168 f->lastlinedefined=LoadInt(S);
170 f->numparams=LoadByte(S);
171 f->is_vararg=LoadByte(S);
172 f->maxstacksize=LoadByte(S);
176 IF (!luaG_checkcode(f), "bad code");
181 static void LoadHeader(LoadState* S)
183 char h[LUAC_HEADERSIZE];
184 char s[LUAC_HEADERSIZE];
186 LoadBlock(S,s,LUAC_HEADERSIZE);
187 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
191 ** load precompiled chunk
193 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
196 if (*name=='@' || *name=='=')
198 else if (*name==LUA_SIGNATURE[0])
199 S.name="binary string";
206 return LoadFunction(&S,luaS_newliteral(L,"=?"));
212 void luaU_header (char* h)
215 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
216 h+=sizeof(LUA_SIGNATURE)-1;
217 *h++=(char)LUAC_VERSION;
218 *h++=(char)LUAC_FORMAT;
219 *h++=(char)*(char*)&x; /* endianness */
220 *h++=(char)sizeof(int);
221 *h++=(char)sizeof(size_t);
222 *h++=(char)sizeof(Instruction);
223 *h++=(char)sizeof(lua_Number);
224 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */