2 ** $Id: lundump.c,v 1.60 2006/02/16 15:53:49 lhf Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
30 #ifdef LUAC_TRUST_BINARIES
33 #define IF(c,s) if (c) error(S,s)
35 static void error(LoadState
* S
, const char* why
)
37 luaO_pushfstring(S
->L
,"%s: %s in precompiled chunk",S
->name
,why
);
38 luaD_throw(S
->L
,LUA_ERRSYNTAX
);
42 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
43 #define LoadByte(S) (lu_byte)LoadChar(S)
44 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
45 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
47 static void LoadBlock(LoadState
* S
, void* b
, size_t size
)
49 size_t r
=luaZ_read(S
->Z
,b
,size
);
50 IF (r
!=0, "unexpected end");
53 static int LoadChar(LoadState
* S
)
60 static int LoadInt(LoadState
* S
)
64 IF (x
<0, "bad integer");
68 static lua_Number
LoadNumber(LoadState
* S
)
75 static TString
* LoadString(LoadState
* S
)
83 char* s
=luaZ_openspace(S
->L
,S
->b
,size
);
85 return luaS_newlstr(S
->L
,s
,size
-1); /* remove trailing '\0' */
89 static void LoadCode(LoadState
* S
, Proto
* f
)
92 f
->code
=luaM_newvector(S
->L
,n
,Instruction
);
94 LoadVector(S
,f
->code
,n
,sizeof(Instruction
));
97 static Proto
* LoadFunction(LoadState
* S
, TString
* p
);
99 static void LoadConstants(LoadState
* S
, Proto
* f
)
103 f
->k
=luaM_newvector(S
->L
,n
,TValue
);
105 for (i
=0; i
<n
; i
++) setnilvalue(&f
->k
[i
]);
116 setbvalue(o
,LoadChar(S
));
119 setnvalue(o
,LoadNumber(S
));
122 setsvalue2n(S
->L
,o
,LoadString(S
));
125 IF (1, "bad constant");
130 f
->p
=luaM_newvector(S
->L
,n
,Proto
*);
132 for (i
=0; i
<n
; i
++) f
->p
[i
]=NULL
;
133 for (i
=0; i
<n
; i
++) f
->p
[i
]=LoadFunction(S
,f
->source
);
136 static void LoadDebug(LoadState
* S
, Proto
* f
)
140 f
->lineinfo
=luaM_newvector(S
->L
,n
,int);
142 LoadVector(S
,f
->lineinfo
,n
,sizeof(int));
144 f
->locvars
=luaM_newvector(S
->L
,n
,LocVar
);
146 for (i
=0; i
<n
; i
++) f
->locvars
[i
].varname
=NULL
;
149 f
->locvars
[i
].varname
=LoadString(S
);
150 f
->locvars
[i
].startpc
=LoadInt(S
);
151 f
->locvars
[i
].endpc
=LoadInt(S
);
154 f
->upvalues
=luaM_newvector(S
->L
,n
,TString
*);
156 for (i
=0; i
<n
; i
++) f
->upvalues
[i
]=NULL
;
157 for (i
=0; i
<n
; i
++) f
->upvalues
[i
]=LoadString(S
);
160 static Proto
* LoadFunction(LoadState
* S
, TString
* p
)
162 Proto
* f
=luaF_newproto(S
->L
);
163 setptvalue2s(S
->L
,S
->L
->top
,f
); incr_top(S
->L
);
164 f
->source
=LoadString(S
); if (f
->source
==NULL
) f
->source
=p
;
165 f
->linedefined
=LoadInt(S
);
166 f
->lastlinedefined
=LoadInt(S
);
168 f
->numparams
=LoadByte(S
);
169 f
->is_vararg
=LoadByte(S
);
170 f
->maxstacksize
=LoadByte(S
);
174 IF (!luaG_checkcode(f
), "bad code");
179 static void LoadHeader(LoadState
* S
)
181 char h
[LUAC_HEADERSIZE
];
182 char s
[LUAC_HEADERSIZE
];
184 LoadBlock(S
,s
,LUAC_HEADERSIZE
);
185 IF (memcmp(h
,s
,LUAC_HEADERSIZE
)!=0, "bad header");
189 ** load precompiled chunk
191 Proto
* luaU_undump (lua_State
* L
, ZIO
* Z
, Mbuffer
* buff
, const char* name
)
194 if (*name
=='@' || *name
=='=')
196 else if (*name
==LUA_SIGNATURE
[0])
197 S
.name
="binary string";
204 return LoadFunction(&S
,luaS_newliteral(L
,"=?"));
210 void luaU_header (char* h
)
213 memcpy(h
,LUA_SIGNATURE
,sizeof(LUA_SIGNATURE
)-1);
214 h
+=sizeof(LUA_SIGNATURE
)-1;
215 *h
++=(char)LUAC_VERSION
;
216 *h
++=(char)LUAC_FORMAT
;
217 *h
++=(char)*(char*)&x
; /* endianness */
218 *h
++=(char)sizeof(int);
219 *h
++=(char)sizeof(size_t);
220 *h
++=(char)sizeof(Instruction
);
221 *h
++=(char)sizeof(lua_Number
);
222 *h
++=(char)(((lua_Number
)0.5)==0); /* is lua_Number integral? */