Переглянути джерело

better interfaces for luaD_calln (x luaD_call)

Roberto Ierusalimschy 26 роки тому
батько
коміт
521b38532a
4 змінених файлів з 36 додано та 51 видалено
  1. 8 10
      lapi.c
  2. 23 35
      ldo.c
  3. 2 3
      ldo.h
  4. 3 3
      lvm.c

+ 8 - 10
lapi.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -131,7 +131,7 @@ int lua_callfunction (lua_Object function)
   else {
     luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
     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
 */
 
-static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
-{
-  StkId base = (L->stack.top-L->stack.stack)-nParams;
+static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults) {
   luaD_openstack(nParams);
-  L->stack.stack[base].ttype = LUA_T_CPROTO;
-  L->stack.stack[base].value.f = f;
-  luaD_call(base+1, nResults);
+  (L->stack.top-nParams)->ttype = LUA_T_CPROTO;
+  (L->stack.top-nParams)->value.f = f;
+  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_pushcfunction(fallback);
   do_unprotectedrun(luaT_setfallback, 2, 1);

+ 23 - 35
ldo.c

@@ -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
 ** 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;
   int nup = cl->nelems;  /* number of upvalues */
   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,
-** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
-** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
+** Call a function (C or Lua). The parameters must be on the stack,
+** between [top-nArgs,top). The function to be called is right below the
+** arguments.
+** When returns, the results are on the stack, between [top-nArgs-1,top).
 ** 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;
-  TObject *func = L->stack.stack+base-1;
   int i;
   switch (ttype(func)) {
     case LUA_T_CPROTO:
@@ -201,24 +202,20 @@ void luaD_call (StkId base, int nResults)
       TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
       if (ttype(im) == LUA_T_NIL)
         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;
     }
   }
   /* adjust the number of results */
-  if (nResults != MULT_RET)
+  if (nResults == MULT_RET)
+    nResults = (S->top-S->stack)-firstResult;
+  else
     luaD_adjusttop(firstResult+nResults);
   /* move results to base-1 (to erase parameters and function) */
   base--;
-  nResults = L->stack.top - (L->stack.stack+firstResult);  /* actual number of results */
   for (i=0; i<nResults; i++)
-    *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
-  L->stack.top -= firstResult-base;
-}
-
-
-void luaD_calln (int nArgs, int nResults) {
-    luaD_call((L->stack.top-L->stack.stack)-nArgs, nResults);
+    *(S->stack+base+i) = *(S->stack+firstResult+i);
+  S->top -= firstResult-base;
 }
 
 
@@ -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
 ** 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;
   struct lua_longjmp myErrorJmp;
   volatile int status;
   struct lua_longjmp *volatile oldErr = L->errorJmp;
   L->errorJmp = &myErrorJmp;
   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;
   }
   else {  /* an error occurred: restore L->Cstack and L->stack.top */
@@ -338,7 +326,7 @@ static int do_main (ZIO *z, int bin) {
     else {
       unsigned long newelems2 = 2*(L->nblocks-old_blocks);
       L->GCthreshold += newelems2;
-      status = luaD_protectedrun(MULT_RET);
+      status = luaD_protectedrun();
       L->GCthreshold -= newelems2;
     }
   } while (bin && status == 0);

+ 2 - 3
ldo.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -35,10 +35,9 @@ void luaD_adjusttop (StkId newtop);
 void luaD_openstack (int nelems);
 void luaD_lineHook (int line);
 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_callTM (TObject *f, int nParams, int nResults);
-int luaD_protectedrun (int nResults);
+int luaD_protectedrun (void);
 void luaD_gcIM (TObject *o);
 void luaD_travstack (int (*fn)(TObject *));
 void luaD_checkstack (int n);

+ 3 - 3
lvm.c

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -342,11 +342,11 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) {
         goto ret;
 
       case CALL: aux = *pc++;
-        luaD_call((S->top-S->stack)-(*pc++), aux);
+        luaD_calln(*pc++, aux);
         break;
 
       case TAILCALL: aux = *pc++;
-        luaD_call((S->top-S->stack)-(*pc++), MULT_RET);
+        luaD_calln(*pc++, MULT_RET);
         base += aux;
         goto ret;