Explorar o código

little change when calling tag methods

Roberto Ierusalimschy %!s(int64=25) %!d(string=hai) anos
pai
achega
46ec57cbc6
Modificáronse 3 ficheiros con 58 adicións e 48 borrados
  1. 4 3
      lapi.c
  2. 15 21
      ldo.c
  3. 39 24
      lvm.c

+ 4 - 3
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.66 1999/12/27 17:33:22 roberto Exp roberto $
+** $Id: lapi.c,v 1.67 1999/12/30 18:27:03 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -157,6 +157,7 @@ lua_Object lua_rawgettable (lua_State *L) {
 
 
 void lua_settable (lua_State *L) {
 void lua_settable (lua_State *L) {
   luaA_checkCparams(L, 3);
   luaA_checkCparams(L, 3);
+  luaD_checkstack(L, 3);  /* may need that to call a tag method */
   luaV_settable(L, L->top-3);
   luaV_settable(L, L->top-3);
   L->top -= 2;  /* pop table and index */
   L->top -= 2;  /* pop table and index */
 }
 }
@@ -178,7 +179,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, 2);  /* 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));
   return luaA_putObjectOnTop(L);
   return luaA_putObjectOnTop(L);
 }
 }
@@ -192,7 +193,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, 2);  /* 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));
 }
 }
 
 

+ 15 - 21
ldo.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.c,v 1.63 1999/12/30 18:28:40 roberto Exp roberto $
+** $Id: ldo.c,v 1.64 1999/12/30 18:40:57 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
 */
 */
@@ -353,9 +353,10 @@ static int do_main (lua_State *L, ZIO *z, int bin) {
 void luaD_gcIM (lua_State *L, const TObject *o) {
 void luaD_gcIM (lua_State *L, const TObject *o) {
   const TObject *im = luaT_getimbyObj(L, o, IM_GC);
   const TObject *im = luaT_getimbyObj(L, o, IM_GC);
   if (ttype(im) != LUA_T_NIL) {
   if (ttype(im) != LUA_T_NIL) {
-    *L->top = *o;
-    incr_top;
-    luaD_callTM(L, im, 1, 0);
+    luaD_checkstack(L, 2);
+    *(L->top++) = *im;
+    *(L->top++) = *o;
+    luaD_call(L, L->top-2, 0);
   }
   }
 }
 }
 
 
@@ -365,25 +366,18 @@ void luaD_gcIM (lua_State *L, const TObject *o) {
 int lua_dofile (lua_State *L, const char *filename) {
 int lua_dofile (lua_State *L, const char *filename) {
   ZIO z;
   ZIO z;
   int status;
   int status;
-  int bin;
+  int bin;  /* flag for file mode */
+  int c;    /* look ahead char */
   char source[MAXFILENAME];
   char source[MAXFILENAME];
-  FILE *f;
+  FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
+  if (f == NULL) return 2;  /* unable to open file */
   luaL_filesource(source, filename, sizeof(source));
   luaL_filesource(source, filename, sizeof(source));
-  if (filename == NULL) {
-    f = stdin;
-    bin = 0;  /* cannot handle stdin as a binary file */
-  }
-  else {
-    int c;
-    f = fopen(filename, "r");
-    if (f == NULL) return 2;  /* unable to open file */
-    c = fgetc(f);
-    ungetc(c, f);
-    bin = (c == ID_CHUNK);
-    if (bin) {
-      f = freopen(filename, "rb", f);  /* set binary mode */
-      if (f == NULL) return 2;  /* unable to reopen file */
-    }
+  c = fgetc(f);
+  ungetc(c, f);
+  bin = (c == ID_CHUNK);
+  if (bin && f != stdin) {
+    f = freopen(filename, "rb", f);  /* set binary mode */
+    if (f == NULL) return 2;  /* unable to reopen file */
   }
   }
   luaZ_Fopen(&z, f, source);
   luaZ_Fopen(&z, f, source);
   status = do_main(L, &z, bin);
   status = do_main(L, &z, bin);

+ 39 - 24
lvm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lvm.c,v 1.77 1999/12/29 16:31:15 roberto Exp roberto $
+** $Id: lvm.c,v 1.78 1999/12/30 18:28:40 roberto Exp roberto $
 ** Lua virtual machine
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -33,8 +33,11 @@
 #define highbyte(L, x)	((x)<<8)
 #define highbyte(L, x)	((x)<<8)
 
 
 
 
-/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
-#define	EXTRA_STACK	6
+/*
+** Extra stack size to run a function:
+** LUA_T_LINE(1), NAME(1), TM calls(3) (plus some extra...)
+*/
+#define	EXTRA_STACK	8
 
 
 
 
 
 
@@ -133,6 +136,7 @@ void luaV_gettable (lua_State *L) {
 
 
 /*
 /*
 ** 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)
 */
 */
 void luaV_settable (lua_State *L, StkId t) {
 void luaV_settable (lua_State *L, StkId t) {
   const TObject *im;
   const TObject *im;
@@ -152,11 +156,12 @@ void luaV_settable (lua_State *L, StkId t) {
   }
   }
   /* 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+1) = *(L->top-1);
-  *(L->top) = *(t+1);
-  *(L->top-1) = *t;
-  L->top += 2;  /* WARNING: caller must assure stack space */
-  luaD_callTM(L, im, 3, 0);
+  *(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);
 }
 }
 
 
 
 
@@ -170,36 +175,41 @@ void luaV_rawsettable (lua_State *L, StkId t) {
 }
 }
 
 
 
 
+/*
+** 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) {
-  /* WARNING: caller must assure stack space */
   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? */
-    ttype(L->top) = LUA_T_STRING;
-    tsvalue(L->top) = gv->name;  /* global name */
-    L->top++;
-    *L->top++ = *value;
-    luaD_callTM(L, im, 2, 1);
-  } else {  /* no tag method */
+  if (ttype(im) == LUA_T_NIL)  /* is there a tag method? */
     *L->top++ = *value;  /* default behavior */
     *L->top++ = *value;  /* default behavior */
+  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);
   }
   }
 }
 }
 
 
 
 
+/*
+** 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) {
   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 = *(--L->top);
   else {
   else {
-    /* WARNING: caller must assure stack space */
-    TObject newvalue;
-    newvalue = *(L->top-1);
-    ttype(L->top-1) = LUA_T_STRING;
-    tsvalue(L->top-1) = gv->name;
-    *L->top++ = *oldvalue;
-    *L->top++ = newvalue;
-    luaD_callTM(L, im, 3, 0);
+    *(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);
   }
   }
 }
 }
 
 
@@ -372,12 +382,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         L->top = top;
         L->top = top;
         luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv);
         luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv);
         top++;
         top++;
+        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case GETTABLE:
       case GETTABLE:
         L->top = top;
         L->top = top;
         luaV_gettable(L);
         luaV_gettable(L);
         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++);
@@ -386,6 +398,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         L->top = top;
         L->top = top;
         luaV_gettable(L);
         luaV_gettable(L);
         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,6 +430,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         L->top = top;
         L->top = top;
         luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv);
         luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv);
         top--;
         top--;
+        LUA_ASSERT(L, top==L->top, "top's not synchronized");
         break;
         break;
 
 
       case SETTABLEPOP:
       case SETTABLEPOP:
@@ -429,6 +443,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
         L->top = top;
         L->top = top;
         luaV_settable(L, top-3-(*pc++));
         luaV_settable(L, top-3-(*pc++));
         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++);