Browse Source

better control for compatibility code for varargs

Roberto Ierusalimschy 20 years ago
parent
commit
bf2b342ac1
4 changed files with 21 additions and 13 deletions
  1. 4 3
      ldebug.c
  2. 3 2
      ldo.c
  3. 6 3
      lobject.h
  4. 8 5
      lparser.c

+ 4 - 3
ldebug.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldebug.c,v 2.20 2005/05/17 19:49:15 roberto Exp roberto $
+** $Id: ldebug.c,v 2.21 2005/05/31 14:25:18 roberto Exp roberto $
 ** Debug Interface
 ** Debug Interface
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -276,7 +276,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
 static int precheck (const Proto *pt) {
 static int precheck (const Proto *pt) {
   check(pt->maxstacksize <= MAXSTACK);
   check(pt->maxstacksize <= MAXSTACK);
   check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
   check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);
-  lua_assert(pt->numparams+pt->is_vararg <= pt->maxstacksize);
+  lua_assert(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);
   check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
   check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);
   return 1;
   return 1;
 }
 }
@@ -440,7 +440,8 @@ static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
         break;
         break;
       }
       }
       case OP_VARARG: {
       case OP_VARARG: {
-        check(pt->is_vararg & NEWSTYLEVARARG);
+        check((pt->is_vararg & VARARG_ISVARARG) &&
+             !(pt->is_vararg & VARARG_NEEDSARG));
         b--;
         b--;
         if (b == LUA_MULTRET) check(checkopenop(pt, pc));
         if (b == LUA_MULTRET) check(checkopenop(pt, pc));
         checkreg(pt, a+b-1);
         checkreg(pt, a+b-1);

+ 3 - 2
ldo.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldo.c,v 2.24 2005/05/20 19:09:05 roberto Exp roberto $
+** $Id: ldo.c,v 2.25 2005/05/31 14:25:18 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
 */
 */
@@ -199,8 +199,9 @@ static StkId adjust_varargs (lua_State *L, int nfixargs, int actual,
       setnilvalue(L->top++);
       setnilvalue(L->top++);
   }
   }
 #if defined(LUA_COMPAT_VARARG)
 #if defined(LUA_COMPAT_VARARG)
-  if (style != NEWSTYLEVARARG) {  /* compatibility with old-style vararg */
+  if (style & VARARG_NEEDSARG) {  /* compatibility with old-style vararg */
     int nvar = actual - nfixargs;  /* number of extra arguments */
     int nvar = actual - nfixargs;  /* number of extra arguments */
+    lua_assert(style & VARARG_HASARG);
     luaC_checkGC(L);
     luaC_checkGC(L);
     htab = luaH_new(L, nvar, 1);  /* create `arg' table */
     htab = luaH_new(L, nvar, 1);  /* create `arg' table */
     for (i=0; i<nvar; i++)  /* put extra arguments into `arg' table */
     for (i=0; i<nvar; i++)  /* put extra arguments into `arg' table */

+ 6 - 3
lobject.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lobject.h,v 2.14 2005/05/31 14:25:18 roberto Exp roberto $
+** $Id: lobject.h,v 2.15 2005/06/06 13:30:25 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -254,8 +254,10 @@ typedef struct Proto {
 } Proto;
 } Proto;
 
 
 
 
-/* mask for new-style vararg */
-#define NEWSTYLEVARARG		2
+/* masks for new-style vararg */
+#define VARARG_HASARG		1
+#define VARARG_ISVARARG		2
+#define VARARG_NEEDSARG		4
 
 
 
 
 typedef struct LocVar {
 typedef struct LocVar {
@@ -373,3 +375,4 @@ LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
 
 
 
 
 #endif
 #endif
+

+ 8 - 5
lparser.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lparser.c,v 2.27 2005/05/17 19:49:15 roberto Exp roberto $
+** $Id: lparser.c,v 2.28 2005/05/20 15:53:42 roberto Exp roberto $
 ** Lua Parser
 ** Lua Parser
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -402,7 +402,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
   lexstate.buff = buff;
   lexstate.buff = buff;
   luaX_setinput(L, &lexstate, z, luaS_new(L, name));
   luaX_setinput(L, &lexstate, z, luaS_new(L, name));
   open_func(&lexstate, &funcstate);
   open_func(&lexstate, &funcstate);
-  funcstate.f->is_vararg = NEWSTYLEVARARG;
+  funcstate.f->is_vararg = VARARG_ISVARARG;  /* main func. is always vararg */
   next(&lexstate);  /* read first token */
   next(&lexstate);  /* read first token */
   chunk(&lexstate);
   chunk(&lexstate);
   check(&lexstate, TK_EOS);
   check(&lexstate, TK_EOS);
@@ -572,9 +572,12 @@ static void parlist (LexState *ls) {
         }
         }
         case TK_DOTS: {  /* param -> `...' */
         case TK_DOTS: {  /* param -> `...' */
           next(ls);
           next(ls);
+#if defined(LUA_COMPAT_VARARG)
           /* use `arg' as default name */
           /* use `arg' as default name */
           new_localvarliteral(ls, "arg", nparams++);
           new_localvarliteral(ls, "arg", nparams++);
-          f->is_vararg = 1;
+          f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
+#endif
+          f->is_vararg |= VARARG_ISVARARG;
           break;
           break;
         }
         }
         default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
         default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
@@ -582,7 +585,7 @@ static void parlist (LexState *ls) {
     } while (!f->is_vararg && testnext(ls, ','));
     } while (!f->is_vararg && testnext(ls, ','));
   }
   }
   adjustlocalvars(ls, nparams);
   adjustlocalvars(ls, nparams);
-  f->numparams = fs->nactvar - f->is_vararg;
+  f->numparams = fs->nactvar - (f->is_vararg & VARARG_HASARG);
   luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
   luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
 }
 }
 
 
@@ -766,7 +769,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
       FuncState *fs = ls->fs;
       FuncState *fs = ls->fs;
       check_condition(ls, fs->f->is_vararg,
       check_condition(ls, fs->f->is_vararg,
                       "cannot use " LUA_QL("...") " outside a vararg function");
                       "cannot use " LUA_QL("...") " outside a vararg function");
-      fs->f->is_vararg = NEWSTYLEVARARG;
+      fs->f->is_vararg &= ~VARARG_NEEDSARG;  /* don't need 'arg' */
       init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
       init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));
       break;
       break;
     }
     }