aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-09-04 15:52:51 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-09-04 15:52:51 -0300
commit6990b064679576ec46d26294108bd6cb7991faa6 (patch)
tree40a5ca64687cd52366a0e346e70248cff315c096
parentcad91499dd183a2c08012337438730da6418e418 (diff)
downloadlua-6990b064679576ec46d26294108bd6cb7991faa6.tar.gz
lua-6990b064679576ec46d26294108bd6cb7991faa6.tar.bz2
lua-6990b064679576ec46d26294108bd6cb7991faa6.zip
finish with parse data before doing main chunk.
-rw-r--r--ldo.c69
1 files changed, 33 insertions, 36 deletions
diff --git a/ldo.c b/ldo.c
index 9e641160..a06fa138 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.91 2000/08/29 20:43:28 roberto Exp roberto $ 2** $Id: ldo.c,v 1.92 2000/08/31 13:31:44 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -265,80 +265,77 @@ int lua_call (lua_State *L, int nargs, int nresults) {
265} 265}
266 266
267 267
268/*
269** returns 0 = chunk loaded; >0 : error; -1 = no more chunks to load
270*/
271static int protectedparser (lua_State *L, ZIO *z, int bin) { 268static int protectedparser (lua_State *L, ZIO *z, int bin) {
272 struct lua_longjmp myErrorJmp; 269 struct lua_longjmp myErrorJmp;
270 unsigned long old_blocks;
271 luaC_checkGC(L);
272 old_blocks = L->nblocks;
273 chain_longjmp(L, &myErrorJmp); 273 chain_longjmp(L, &myErrorJmp);
274 if (setjmp(myErrorJmp.b) == 0) { 274 if (setjmp(myErrorJmp.b) == 0) {
275 Proto *tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z); 275 Proto *tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
276 if (tf == NULL)
277 myErrorJmp.status = -1; /* `natural' end */
278 luaV_Lclosure(L, tf, 0); 276 luaV_Lclosure(L, tf, 0);
279 } 277 }
280 else { /* an error occurred: correct error code */ 278 else { /* an error occurred: correct error code */
281 if (myErrorJmp.status == LUA_ERRRUN) 279 if (myErrorJmp.status == LUA_ERRRUN)
282 myErrorJmp.status = LUA_ERRSYNTAX; 280 myErrorJmp.status = LUA_ERRSYNTAX;
283 } 281 }
282 /* add new memory to threshould (as it probably will stay) */
283 L->GCthreshold += (L->nblocks - old_blocks);
284 return restore_longjmp(L, &myErrorJmp); /* error code */ 284 return restore_longjmp(L, &myErrorJmp); /* error code */
285} 285}
286 286
287 287
288static int do_main (lua_State *L, ZIO *z, int bin) { 288static int parse_file (lua_State *L, const char *filename) {
289 int status;
290 do {
291 unsigned long old_blocks;
292 luaC_checkGC(L);
293 old_blocks = L->nblocks;
294 status = protectedparser(L, z, bin);
295 if (status > 0) return status; /* error */
296 else if (status < 0) return 0; /* `natural' end */
297 else {
298 unsigned long newelems2 = 2*(L->nblocks-old_blocks);
299 L->GCthreshold += newelems2;
300 status = lua_call(L, 0, LUA_MULTRET);
301 L->GCthreshold -= newelems2;
302 }
303 } while (bin && status == 0);
304 return status;
305}
306
307
308int lua_dofile (lua_State *L, const char *filename) {
309 ZIO z; 289 ZIO z;
290 char source[MAXFILENAME];
310 int status; 291 int status;
311 int bin; /* flag for file mode */ 292 int bin; /* flag for file mode */
312 int c; /* look ahead char */ 293 int c; /* look ahead char */
313 char source[MAXFILENAME];
314 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); 294 FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
315 if (f == NULL) return 2; /* unable to open file */ 295 if (f == NULL) return LUA_ERRFILE; /* unable to open file */
316 luaL_filesource(source, filename, sizeof(source)); 296 luaL_filesource(source, filename, sizeof(source));
317 c = fgetc(f); 297 c = fgetc(f);
318 ungetc(c, f); 298 ungetc(c, f);
319 bin = (c == ID_CHUNK); 299 bin = (c == ID_CHUNK);
320 if (bin && f != stdin) { 300 if (bin && f != stdin) {
321 f = freopen(filename, "rb", f); /* set binary mode */ 301 f = freopen(filename, "rb", f); /* set binary mode */
322 if (f == NULL) return 2; /* unable to reopen file */ 302 if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
323 } 303 }
324 luaZ_Fopen(&z, f, source); 304 luaZ_Fopen(&z, f, source);
325 status = do_main(L, &z, bin); 305 status = protectedparser(L, &z, bin);
326 if (f != stdin) 306 if (f != stdin)
327 fclose(f); 307 fclose(f);
328 return status; 308 return status;
329} 309}
330 310
331 311
332int lua_dostring (lua_State *L, const char *str) { 312int lua_dofile (lua_State *L, const char *filename) {
333 return lua_dobuffer(L, str, strlen(str), str); 313 int status = parse_file(L, filename);
314 if (status == 0) /* parse OK? */
315 status = lua_call(L, 0, LUA_MULTRET); /* call main */
316 return status;
334} 317}
335 318
336 319
337int lua_dobuffer (lua_State *L, const char *buff, size_t size, 320static int parse_buffer (lua_State *L, const char *buff, size_t size,
338 const char *name) { 321 const char *name) {
339 ZIO z; 322 ZIO z;
340 if (!name) name = "?"; 323 if (!name) name = "?";
341 luaZ_mopen(&z, buff, size, name); 324 luaZ_mopen(&z, buff, size, name);
342 return do_main(L, &z, buff[0]==ID_CHUNK); 325 return protectedparser(L, &z, buff[0]==ID_CHUNK);
326}
327
328
329int lua_dobuffer (lua_State *L, const char *buff, size_t size,
330 const char *name) {
331 int status = parse_buffer(L, buff, size, name);
332 if (status == 0) /* parse OK? */
333 status = lua_call(L, 0, LUA_MULTRET); /* call main */
334 return status;
335}
336
337
338int lua_dostring (lua_State *L, const char *str) {
339 return lua_dobuffer(L, str, strlen(str), str);
343} 340}
344 341