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)
76
static TString* LoadString(LoadState* S)
84
char* s=luaZ_openspace(S->L,S->b,size);
86
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
90
static void LoadCode(LoadState* S, Proto* f)
93
f->code=luaM_newvector(S->L,n,Instruction);
95
LoadVector(S,f->code,n,sizeof(Instruction));
98
static Proto* LoadFunction(LoadState* S, TString* p);
100
static void LoadConstants(LoadState* S, Proto* f)
104
f->k=luaM_newvector(S->L,n,TValue);
106
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
117
setbvalue(o,LoadChar(S)!=0);
120
setnvalue(o,LoadNumber(S));
123
setsvalue2n(S->L,o,LoadString(S));
126
error(S,"bad constant");
131
f->p=luaM_newvector(S->L,n,Proto*);
133
for (i=0; i<n; i++) f->p[i]=NULL;
134
for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
137
static void LoadDebug(LoadState* S, Proto* f)
141
f->lineinfo=luaM_newvector(S->L,n,int);
143
LoadVector(S,f->lineinfo,n,sizeof(int));
145
f->locvars=luaM_newvector(S->L,n,LocVar);
147
for (i=0; i<n; i++) f->locvars[i].varname=NULL;
150
f->locvars[i].varname=LoadString(S);
151
f->locvars[i].startpc=LoadInt(S);
152
f->locvars[i].endpc=LoadInt(S);
155
f->upvalues=luaM_newvector(S->L,n,TString*);
157
for (i=0; i<n; i++) f->upvalues[i]=NULL;
158
for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
161
static Proto* LoadFunction(LoadState* S, TString* p)
164
if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
165
f=luaF_newproto(S->L);
166
setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
167
f->source=LoadString(S); if (f->source==NULL) f->source=p;
168
f->linedefined=LoadInt(S);
169
f->lastlinedefined=LoadInt(S);
171
f->numparams=LoadByte(S);
172
f->is_vararg=LoadByte(S);
173
f->maxstacksize=LoadByte(S);
177
IF (!luaG_checkcode(f), "bad code");
183
static void LoadHeader(LoadState* S)
185
char h[LUAC_HEADERSIZE];
186
char s[LUAC_HEADERSIZE];
188
LoadBlock(S,s,LUAC_HEADERSIZE);
189
IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
193
** load precompiled chunk
195
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
198
if (*name=='@' || *name=='=')
200
else if (*name==LUA_SIGNATURE[0])
201
S.name="binary string";
208
return LoadFunction(&S,luaS_newliteral(L,"=?"));
214
void luaU_header (char* h)
217
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
218
h+=sizeof(LUA_SIGNATURE)-1;
219
*h++=(char)LUAC_VERSION;
220
*h++=(char)LUAC_FORMAT;
221
*h++=(char)*(char*)&x; /* endianness */
222
*h++=(char)sizeof(int);
223
*h++=(char)sizeof(size_t);
224
*h++=(char)sizeof(Instruction);
225
*h++=(char)sizeof(lua_Number);
226
*h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */