diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-16 13:50:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-16 13:50:22 -0300 |
commit | c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197 (patch) | |
tree | 6f099e983ecb968f625a8824ac2ca78e625882e3 /undump.c | |
parent | 9fe5be3acf340b90fe0c24d36a601adaf6f21c79 (diff) | |
download | lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.tar.gz lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.tar.bz2 lua-c9a2dfeb2c71b1c6b8164a1e5aad4b178950e197.zip |
using "zio" for parsing Lua code.
Diffstat (limited to 'undump.c')
-rw-r--r-- | undump.c | 105 |
1 files changed, 51 insertions, 54 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** load bytecodes from files | 3 | ** load bytecodes from files |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp lhf $"; | 6 | char* rcs_undump="$Id: undump.c,v 1.24 1997/06/13 11:08:47 lhf Exp $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
@@ -12,6 +12,7 @@ char* rcs_undump="$Id: undump.c,v 1.21 1996/11/18 11:18:29 lhf Exp lhf $"; | |||
12 | #include "luamem.h" | 12 | #include "luamem.h" |
13 | #include "table.h" | 13 | #include "table.h" |
14 | #include "undump.h" | 14 | #include "undump.h" |
15 | #include "zio.h" | ||
15 | 16 | ||
16 | static int swapword=0; | 17 | static int swapword=0; |
17 | static int swapfloat=0; | 18 | static int swapfloat=0; |
@@ -147,10 +148,10 @@ static void Unthread(Byte* code, int i, int v) | |||
147 | } | 148 | } |
148 | } | 149 | } |
149 | 150 | ||
150 | static int LoadWord(FILE* D) | 151 | static int LoadWord(ZIO* Z) |
151 | { | 152 | { |
152 | Word w; | 153 | Word w; |
153 | fread(&w,sizeof(w),1,D); | 154 | zread(Z,&w,sizeof(w)); |
154 | if (swapword) | 155 | if (swapword) |
155 | { | 156 | { |
156 | Byte* p=(Byte*)&w; | 157 | Byte* p=(Byte*)&w; |
@@ -160,101 +161,101 @@ static int LoadWord(FILE* D) | |||
160 | return w; | 161 | return w; |
161 | } | 162 | } |
162 | 163 | ||
163 | static int LoadSize(FILE* D) | 164 | static int LoadSize(ZIO* Z) |
164 | { | 165 | { |
165 | Word hi=LoadWord(D); | 166 | Word hi=LoadWord(Z); |
166 | Word lo=LoadWord(D); | 167 | Word lo=LoadWord(Z); |
167 | int s=(hi<<16)|lo; | 168 | int s=(hi<<16)|lo; |
168 | if ((Word)s != s) lua_error("code too long"); | 169 | if ((Word)s != s) lua_error("code too long"); |
169 | return s; | 170 | return s; |
170 | } | 171 | } |
171 | 172 | ||
172 | static void* LoadBlock(int size, FILE* D) | 173 | static void* LoadBlock(int size, ZIO* Z) |
173 | { | 174 | { |
174 | void* b=luaI_malloc(size); | 175 | void* b=luaI_malloc(size); |
175 | fread(b,size,1,D); | 176 | zread(Z,b,size); |
176 | return b; | 177 | return b; |
177 | } | 178 | } |
178 | 179 | ||
179 | static char* LoadString(FILE* D) | 180 | static char* LoadString(ZIO* Z) |
180 | { | 181 | { |
181 | int size=LoadWord(D); | 182 | int size=LoadWord(Z); |
182 | char *b=luaI_buffer(size); | 183 | char *b=luaI_buffer(size); |
183 | fread(b,size,1,D); | 184 | zread(Z,b,size); |
184 | return b; | 185 | return b; |
185 | } | 186 | } |
186 | 187 | ||
187 | static char* LoadNewString(FILE* D) | 188 | static char* LoadNewString(ZIO* Z) |
188 | { | 189 | { |
189 | return LoadBlock(LoadWord(D),D); | 190 | return LoadBlock(LoadWord(Z),Z); |
190 | } | 191 | } |
191 | 192 | ||
192 | static void LoadFunction(FILE* D) | 193 | static void LoadFunction(ZIO* Z) |
193 | { | 194 | { |
194 | TFunc* tf=new(TFunc); | 195 | TFunc* tf=new(TFunc); |
195 | tf->next=NULL; | 196 | tf->next=NULL; |
196 | tf->locvars=NULL; | 197 | tf->locvars=NULL; |
197 | tf->size=LoadSize(D); | 198 | tf->size=LoadSize(Z); |
198 | tf->lineDefined=LoadWord(D); | 199 | tf->lineDefined=LoadWord(Z); |
199 | if (IsMain(tf)) /* new main */ | 200 | if (IsMain(tf)) /* new main */ |
200 | { | 201 | { |
201 | tf->fileName=LoadNewString(D); | 202 | tf->fileName=LoadNewString(Z); |
202 | Main=lastF=tf; | 203 | Main=lastF=tf; |
203 | } | 204 | } |
204 | else /* fix PUSHFUNCTION */ | 205 | else /* fix PUSHFUNCTION */ |
205 | { | 206 | { |
206 | tf->marked=LoadWord(D); | 207 | tf->marked=LoadWord(Z); |
207 | tf->fileName=Main->fileName; | 208 | tf->fileName=Main->fileName; |
208 | memcpy(Main->code+tf->marked,&tf,sizeof(tf)); | 209 | memcpy(Main->code+tf->marked,&tf,sizeof(tf)); |
209 | lastF=lastF->next=tf; | 210 | lastF=lastF->next=tf; |
210 | } | 211 | } |
211 | tf->code=LoadBlock(tf->size,D); | 212 | tf->code=LoadBlock(tf->size,Z); |
212 | if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size); | 213 | if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size); |
213 | while (1) /* unthread */ | 214 | while (1) /* unthread */ |
214 | { | 215 | { |
215 | int c=getc(D); | 216 | int c=zgetc(Z); |
216 | if (c==ID_VAR) /* global var */ | 217 | if (c==ID_VAR) /* global var */ |
217 | { | 218 | { |
218 | int i=LoadWord(D); | 219 | int i=LoadWord(Z); |
219 | char* s=LoadString(D); | 220 | char* s=LoadString(Z); |
220 | int v=luaI_findsymbolbyname(s); | 221 | int v=luaI_findsymbolbyname(s); |
221 | Unthread(tf->code,i,v); | 222 | Unthread(tf->code,i,v); |
222 | } | 223 | } |
223 | else if (c==ID_STR) /* constant string */ | 224 | else if (c==ID_STR) /* constant string */ |
224 | { | 225 | { |
225 | int i=LoadWord(D); | 226 | int i=LoadWord(Z); |
226 | char* s=LoadString(D); | 227 | char* s=LoadString(Z); |
227 | int v=luaI_findconstantbyname(s); | 228 | int v=luaI_findconstantbyname(s); |
228 | Unthread(tf->code,i,v); | 229 | Unthread(tf->code,i,v); |
229 | } | 230 | } |
230 | else | 231 | else |
231 | { | 232 | { |
232 | ungetc(c,D); | 233 | zungetc(Z); |
233 | break; | 234 | break; |
234 | } | 235 | } |
235 | } | 236 | } |
236 | } | 237 | } |
237 | 238 | ||
238 | static void LoadSignature(FILE* D) | 239 | static void LoadSignature(ZIO* Z) |
239 | { | 240 | { |
240 | char* s=SIGNATURE; | 241 | char* s=SIGNATURE; |
241 | while (*s!=0 && getc(D)==*s) | 242 | while (*s!=0 && zgetc(Z)==*s) |
242 | ++s; | 243 | ++s; |
243 | if (*s!=0) lua_error("cannot load binary file: bad signature"); | 244 | if (*s!=0) lua_error("cannot load binary file: bad signature"); |
244 | } | 245 | } |
245 | 246 | ||
246 | static void LoadHeader(FILE* D) | 247 | static void LoadHeader(ZIO* Z) |
247 | { | 248 | { |
248 | Word w,tw=TEST_WORD; | 249 | Word w,tw=TEST_WORD; |
249 | float f,tf=TEST_FLOAT; | 250 | float f,tf=TEST_FLOAT; |
250 | int version; | 251 | int version; |
251 | LoadSignature(D); | 252 | LoadSignature(Z); |
252 | version=getc(D); | 253 | version=zgetc(Z); |
253 | if (version>0x23) /* after 2.5 */ | 254 | if (version>0x23) /* after 2.5 */ |
254 | { | 255 | { |
255 | int oldsizeofW=getc(D); | 256 | int oldsizeofW=zgetc(Z); |
256 | int oldsizeofF=getc(D); | 257 | int oldsizeofF=zgetc(Z); |
257 | int oldsizeofP=getc(D); | 258 | int oldsizeofP=zgetc(Z); |
258 | if (oldsizeofW!=2) | 259 | if (oldsizeofW!=2) |
259 | luaL_verror( | 260 | luaL_verror( |
260 | "cannot load binary file created on machine with sizeof(Word)=%d; " | 261 | "cannot load binary file created on machine with sizeof(Word)=%d; " |
@@ -266,14 +267,14 @@ static void LoadHeader(FILE* D) | |||
266 | if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */ | 267 | if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */ |
267 | luaL_verror( | 268 | luaL_verror( |
268 | "cannot load binary file created on machine with sizeof(TFunc*)=%d; " | 269 | "cannot load binary file created on machine with sizeof(TFunc*)=%d; " |
269 | "expected %d",oldsizeofP,sizeof(TFunc*)); | 270 | "expected %d",oldsizeofP,(int)sizeof(TFunc*)); |
270 | } | 271 | } |
271 | fread(&w,sizeof(w),1,D); /* test word */ | 272 | zread(Z,&w,sizeof(w)); /* test word */ |
272 | if (w!=tw) | 273 | if (w!=tw) |
273 | { | 274 | { |
274 | swapword=1; | 275 | swapword=1; |
275 | } | 276 | } |
276 | fread(&f,sizeof(f),1,D); /* test float */ | 277 | zread(Z,&f,sizeof(f)); /* test float */ |
277 | if (f!=tf) | 278 | if (f!=tf) |
278 | { | 279 | { |
279 | Byte* p=(Byte*)&f; | 280 | Byte* p=(Byte*)&f; |
@@ -286,13 +287,13 @@ static void LoadHeader(FILE* D) | |||
286 | } | 287 | } |
287 | } | 288 | } |
288 | 289 | ||
289 | static void LoadChunk(FILE* D) | 290 | static void LoadChunk(ZIO* Z) |
290 | { | 291 | { |
291 | LoadHeader(D); | 292 | LoadHeader(Z); |
292 | while (1) | 293 | while (1) |
293 | { | 294 | { |
294 | int c=getc(D); | 295 | int c=zgetc(Z); |
295 | if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; } | 296 | if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; } |
296 | } | 297 | } |
297 | } | 298 | } |
298 | 299 | ||
@@ -300,30 +301,26 @@ static void LoadChunk(FILE* D) | |||
300 | ** load one chunk from a file. | 301 | ** load one chunk from a file. |
301 | ** return list of functions found, headed by main, or NULL at EOF. | 302 | ** return list of functions found, headed by main, or NULL at EOF. |
302 | */ | 303 | */ |
303 | TFunc* luaI_undump1(FILE* D) | 304 | static TFunc* luaI_undump1(ZIO* Z) |
304 | { | 305 | { |
305 | while (1) | 306 | int c=zgetc(Z); |
307 | if (c==ID_CHUNK) | ||
306 | { | 308 | { |
307 | int c=getc(D); | 309 | LoadChunk(Z); |
308 | if (c==ID_CHUNK) | 310 | return Main; |
309 | { | ||
310 | LoadChunk(D); | ||
311 | return Main; | ||
312 | } | ||
313 | else if (c==EOF) | ||
314 | return NULL; | ||
315 | else | ||
316 | lua_error("not a lua binary file"); | ||
317 | } | 311 | } |
312 | else if (c!=EOZ) | ||
313 | lua_error("not a lua binary file"); | ||
314 | return NULL; | ||
318 | } | 315 | } |
319 | 316 | ||
320 | /* | 317 | /* |
321 | ** load and run all chunks in a file | 318 | ** load and run all chunks in a file |
322 | */ | 319 | */ |
323 | int luaI_undump(FILE* D) | 320 | int luaI_undump(ZIO* Z) |
324 | { | 321 | { |
325 | TFunc* m; | 322 | TFunc* m; |
326 | while ((m=luaI_undump1(D))) | 323 | while ((m=luaI_undump1(Z))) |
327 | { | 324 | { |
328 | int status=luaI_dorun(m); | 325 | int status=luaI_dorun(m); |
329 | luaI_freefunc(m); | 326 | luaI_freefunc(m); |