2
0
Эх сурвалжийг харах

better implementation for luaV_pack

Roberto Ierusalimschy 25 жил өмнө
parent
commit
4e56c0d514
3 өөрчлөгдсөн 16 нэмэгдсэн , 23 устгасан
  1. 3 5
      lbuiltin.c
  2. 11 16
      lvm.c
  3. 2 2
      lvm.h

+ 3 - 5
lbuiltin.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbuiltin.c,v 1.122 2000/08/28 17:57:04 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.123 2000/08/29 14:33:31 roberto Exp roberto $
 ** Built-in functions
 ** See Copyright Notice in lua.h
 */
@@ -333,7 +333,6 @@ int luaB_call (lua_State *L) {
   for (i=0; i<n; i++)
     *(L->top++) = *luaH_getnum(arg, i+1);
   status = lua_call(L, n, LUA_MULTRET);
-  n = lua_gettop(L) - oldtop;  /* number of results */
   if (err != 0) {  /* restore old error method */
     lua_pushobject(L, err);
     lua_setglobal(L, LUA_ERRORMESSAGE);
@@ -347,12 +346,11 @@ int luaB_call (lua_State *L) {
   }
   else {  /* no errors */
     if (strchr(options, 'p')) {  /* pack results? */
-      luaV_pack(L, luaA_index(L, oldtop+1), n, L->top);
-      incr_top;
+      luaV_pack(L, luaA_index(L, oldtop+1));
       return 1;  /* only table is returned */
     }
     else
-      return n;  /* results are already on the stack */
+      return lua_gettop(L) - oldtop;  /* results are already on the stack */
   }
 }
 

+ 11 - 16
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.128 2000/08/22 20:49:29 roberto Exp roberto $
+** $Id: lvm.c,v 1.129 2000/08/22 20:53:30 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -313,30 +313,25 @@ static void strconc (lua_State *L, int total, StkId top) {
 }
 
 
-void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
+void luaV_pack (lua_State *L, StkId firstelem) {
   int i;
-  Hash *htab;
-  htab = hvalue(tab) = luaH_new(L, nvararg+1);  /* +1 for field `n' */
-  ttype(tab) = TAG_TABLE;
-  for (i=0; i<nvararg; i++)
+  Hash *htab = luaH_new(L, 0);
+  for (i=0; firstelem+i<L->top; i++)
     *luaH_setint(L, htab, i+1) = *(firstelem+i);
   /* store counter in field `n' */
-  luaH_setstrnum(L, htab, luaS_new(L, "n"), nvararg);
+  luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
+  L->top = firstelem;  /* remove elements from the stack */
+  ttype(L->top) = TAG_TABLE;
+  hvalue(L->top) = htab;
+  incr_top;
 }
 
 
 static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
-  TObject arg;
   int nvararg = (L->top-base) - nfixargs;
-  if (nvararg < 0) {
-    luaV_pack(L, base, 0, &arg);
+  if (nvararg < 0)
     luaD_adjusttop(L, base, nfixargs);
-  }
-  else {
-    luaV_pack(L, base+nfixargs, nvararg, &arg);
-    L->top = base+nfixargs;
-  }
-  *L->top++ = arg;
+  luaV_pack(L, base+nfixargs);
 }
 
 

+ 2 - 2
lvm.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.h,v 1.22 2000/05/08 19:32:53 roberto Exp roberto $
+** $Id: lvm.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -17,7 +17,7 @@
 #define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0))
 
 
-void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab);
+void luaV_pack (lua_State *L, StkId firstel);
 int luaV_tonumber (TObject *obj);
 int luaV_tostring (lua_State *L, TObject *obj);
 void luaV_gettable (lua_State *L, StkId top);