From 7e0134865866951e522ad95034daf0e044fbedd2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Oct 1995 10:10:20 -0300 Subject: [PATCH] "docall" now assumes that called function is always on the stack, just below "base". That gives more regularity for the stack shape, and prevents fallbacks of being garbage collected if they are redefined during execution. --- opcode.c | 176 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 101 insertions(+), 75 deletions(-) diff --git a/opcode.c b/opcode.c index 78fabf25..1fb831c7 100644 --- a/opcode.c +++ b/opcode.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_opcode="$Id: opcode.c,v 3.38 1995/05/16 17:23:58 roberto Exp $"; +char *rcs_opcode="$Id: opcode.c,v 3.40 1995/10/04 14:20:26 roberto Exp roberto $"; #include #include @@ -54,7 +54,7 @@ static jmp_buf *errorJmp = NULL; /* current error recover point */ static StkId lua_execute (Byte *pc, StkId base); -static void do_call (Object *func, StkId base, int nResults, StkId whereRes); +static void do_call (StkId base, int nResults); @@ -64,32 +64,6 @@ Object *luaI_Address (lua_Object o) } -/* -** Error messages -*/ - - -static void lua_message (char *s) -{ - luaI_reportbug(s, 1); - do_call(&luaI_fallBacks[FB_ERROR].function, (top-stack)-1, 0, (top-stack)-1); -} - -/* -** Reports an error, and jumps up to the available recover label -*/ -void lua_error (char *s) -{ - if (s) lua_message(s); - if (errorJmp) - longjmp(*errorJmp, 1); - else - { - fprintf (stderr, "lua: exit(1). Unable to recover\n"); - exit(1); - } -} - /* ** Init stack @@ -211,6 +185,17 @@ static void adjustC (int nParams) adjust_top(CBase+nParams); } +/* +** Open a hole below "nelems" from the top. +*/ +static void open_stack (int nelems) +{ + int i; + for (i=0; ibase; i--) - stack[i] = stack[i-1]; - incr_top; - stack[base] = *func; - do_call(&luaI_fallBacks[FB_FUNCTION].function, base, nResults, whereRes); + open_stack((top-stack)-(base-1)); + stack[base-1] = luaI_fallBacks[FB_FUNCTION].function; + do_call(base, nResults); } /* ** Call a function (C or Lua). The parameters must be on the stack, -** between [stack+base,top). When returns, the results are on the stack, -** between [stack+whereRes,top). The number of results is nResults, unless -** nResults=MULT_RET. +** between [stack+base,top). The function to be called is at stack+base-1. +** When returns, the results are on the stack, between [stack+base-1,top). +** The number of results is nResults, unless nResults=MULT_RET. */ -static void do_call (Object *func, StkId base, int nResults, StkId whereRes) +static void do_call (StkId base, int nResults) { StkId firstResult; + Object *func = stack+base-1; if (tag(func) == LUA_T_CFUNCTION) firstResult = callC(fvalue(func), base); else if (tag(func) == LUA_T_FUNCTION) firstResult = lua_execute(func->value.tf->code, base); else { /* func is not a function */ - call_funcFB(func, base, nResults, whereRes); + call_funcFB(base, nResults); return; } /* adjust the number of results */ if (nResults != MULT_RET && top - (stack+firstResult) != nResults) adjust_top(firstResult+nResults); - /* move results to the given position */ - if (firstResult != whereRes) + /* move results to base-1 (to erase parameters and function) */ + base--; + if (firstResult != base) { int i; nResults = top - (stack+firstResult); /* actual number of results */ for (i=0; i