2 ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 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);
51 IF (r!=0, "unexpected end");
54 static int LoadChar(LoadState* S)
61 static int LoadInt(LoadState* S)
65 IF (x<0, "bad integer");
69 static lua_Number LoadNumber(LoadState* S)
77 static lua_Integer LoadInteger(LoadState* S)
85 static TString* LoadString(LoadState* S)
93 char* s=luaZ_openspace(S->L,S->b,size);
95 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
99 static void LoadCode(LoadState* S, Proto* f)
102 f->code=luaM_newvector(S->L,n,Instruction);
104 LoadVector(S,f->code,n,sizeof(Instruction));
107 static Proto* LoadFunction(LoadState* S, TString* p);
109 static void LoadConstants(LoadState* S, Proto* f)
113 f->k=luaM_newvector(S->L,n,TValue);
115 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
126 setbvalue(o,LoadChar(S)!=0);
129 setnvalue(o,LoadNumber(S));
132 case LUA_TINT: /* Integer type saved in bytecode (see lcode.c) */
133 setivalue(o,LoadInteger(S));
137 setsvalue2n(S->L,o,LoadString(S));
140 error(S,"bad constant");
145 f->p=luaM_newvector(S->L,n,Proto*);
147 for (i=0; i<n; i++) f->p[i]=NULL;
148 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
151 static void LoadDebug(LoadState* S, Proto* f)
155 f->lineinfo=luaM_newvector(S->L,n,int);
157 LoadVector(S,f->lineinfo,n,sizeof(int));
159 f->locvars=luaM_newvector(S->L,n,LocVar);
161 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
164 f->locvars[i].varname=LoadString(S);
165 f->locvars[i].startpc=LoadInt(S);
166 f->locvars[i].endpc=LoadInt(S);
169 f->upvalues=luaM_newvector(S->L,n,TString*);
171 for (i=0; i<n; i++) f->upvalues[i]=NULL;
172 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
175 static Proto* LoadFunction(LoadState* S, TString* p)
178 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
179 f=luaF_newproto(S->L);
180 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
181 f->source=LoadString(S); if (f->source==NULL) f->source=p;
182 f->linedefined=LoadInt(S);
183 f->lastlinedefined=LoadInt(S);
185 f->numparams=LoadByte(S);
186 f->is_vararg=LoadByte(S);
187 f->maxstacksize=LoadByte(S);
191 IF (!luaG_checkcode(f), "bad code");
197 static void LoadHeader(LoadState* S)
199 char h[LUAC_HEADERSIZE];
200 char s[LUAC_HEADERSIZE];
202 LoadBlock(S,s,LUAC_HEADERSIZE);
203 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
207 ** load precompiled chunk
209 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
212 if (*name=='@' || *name=='=')
214 else if (*name==LUA_SIGNATURE[0])
215 S.name="binary string";
222 return LoadFunction(&S,luaS_newliteral(L,"=?"));
228 void luaU_header (char* h)
231 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
232 h+=sizeof(LUA_SIGNATURE)-1;
233 *h++=(char)LUAC_VERSION;
234 *h++=(char)LUAC_FORMAT;
235 *h++=(char)*(char*)&x; /* endianness */
236 *h++=(char)sizeof(int);
237 *h++=(char)sizeof(size_t);
238 *h++=(char)sizeof(Instruction);
239 *h++=(char)sizeof(lua_Number);
242 * Last byte of header (0/1 in unpatched Lua 5.1.3):
244 * 0: lua_Number is float/double/ldouble (nonpatched only)
245 * 1: lua_Number is integer (nonpatched only)
246 * 4: LNUM_INT32: sizeof(lua_Integer)
247 * 8: LNUM_INT64: sizeof(lua_Integer)
248 * +0x80: LNUM_COMPLEX
250 *h++ = (char)( sizeof(lua_Integer)