浏览代码

keep `top' in registers when running basic tasks (settable, getglobal, ...)

Roberto Ierusalimschy 25 年之前
父节点
当前提交
99e340b2ba
共有 3 个文件被更改,包括 56 次插入64 次删除
  1. 8 6
      lapi.c
  2. 43 53
      lvm.c
  3. 5 5
      lvm.h

+ 8 - 6
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.68 2000/01/13 15:56:03 roberto Exp roberto $
+** $Id: lapi.c,v 1.69 2000/01/19 12:00:45 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -123,7 +123,7 @@ lua_Object lua_seterrormethod (lua_State *L) {
 
 
 lua_Object lua_gettable (lua_State *L) {
 lua_Object lua_gettable (lua_State *L) {
   luaA_checkCparams(L, 2);
   luaA_checkCparams(L, 2);
-  luaV_gettable(L);
+  luaV_gettable(L, L->top--);
   return luaA_putObjectOnTop(L);
   return luaA_putObjectOnTop(L);
 }
 }
 
 
@@ -139,10 +139,12 @@ lua_Object lua_rawgettable (lua_State *L) {
 
 
 
 
 void lua_settable (lua_State *L) {
 void lua_settable (lua_State *L) {
+  StkId top;
   luaA_checkCparams(L, 3);
   luaA_checkCparams(L, 3);
   luaD_checkstack(L, 3);  /* may need that to call a tag method */
   luaD_checkstack(L, 3);  /* may need that to call a tag method */
-  luaV_settable(L, L->top-3);
-  L->top -= 2;  /* pop table and index */
+  top = L->top;
+  luaV_settable(L, top-3, top);
+  L->top = top-3;  /* pop table, index, and value */
 }
 }
 
 
 
 
@@ -163,7 +165,7 @@ lua_Object lua_createtable (lua_State *L) {
 
 
 lua_Object lua_getglobal (lua_State *L, const char *name) {
 lua_Object lua_getglobal (lua_State *L, const char *name) {
   luaD_checkstack(L, 3);  /* may need that to call a tag method */
   luaD_checkstack(L, 3);  /* may need that to call a tag method */
-  luaV_getglobal(L, luaS_assertglobalbyname(L, name));
+  luaV_getglobal(L, luaS_assertglobalbyname(L, name), L->top++);
   return luaA_putObjectOnTop(L);
   return luaA_putObjectOnTop(L);
 }
 }
 
 
@@ -177,7 +179,7 @@ lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
 void lua_setglobal (lua_State *L, const char *name) {
 void lua_setglobal (lua_State *L, const char *name) {
   luaA_checkCparams(L, 1);
   luaA_checkCparams(L, 1);
   luaD_checkstack(L, 3);  /* may need that to call a tag method */
   luaD_checkstack(L, 3);  /* may need that to call a tag method */
-  luaV_setglobal(L, luaS_assertglobalbyname(L, name));
+  luaV_setglobal(L, luaS_assertglobalbyname(L, name), L->top--);
 }
 }
 
 
 
 

+ 43 - 53
lvm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lvm.c,v 1.80 2000/01/19 12:00:45 roberto Exp roberto $
+** $Id: lvm.c,v 1.81 2000/01/19 16:50:30 roberto Exp roberto $
 ** Lua virtual machine
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -103,13 +103,15 @@ void luaV_closure (lua_State *L, int nelems) {
 ** Function to index a table.
 ** Function to index a table.
 ** Receives the table at top-2 and the index at top-1.
 ** Receives the table at top-2 and the index at top-1.
 */
 */
-void luaV_gettable (lua_State *L) {
-  TObject *table = L->top-2;
+void luaV_gettable (lua_State *L, StkId top) {
+  TObject *table = top-2;
   const TObject *im;
   const TObject *im;
   if (ttype(table) != LUA_T_ARRAY) {  /* not a table, get gettable method */
   if (ttype(table) != LUA_T_ARRAY) {  /* not a table, get gettable method */
     im = luaT_getimbyObj(L, table, IM_GETTABLE);
     im = luaT_getimbyObj(L, table, IM_GETTABLE);
-    if (ttype(im) == LUA_T_NIL)
+    if (ttype(im) == LUA_T_NIL) {
+      L->top = top;
       luaG_indexerror(L, table);
       luaG_indexerror(L, table);
+    }
   }
   }
   else {  /* object is a table... */
   else {  /* object is a table... */
     int tg = table->value.a->htag;
     int tg = table->value.a->htag;
@@ -119,28 +121,29 @@ void luaV_gettable (lua_State *L) {
       if (ttype(h) == LUA_T_NIL &&
       if (ttype(h) == LUA_T_NIL &&
           (ttype(im=luaT_getim(L, tg, IM_INDEX)) != LUA_T_NIL)) {
           (ttype(im=luaT_getim(L, tg, IM_INDEX)) != LUA_T_NIL)) {
         /* result is nil and there is an `index' tag method */
         /* result is nil and there is an `index' tag method */
+        L->top = top;
         luaD_callTM(L, im, 2, 1);  /* calls it */
         luaD_callTM(L, im, 2, 1);  /* calls it */
       }
       }
-      else {
-        L->top--;
+      else
         *table = *h;  /* `push' result into table position */
         *table = *h;  /* `push' result into table position */
-      }
       return;
       return;
     }
     }
     /* else it has a `gettable' method, go through to next command */
     /* else it has a `gettable' method, go through to next command */
   }
   }
   /* object is not a table, or it has a `gettable' method */
   /* object is not a table, or it has a `gettable' method */
+  L->top = top;
   luaD_callTM(L, im, 2, 1);
   luaD_callTM(L, im, 2, 1);
 }
 }
 
 
 
 
 /*
 /*
-** Receives table at *t, index at *(t+1) and value at top.
+** Receives table at *t, index at *(t+1) and value at `top'.
 ** WARNING: caller must assure 3 extra stack slots (to call a tag method)
 ** WARNING: caller must assure 3 extra stack slots (to call a tag method)
 */
 */
-void luaV_settable (lua_State *L, StkId t) {
+void luaV_settable (lua_State *L, StkId t, StkId top) {
   const TObject *im;
   const TObject *im;
   if (ttype(t) != LUA_T_ARRAY) {  /* not a table, get `settable' method */
   if (ttype(t) != LUA_T_ARRAY) {  /* not a table, get `settable' method */
+    L->top = top;
     im = luaT_getimbyObj(L, t, IM_SETTABLE);
     im = luaT_getimbyObj(L, t, IM_SETTABLE);
     if (ttype(im) == LUA_T_NIL)
     if (ttype(im) == LUA_T_NIL)
       luaG_indexerror(L, t);
       luaG_indexerror(L, t);
@@ -148,20 +151,19 @@ void luaV_settable (lua_State *L, StkId t) {
   else {  /* object is a table... */
   else {  /* object is a table... */
     im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
     im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
     if (ttype(im) == LUA_T_NIL) {  /* and does not have a `settable' method */
     if (ttype(im) == LUA_T_NIL) {  /* and does not have a `settable' method */
-      luaH_set(L, avalue(t), t+1, L->top-1);
-      L->top--;  /* pop value */
+      luaH_set(L, avalue(t), t+1, top-1);
       return;
       return;
     }
     }
     /* else it has a `settable' method, go through to next command */
     /* else it has a `settable' method, go through to next command */
   }
   }
   /* object is not a table, or it has a `settable' method */
   /* object is not a table, or it has a `settable' method */
   /* prepare arguments and call the tag method */
   /* prepare arguments and call the tag method */
-  *(L->top+2) = *(L->top-1);
-  *(L->top+1) = *(t+1);
-  *(L->top) = *t;
-  *(L->top-1) = *im;
-  L->top += 3;
-  luaD_call(L, L->top-4, 0);
+  *(top+2) = *(top-1);
+  *(top+1) = *(t+1);
+  *(top) = *t;
+  *(top-1) = *im;
+  L->top = top+3;
+  luaD_call(L, top-1, 0);
 }
 }
 
 
 
 
@@ -178,18 +180,18 @@ void luaV_rawsettable (lua_State *L, StkId t) {
 /*
 /*
 ** WARNING: caller must assure 3 extra stack slots (to call a tag method)
 ** WARNING: caller must assure 3 extra stack slots (to call a tag method)
 */
 */
-void luaV_getglobal (lua_State *L, GlobalVar *gv) {
+void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
   const TObject *value = &gv->value;
   const TObject *value = &gv->value;
   TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
   TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
   if (ttype(im) == LUA_T_NIL)  /* is there a tag method? */
   if (ttype(im) == LUA_T_NIL)  /* is there a tag method? */
-    *L->top++ = *value;  /* default behavior */
+    *top = *value;  /* default behavior */
   else {  /* tag method */
   else {  /* tag method */
-    *L->top = *im;
-    ttype(L->top+1) = LUA_T_STRING;
-    tsvalue(L->top+1) = gv->name;  /* global name */
-    *(L->top+2) = *value;
-    L->top += 3;
-    luaD_call(L, L->top-3, 1);
+    *top = *im;
+    ttype(top+1) = LUA_T_STRING;
+    tsvalue(top+1) = gv->name;  /* global name */
+    *(top+2) = *value;
+    L->top = top+3;
+    luaD_call(L, top, 1);
   }
   }
 }
 }
 
 
@@ -197,19 +199,19 @@ void luaV_getglobal (lua_State *L, GlobalVar *gv) {
 /*
 /*
 ** WARNING: caller must assure 3 extra stack slots (to call a tag method)
 ** WARNING: caller must assure 3 extra stack slots (to call a tag method)
 */
 */
-void luaV_setglobal (lua_State *L, GlobalVar *gv) {
+void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
   const TObject *oldvalue = &gv->value;
   const TObject *oldvalue = &gv->value;
   const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
   const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
   if (ttype(im) == LUA_T_NIL)  /* is there a tag method? */
   if (ttype(im) == LUA_T_NIL)  /* is there a tag method? */
-    gv->value = *(--L->top);
+    gv->value = *(top-1);
   else {
   else {
-    *(L->top+2) = *(L->top-1);  /* new value */
-    *(L->top+1) = *oldvalue;
-    ttype(L->top) = LUA_T_STRING;
-    tsvalue(L->top) = gv->name;
-    *(L->top-1) = *im;
-    L->top += 3;
-    luaD_call(L, L->top-4, 0);
+    *(top+2) = *(top-1);  /* new value */
+    *(top+1) = *oldvalue;
+    ttype(top) = LUA_T_STRING;
+    tsvalue(top) = gv->name;
+    *(top-1) = *im;
+    L->top = top+3;
+    luaD_call(L, top-1, 0);
   }
   }
 }
 }
 
 
@@ -385,18 +387,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
 
 
       case GETGLOBALW: aux += highbyte(L, *pc++);
       case GETGLOBALW: aux += highbyte(L, *pc++);
       case GETGLOBAL:  aux += *pc++;
       case GETGLOBAL:  aux += *pc++;
-        L->top = top;
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
-        luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv);
+        luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv, top);
         top++;
         top++;
-        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case GETTABLE:
       case GETTABLE:
-        L->top = top;
-        luaV_gettable(L);
+        luaV_gettable(L, top);
         top--;
         top--;
-        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case GETDOTTEDW: aux += highbyte(L, *pc++);
       case GETDOTTEDW: aux += highbyte(L, *pc++);
@@ -404,10 +402,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         ttype(top) = LUA_T_STRING;
         ttype(top) = LUA_T_STRING;
         tsvalue(top++) = tsvalue(&consts[aux]);
         tsvalue(top++) = tsvalue(&consts[aux]);
-        L->top = top;
-        luaV_gettable(L);
+        luaV_gettable(L, top);
         top--;
         top--;
-        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case PUSHSELFW: aux += highbyte(L, *pc++);
       case PUSHSELFW: aux += highbyte(L, *pc++);
@@ -417,8 +413,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         ttype(top) = LUA_T_STRING;
         ttype(top) = LUA_T_STRING;
         tsvalue(top++) = tsvalue(&consts[aux]);
         tsvalue(top++) = tsvalue(&consts[aux]);
-        L->top = top;
-        luaV_gettable(L);
+        luaV_gettable(L, top);
         *(top-1) = receiver;
         *(top-1) = receiver;
         break;
         break;
       }
       }
@@ -439,23 +434,18 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
       case SETGLOBALW: aux += highbyte(L, *pc++);
       case SETGLOBALW: aux += highbyte(L, *pc++);
       case SETGLOBAL:  aux += *pc++;
       case SETGLOBAL:  aux += *pc++;
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
         LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type");
-        L->top = top;
-        luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv);
+        luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv, top);
         top--;
         top--;
-        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case SETTABLEPOP:
       case SETTABLEPOP:
-        L->top = top;
-        luaV_settable(L, top-3);
+        luaV_settable(L, top-3, top);
         top -= 3;  /* pop table, index, and value */
         top -= 3;  /* pop table, index, and value */
         break;
         break;
 
 
       case SETTABLE:
       case SETTABLE:
-        L->top = top;
-        luaV_settable(L, top-3-(*pc++));
+        luaV_settable(L, top-3-(*pc++), top);
         top--;  /* pop value */
         top--;  /* pop value */
-        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case SETLISTW: aux += highbyte(L, *pc++);
       case SETLISTW: aux += highbyte(L, *pc++);

+ 5 - 5
lvm.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lvm.h,v 1.13 1999/12/01 19:50:08 roberto Exp roberto $
+** $Id: lvm.h,v 1.14 2000/01/19 16:50:30 roberto Exp roberto $
 ** Lua virtual machine
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -21,11 +21,11 @@ void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab);
 int luaV_tonumber (TObject *obj);
 int luaV_tonumber (TObject *obj);
 int luaV_tostring (lua_State *L, TObject *obj);
 int luaV_tostring (lua_State *L, TObject *obj);
 void luaV_setn (lua_State *L, Hash *t, int val);
 void luaV_setn (lua_State *L, Hash *t, int val);
-void luaV_gettable (lua_State *L);
-void luaV_settable (lua_State *L, StkId t);
+void luaV_gettable (lua_State *L, StkId top);
+void luaV_settable (lua_State *L, StkId t, StkId top);
 void luaV_rawsettable (lua_State *L, StkId t);
 void luaV_rawsettable (lua_State *L, StkId t);
-void luaV_getglobal (lua_State *L, GlobalVar *gv);
-void luaV_setglobal (lua_State *L, GlobalVar *gv);
+void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top);
+void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top);
 StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base);
 StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base);
 void luaV_closure (lua_State *L, int nelems);
 void luaV_closure (lua_State *L, int nelems);
 void luaV_comparison (lua_State *L);
 void luaV_comparison (lua_State *L);