aboutsummaryrefslogtreecommitdiff
path: root/opcode.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-09 16:13:29 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-09 16:13:29 -0200
commitd531ccd082a73aa2fda585dfe5edf2749c7e7d13 (patch)
tree62eda5b7aaf8a93d215f0457aacaef0b1716b33d /opcode.c
parentdf0cfc1e19ed220e15bf2c088be21a2ebb7aaad3 (diff)
downloadlua-d531ccd082a73aa2fda585dfe5edf2749c7e7d13.tar.gz
lua-d531ccd082a73aa2fda585dfe5edf2749c7e7d13.tar.bz2
lua-d531ccd082a73aa2fda585dfe5edf2749c7e7d13.zip
execution of 'mains' separated from calls
Diffstat (limited to 'opcode.c')
-rw-r--r--opcode.c56
1 files changed, 41 insertions, 15 deletions
diff --git a/opcode.c b/opcode.c
index 1f6573cd..358771a5 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.5 1994/11/07 18:27:39 roberto Exp $"; 6char *rcs_opcode="$Id: opcode.c,v 3.6 1994/11/08 19:56:39 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <stdlib.h> 9#include <stdlib.h>
@@ -286,7 +286,10 @@ static void do_call (Object *func, int base, int nResults, int whereRes)
286 else if (tag(func) == LUA_T_FUNCTION) 286 else if (tag(func) == LUA_T_FUNCTION)
287 firstResult = lua_execute(bvalue(func), base); 287 firstResult = lua_execute(bvalue(func), base);
288 else 288 else
289 {
289 lua_reportbug ("call expression not a function"); 290 lua_reportbug ("call expression not a function");
291 return; /* to avoid warnings */
292 }
290 /* adjust the number of results */ 293 /* adjust the number of results */
291 if (nResults != MULT_RET && top - (stack+firstResult) != nResults) 294 if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
292 adjust_top(stack+firstResult+nResults); 295 adjust_top(stack+firstResult+nResults);
@@ -356,7 +359,6 @@ void lua_travstack (void (*fn)(Object *))
356*/ 359*/
357static int do_protectedrun (Object *function, int nResults) 360static int do_protectedrun (Object *function, int nResults)
358{ 361{
359 Object f;
360 jmp_buf myErrorJmp; 362 jmp_buf myErrorJmp;
361 int status; 363 int status;
362 int oldCBase = CBase; 364 int oldCBase = CBase;
@@ -364,14 +366,6 @@ static int do_protectedrun (Object *function, int nResults)
364 errorJmp = &myErrorJmp; 366 errorJmp = &myErrorJmp;
365 if (setjmp(myErrorJmp) == 0) 367 if (setjmp(myErrorJmp) == 0)
366 { 368 {
367 if (function == NULL)
368 {
369 tag(&f) = LUA_T_FUNCTION;
370 bvalue(&f) = lua_parse();
371 function = &f;
372 }
373 else
374 tag(&f) = LUA_T_NIL;
375 do_call(function, CBase, nResults, CBase); 369 do_call(function, CBase, nResults, CBase);
376 CnResults = (top-stack) - CBase; /* number of results */ 370 CnResults = (top-stack) - CBase; /* number of results */
377 CBase += CnResults; /* incorporate results on the stack */ 371 CBase += CnResults; /* incorporate results on the stack */
@@ -383,12 +377,38 @@ static int do_protectedrun (Object *function, int nResults)
383 top = stack+CBase; 377 top = stack+CBase;
384 status = 1; 378 status = 1;
385 } 379 }
386 if (tag(&f) == LUA_T_FUNCTION)
387 free(bvalue(&f));
388 errorJmp = oldErr; 380 errorJmp = oldErr;
389 return status; 381 return status;
390} 382}
391 383
384
385static int do_protectedmain (void)
386{
387 Byte *code = NULL;
388 int status;
389 int oldCBase = CBase;
390 jmp_buf myErrorJmp;
391 jmp_buf *oldErr = errorJmp;
392 errorJmp = &myErrorJmp;
393 if (setjmp(myErrorJmp) == 0)
394 {
395 Object f;
396 lua_parse(&code);
397 tag(&f) = LUA_T_FUNCTION; bvalue(&f) = code;
398 do_call(&f, CBase, 0, CBase);
399 status = 0;
400 }
401 else
402 status = 1;
403 if (code)
404 free(code);
405 errorJmp = oldErr;
406 CBase = oldCBase;
407 top = stack+CBase;
408 return status;
409}
410
411
392/* 412/*
393** Execute the given lua function. Return 0 on success or 1 on error. 413** Execute the given lua function. Return 0 on success or 1 on error.
394*/ 414*/
@@ -401,6 +421,13 @@ int lua_callfunction (lua_Object function)
401} 421}
402 422
403 423
424int lua_call (char *funcname)
425{
426 int n = lua_findsymbol(funcname);
427 return do_protectedrun(&s_object(n), MULT_RET);
428}
429
430
404/* 431/*
405** Open file, generate opcode and execute global statement. Return 0 on 432** Open file, generate opcode and execute global statement. Return 0 on
406** success or 1 on error. 433** success or 1 on error.
@@ -414,7 +441,7 @@ int lua_dofile (char *filename)
414 lua_message(message); 441 lua_message(message);
415 return 1; 442 return 1;
416 } 443 }
417 status = do_protectedrun(NULL, 0); 444 status = do_protectedmain();
418 lua_closefile(); 445 lua_closefile();
419 return status; 446 return status;
420} 447}
@@ -432,7 +459,7 @@ int lua_dostring (char *string)
432 lua_message(message); 459 lua_message(message);
433 return 1; 460 return 1;
434 } 461 }
435 status = do_protectedrun(NULL, 0); 462 status = do_protectedmain();
436 lua_closestring(); 463 lua_closestring();
437 return status; 464 return status;
438} 465}
@@ -694,7 +721,6 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal,
694*/ 721*/
695static int lua_execute (Byte *pc, int base) 722static int lua_execute (Byte *pc, int base)
696{ 723{
697 lua_debugline = 0; /* reset debug flag */
698 lua_checkstack(STACKGAP+MAX_TEMPS+base); 724 lua_checkstack(STACKGAP+MAX_TEMPS+base);
699 while (1) 725 while (1)
700 { 726 {