better control for compatibility code for varargs

This commit is contained in:
Roberto Ierusalimschy 2005-06-13 11:15:54 -03:00
parent 5fa2bec1f0
commit bf2b342ac1
4 changed files with 21 additions and 13 deletions

View File

@ -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
** 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) {
check(pt->maxstacksize <= MAXSTACK);
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);
return 1;
}
@ -440,7 +440,8 @@ static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
break;
}
case OP_VARARG: {
check(pt->is_vararg & NEWSTYLEVARARG);
check((pt->is_vararg & VARARG_ISVARARG) &&
!(pt->is_vararg & VARARG_NEEDSARG));
b--;
if (b == LUA_MULTRET) check(checkopenop(pt, pc));
checkreg(pt, a+b-1);

5
ldo.c
View File

@ -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
** See Copyright Notice in lua.h
*/
@ -199,8 +199,9 @@ static StkId adjust_varargs (lua_State *L, int nfixargs, int actual,
setnilvalue(L->top++);
}
#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 */
lua_assert(style & VARARG_HASARG);
luaC_checkGC(L);
htab = luaH_new(L, nvar, 1); /* create `arg' table */
for (i=0; i<nvar; i++) /* put extra arguments into `arg' table */

View File

@ -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
** See Copyright Notice in lua.h
*/
@ -254,8 +254,10 @@ typedef struct 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 {
@ -373,3 +375,4 @@ LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
#endif

View File

@ -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
** 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;
luaX_setinput(L, &lexstate, z, luaS_new(L, name));
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 */
chunk(&lexstate);
check(&lexstate, TK_EOS);
@ -572,9 +572,12 @@ static void parlist (LexState *ls) {
}
case TK_DOTS: { /* param -> `...' */
next(ls);
#if defined(LUA_COMPAT_VARARG)
/* use `arg' as default name */
new_localvarliteral(ls, "arg", nparams++);
f->is_vararg = 1;
f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
#endif
f->is_vararg |= VARARG_ISVARARG;
break;
}
default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
@ -582,7 +585,7 @@ static void parlist (LexState *ls) {
} while (!f->is_vararg && testnext(ls, ','));
}
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 */
}
@ -766,7 +769,7 @@ static void simpleexp (LexState *ls, expdesc *v) {
FuncState *fs = ls->fs;
check_condition(ls, fs->f->is_vararg,
"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));
break;
}