2 ** $Id: lundump.c,v 1.71 2011/12/07 10:39:12 lhf Exp $
3 ** load precompiled Lua chunks
4 ** See Copyright Notice in lua.h
30 static void error(LoadState
* S
, const char* why
)
32 luaO_pushfstring(S
->L
,"%s: %s precompiled chunk",S
->name
,why
);
33 luaD_throw(S
->L
,LUA_ERRSYNTAX
);
36 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
37 #define LoadByte(S) (lu_byte)LoadChar(S)
38 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
39 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
41 #if !defined(luai_verifycode)
42 #define luai_verifycode(L,b,f) (f)
45 static void LoadBlock(LoadState
* S
, void* b
, size_t size
)
47 if (luaZ_read(S
->Z
,b
,size
)!=0) error(S
,"truncated");
50 static int LoadChar(LoadState
* S
)
57 static int LoadInt(LoadState
* S
)
61 if (x
<0) error(S
,"corrupted");
65 static lua_Number
LoadNumber(LoadState
* S
)
72 static TString
* LoadString(LoadState
* S
)
80 char* s
=luaZ_openspace(S
->L
,S
->b
,size
);
81 LoadBlock(S
,s
,size
*sizeof(char));
82 return luaS_newlstr(S
->L
,s
,size
-1); /* remove trailing '\0' */
86 static void LoadCode(LoadState
* S
, Proto
* f
)
89 f
->code
=luaM_newvector(S
->L
,n
,Instruction
);
91 LoadVector(S
,f
->code
,n
,sizeof(Instruction
));
94 static Proto
* LoadFunction(LoadState
* S
);
96 static void LoadConstants(LoadState
* S
, Proto
* f
)
100 f
->k
=luaM_newvector(S
->L
,n
,TValue
);
102 for (i
=0; i
<n
; i
++) setnilvalue(&f
->k
[i
]);
113 setbvalue(o
,LoadChar(S
));
116 setnvalue(o
,LoadNumber(S
));
119 setsvalue2n(S
->L
,o
,LoadString(S
));
124 f
->p
=luaM_newvector(S
->L
,n
,Proto
*);
126 for (i
=0; i
<n
; i
++) f
->p
[i
]=NULL
;
127 for (i
=0; i
<n
; i
++) f
->p
[i
]=LoadFunction(S
);
130 static void LoadUpvalues(LoadState
* S
, Proto
* f
)
134 f
->upvalues
=luaM_newvector(S
->L
,n
,Upvaldesc
);
136 for (i
=0; i
<n
; i
++) f
->upvalues
[i
].name
=NULL
;
139 f
->upvalues
[i
].instack
=LoadByte(S
);
140 f
->upvalues
[i
].idx
=LoadByte(S
);
144 static void LoadDebug(LoadState
* S
, Proto
* f
)
147 f
->source
=LoadString(S
);
149 f
->lineinfo
=luaM_newvector(S
->L
,n
,int);
151 LoadVector(S
,f
->lineinfo
,n
,sizeof(int));
153 f
->locvars
=luaM_newvector(S
->L
,n
,LocVar
);
155 for (i
=0; i
<n
; i
++) f
->locvars
[i
].varname
=NULL
;
158 f
->locvars
[i
].varname
=LoadString(S
);
159 f
->locvars
[i
].startpc
=LoadInt(S
);
160 f
->locvars
[i
].endpc
=LoadInt(S
);
163 for (i
=0; i
<n
; i
++) f
->upvalues
[i
].name
=LoadString(S
);
166 static Proto
* LoadFunction(LoadState
* S
)
168 Proto
* f
=luaF_newproto(S
->L
);
169 setptvalue2s(S
->L
,S
->L
->top
,f
); incr_top(S
->L
);
170 f
->linedefined
=LoadInt(S
);
171 f
->lastlinedefined
=LoadInt(S
);
172 f
->numparams
=LoadByte(S
);
173 f
->is_vararg
=LoadByte(S
);
174 f
->maxstacksize
=LoadByte(S
);
183 /* the code below must be consistent with the code in luaU_header */
184 #define N0 LUAC_HEADERSIZE
185 #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
189 static void LoadHeader(LoadState
* S
)
191 lu_byte h
[LUAC_HEADERSIZE
];
192 lu_byte s
[LUAC_HEADERSIZE
];
194 memcpy(s
,h
,sizeof(char)); /* first char already read */
195 LoadBlock(S
,s
+sizeof(char),LUAC_HEADERSIZE
-sizeof(char));
196 if (memcmp(h
,s
,N0
)==0) return;
197 if (memcmp(h
,s
,N1
)!=0) error(S
,"not a");
198 if (memcmp(h
,s
,N2
)!=0) error(S
,"version mismatch in");
199 if (memcmp(h
,s
,N3
)!=0) error(S
,"incompatible"); else error(S
,"corrupted");
203 ** load precompiled chunk
205 Proto
* luaU_undump (lua_State
* L
, ZIO
* Z
, Mbuffer
* buff
, const char* name
)
208 if (*name
=='@' || *name
=='=')
210 else if (*name
==LUA_SIGNATURE
[0])
211 S
.name
="binary string";
218 return luai_verifycode(L
,buff
,LoadFunction(&S
));
221 #define MYINT(s) (s[0]-'0')
222 #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
223 #define FORMAT 0 /* this is the official format */
226 * make header for precompiled chunks
227 * if you change the code below be sure to update LoadHeader and FORMAT above
228 * and LUAC_HEADERSIZE in lundump.h
230 void luaU_header (lu_byte
* h
)
233 memcpy(h
,LUA_SIGNATURE
,sizeof(LUA_SIGNATURE
)-sizeof(char));
234 h
+=sizeof(LUA_SIGNATURE
)-sizeof(char);
235 *h
++=cast_byte(VERSION
);
236 *h
++=cast_byte(FORMAT
);
237 *h
++=cast_byte(*(char*)&x
); /* endianness */
238 *h
++=cast_byte(sizeof(int));
239 *h
++=cast_byte(sizeof(size_t));
240 *h
++=cast_byte(sizeof(Instruction
));
241 *h
++=cast_byte(sizeof(lua_Number
));
242 *h
++=cast_byte(((lua_Number
)0.5)==0); /* is lua_Number integral? */
243 memcpy(h
,LUAC_TAIL
,sizeof(LUAC_TAIL
)-sizeof(char));