1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

better interfaces for luaD_calln (x luaD_call)

This commit is contained in:
Roberto Ierusalimschy 1999-06-22 17:37:23 -03:00
parent 36b6fe8d17
commit 521b38532a
4 changed files with 36 additions and 51 deletions

18
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.45 1999/05/14 12:24:20 roberto Exp roberto $ ** $Id: lapi.c,v 1.46 1999/06/17 17:04:03 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -131,7 +131,7 @@ int lua_callfunction (lua_Object function)
else { else {
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
set_normalized(L->stack.stack+L->Cstack.base, Address(function)); set_normalized(L->stack.stack+L->Cstack.base, Address(function));
return luaD_protectedrun(MULT_RET); return luaD_protectedrun();
} }
} }
@ -675,17 +675,15 @@ lua_Object lua_getref (int ref) {
** API: set a function as a fallback ** API: set a function as a fallback
*/ */
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) {
{
StkId base = (L->stack.top-L->stack.stack)-nParams;
luaD_openstack(nParams); luaD_openstack(nParams);
L->stack.stack[base].ttype = LUA_T_CPROTO; (L->stack.top-nParams)->ttype = LUA_T_CPROTO;
L->stack.stack[base].value.f = f; (L->stack.top-nParams)->value.f = f;
luaD_call(base+1, nResults); luaD_calln(nParams, nResults);
} }
lua_Object lua_setfallback (char *name, lua_CFunction fallback)
{ lua_Object lua_setfallback (char *name, lua_CFunction fallback) {
lua_pushstring(name); lua_pushstring(name);
lua_pushcfunction(fallback); lua_pushcfunction(fallback);
do_unprotectedrun(luaT_setfallback, 2, 1); do_unprotectedrun(luaT_setfallback, 2, 1);

58
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.43 1999/05/24 17:53:03 roberto Exp roberto $ ** $Id: ldo.c,v 1.44 1999/06/17 17:04:03 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -145,8 +145,7 @@ static StkId callC (lua_CFunction f, StkId base) {
} }
static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base) static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base) {
{
TObject *pbase; TObject *pbase;
int nup = cl->nelems; /* number of upvalues */ int nup = cl->nelems; /* number of upvalues */
luaD_checkstack(nup); luaD_checkstack(nup);
@ -168,15 +167,17 @@ void luaD_callTM (TObject *f, int nParams, int nResults) {
/* /*
** Call a function (C or Lua). The parameters must be on the L->stack.stack, ** Call a function (C or Lua). The parameters must be on the stack,
** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1. ** between [top-nArgs,top). The function to be called is right below the
** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top). ** arguments.
** When returns, the results are on the stack, between [top-nArgs-1,top).
** The number of results is nResults, unless nResults=MULT_RET. ** The number of results is nResults, unless nResults=MULT_RET.
*/ */
void luaD_call (StkId base, int nResults) void luaD_calln (int nArgs, int nResults) {
{ struct Stack *S = &L->stack; /* to optimize */
StkId base = (S->top-S->stack)-nArgs;
TObject *func = S->stack+base-1;
StkId firstResult; StkId firstResult;
TObject *func = L->stack.stack+base-1;
int i; int i;
switch (ttype(func)) { switch (ttype(func)) {
case LUA_T_CPROTO: case LUA_T_CPROTO:
@ -201,24 +202,20 @@ void luaD_call (StkId base, int nResults)
TObject *im = luaT_getimbyObj(func, IM_FUNCTION); TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
if (ttype(im) == LUA_T_NIL) if (ttype(im) == LUA_T_NIL)
lua_error("call expression not a function"); lua_error("call expression not a function");
luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults); luaD_callTM(im, (S->top-S->stack)-(base-1), nResults);
return; return;
} }
} }
/* adjust the number of results */ /* adjust the number of results */
if (nResults != MULT_RET) if (nResults == MULT_RET)
nResults = (S->top-S->stack)-firstResult;
else
luaD_adjusttop(firstResult+nResults); luaD_adjusttop(firstResult+nResults);
/* move results to base-1 (to erase parameters and function) */ /* move results to base-1 (to erase parameters and function) */
base--; base--;
nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
for (i=0; i<nResults; i++) for (i=0; i<nResults; i++)
*(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i); *(S->stack+base+i) = *(S->stack+firstResult+i);
L->stack.top -= firstResult-base; S->top -= firstResult-base;
}
void luaD_calln (int nArgs, int nResults) {
luaD_call((L->stack.top-L->stack.stack)-nArgs, nResults);
} }
@ -258,32 +255,23 @@ void lua_error (char *s) {
} }
} }
/*
** Call the function at L->Cstack.base, and incorporate results on
** the Lua2C structure.
*/
static void do_callinc (int nResults)
{
StkId base = L->Cstack.base;
luaD_call(base+1, nResults);
L->Cstack.lua2C = base; /* position of the new results */
L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
}
/* /*
** Execute a protected call. Assumes that function is at L->Cstack.base and ** Execute a protected call. Assumes that function is at L->Cstack.base and
** parameters are on top of it. Leave nResults on the stack. ** parameters are on top of it. Leave nResults on the stack.
*/ */
int luaD_protectedrun (int nResults) { int luaD_protectedrun (void) {
volatile struct C_Lua_Stack oldCLS = L->Cstack; volatile struct C_Lua_Stack oldCLS = L->Cstack;
struct lua_longjmp myErrorJmp; struct lua_longjmp myErrorJmp;
volatile int status; volatile int status;
struct lua_longjmp *volatile oldErr = L->errorJmp; struct lua_longjmp *volatile oldErr = L->errorJmp;
L->errorJmp = &myErrorJmp; L->errorJmp = &myErrorJmp;
if (setjmp(myErrorJmp.b) == 0) { if (setjmp(myErrorJmp.b) == 0) {
do_callinc(nResults); StkId base = L->Cstack.base;
luaD_calln((L->stack.top-L->stack.stack)-base-1, MULT_RET);
L->Cstack.lua2C = base; /* position of the new results */
L->Cstack.num = (L->stack.top-L->stack.stack) - base;
L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
status = 0; status = 0;
} }
else { /* an error occurred: restore L->Cstack and L->stack.top */ else { /* an error occurred: restore L->Cstack and L->stack.top */
@ -338,7 +326,7 @@ static int do_main (ZIO *z, int bin) {
else { else {
unsigned long newelems2 = 2*(L->nblocks-old_blocks); unsigned long newelems2 = 2*(L->nblocks-old_blocks);
L->GCthreshold += newelems2; L->GCthreshold += newelems2;
status = luaD_protectedrun(MULT_RET); status = luaD_protectedrun();
L->GCthreshold -= newelems2; L->GCthreshold -= newelems2;
} }
} while (bin && status == 0); } while (bin && status == 0);

5
ldo.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.4 1997/12/15 16:17:20 roberto Exp roberto $ ** $Id: ldo.h,v 1.5 1998/07/12 16:14:34 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -35,10 +35,9 @@ void luaD_adjusttop (StkId newtop);
void luaD_openstack (int nelems); void luaD_openstack (int nelems);
void luaD_lineHook (int line); void luaD_lineHook (int line);
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn); void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn);
void luaD_call (StkId base, int nResults);
void luaD_calln (int nArgs, int nResults); void luaD_calln (int nArgs, int nResults);
void luaD_callTM (TObject *f, int nParams, int nResults); void luaD_callTM (TObject *f, int nParams, int nResults);
int luaD_protectedrun (int nResults); int luaD_protectedrun (void);
void luaD_gcIM (TObject *o); void luaD_gcIM (TObject *o);
void luaD_travstack (int (*fn)(TObject *)); void luaD_travstack (int (*fn)(TObject *));
void luaD_checkstack (int n); void luaD_checkstack (int n);

6
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.56 1999/05/21 17:23:15 roberto Exp roberto $ ** $Id: lvm.c,v 1.57 1999/05/21 19:41:49 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -342,11 +342,11 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) {
goto ret; goto ret;
case CALL: aux = *pc++; case CALL: aux = *pc++;
luaD_call((S->top-S->stack)-(*pc++), aux); luaD_calln(*pc++, aux);
break; break;
case TAILCALL: aux = *pc++; case TAILCALL: aux = *pc++;
luaD_call((S->top-S->stack)-(*pc++), MULT_RET); luaD_calln(*pc++, MULT_RET);
base += aux; base += aux;
goto ret; goto ret;