bug: scope of generic for variables is not sound
This commit is contained in:
5
bugs
5
bugs
@@ -298,3 +298,8 @@ Fri Aug 30 13:49:14 EST 2002
|
|||||||
>> userdata may not be cleared properly
|
>> userdata may not be cleared properly
|
||||||
(by Roberto; since 5.0a)
|
(by Roberto; since 5.0a)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Fri Dec 6 17:06:40 UTC 2002
|
||||||
|
>> scope of generic for variables is not sound
|
||||||
|
(by Gavin Wraith; since 5.0a)
|
||||||
|
|
||||||
|
|||||||
52
lparser.c
52
lparser.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lparser.c,v 1.199 2002/11/22 16:42:53 roberto Exp roberto $
|
** $Id: lparser.c,v 1.200 2002/12/04 17:38:31 roberto Exp roberto $
|
||||||
** Lua Parser
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -303,8 +303,8 @@ static void pushclosure (LexState *ls, FuncState *func, expdesc *v) {
|
|||||||
f->p[fs->np++] = func->f;
|
f->p[fs->np++] = func->f;
|
||||||
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
|
init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
|
||||||
for (i=0; i<func->f->nupvalues; i++) {
|
for (i=0; i<func->f->nupvalues; i++) {
|
||||||
luaK_exp2nextreg(fs, &func->upvalues[i]);
|
OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
|
||||||
fs->freereg--; /* CLOSURE will use these values */
|
luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1014,10 +1014,27 @@ static int exp1 (LexState *ls) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
|
||||||
|
BlockCnt bl;
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
int prep, endfor;
|
||||||
|
adjustlocalvars(ls, nvars); /* scope for all variables */
|
||||||
|
check(ls, TK_DO);
|
||||||
|
enterblock(fs, &bl, 1); /* loop block */
|
||||||
|
prep = luaK_getlabel(fs);
|
||||||
|
block(ls);
|
||||||
|
luaK_patchtohere(fs, prep-1);
|
||||||
|
endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
|
||||||
|
luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
|
||||||
|
luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
|
||||||
|
luaK_patchlist(fs, (isnum) ? endfor : luaK_jump(fs), prep);
|
||||||
|
leaveblock(fs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void fornum (LexState *ls, TString *varname, int line) {
|
static void fornum (LexState *ls, TString *varname, int line) {
|
||||||
/* fornum -> NAME = exp1,exp1[,exp1] DO body */
|
/* fornum -> NAME = exp1,exp1[,exp1] DO body */
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
int prep, endfor;
|
|
||||||
int base = fs->freereg;
|
int base = fs->freereg;
|
||||||
new_localvar(ls, varname, 0);
|
new_localvar(ls, varname, 0);
|
||||||
new_localvarstr(ls, "(for limit)", 1);
|
new_localvarstr(ls, "(for limit)", 1);
|
||||||
@@ -1032,16 +1049,9 @@ static void fornum (LexState *ls, TString *varname, int line) {
|
|||||||
luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
|
luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
|
||||||
luaK_reserveregs(fs, 1);
|
luaK_reserveregs(fs, 1);
|
||||||
}
|
}
|
||||||
adjustlocalvars(ls, 3); /* scope for control variables */
|
|
||||||
luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
|
luaK_codeABC(fs, OP_SUB, fs->freereg - 3, fs->freereg - 3, fs->freereg - 1);
|
||||||
luaK_jump(fs);
|
luaK_jump(fs);
|
||||||
prep = luaK_getlabel(fs);
|
forbody(ls, base, line, 3, 1);
|
||||||
check(ls, TK_DO);
|
|
||||||
block(ls);
|
|
||||||
luaK_patchtohere(fs, prep-1);
|
|
||||||
endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
|
|
||||||
luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
|
|
||||||
luaK_patchlist(fs, endfor, prep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1049,9 +1059,8 @@ static void forlist (LexState *ls, TString *indexname) {
|
|||||||
/* forlist -> NAME {,NAME} IN explist1 DO body */
|
/* forlist -> NAME {,NAME} IN explist1 DO body */
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
expdesc e;
|
expdesc e;
|
||||||
int line;
|
|
||||||
int nvars = 0;
|
int nvars = 0;
|
||||||
int prep;
|
int line;
|
||||||
int base = fs->freereg;
|
int base = fs->freereg;
|
||||||
new_localvarstr(ls, "(for generator)", nvars++);
|
new_localvarstr(ls, "(for generator)", nvars++);
|
||||||
new_localvarstr(ls, "(for state)", nvars++);
|
new_localvarstr(ls, "(for state)", nvars++);
|
||||||
@@ -1060,18 +1069,9 @@ static void forlist (LexState *ls, TString *indexname) {
|
|||||||
new_localvar(ls, str_checkname(ls), nvars++);
|
new_localvar(ls, str_checkname(ls), nvars++);
|
||||||
check(ls, TK_IN);
|
check(ls, TK_IN);
|
||||||
line = ls->linenumber;
|
line = ls->linenumber;
|
||||||
adjust_assign(ls, 3, explist1(ls, &e), &e);
|
adjust_assign(ls, nvars, explist1(ls, &e), &e);
|
||||||
luaK_reserveregs(fs, nvars - 3); /* registers for other variables */
|
|
||||||
luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
|
luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
|
||||||
adjustlocalvars(ls, nvars); /* scope for all variables */
|
forbody(ls, base, line, nvars, 0);
|
||||||
check(ls, TK_DO);
|
|
||||||
prep = luaK_getlabel(fs);
|
|
||||||
block(ls);
|
|
||||||
luaK_patchtohere(fs, prep-1);
|
|
||||||
removevars(fs->ls, fs->nactvar - nvars); /* deactivate locals for next op. */
|
|
||||||
luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
|
|
||||||
luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */
|
|
||||||
luaK_patchlist(fs, luaK_jump(fs), prep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1080,7 +1080,7 @@ static void forstat (LexState *ls, int line) {
|
|||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
TString *varname;
|
TString *varname;
|
||||||
BlockCnt bl;
|
BlockCnt bl;
|
||||||
enterblock(fs, &bl, 1);
|
enterblock(fs, &bl, 0); /* block to control variable scope */
|
||||||
next(ls); /* skip `for' */
|
next(ls); /* skip `for' */
|
||||||
varname = str_checkname(ls); /* first variable name */
|
varname = str_checkname(ls); /* first variable name */
|
||||||
switch (ls->t.token) {
|
switch (ls->t.token) {
|
||||||
|
|||||||
25
lvm.c
25
lvm.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lvm.c,v 1.270 2002/11/25 17:47:13 roberto Exp roberto $
|
** $Id: lvm.c,v 1.271 2002/12/04 17:38:31 roberto Exp roberto $
|
||||||
** Lua virtual machine
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -671,14 +671,23 @@ StkId luaV_execute (lua_State *L) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_TFORLOOP: {
|
case OP_TFORLOOP: {
|
||||||
setobjs2s(ra+4, ra+2);
|
int nvar = GETARG_C(i) + 1;
|
||||||
setobjs2s(ra+3, ra+1);
|
StkId cb = ra + nvar + 2; /* call base */
|
||||||
setobjs2s(ra+2, ra);
|
setobjs2s(cb, ra);
|
||||||
L->top = ra+5;
|
setobjs2s(cb+1, ra+1);
|
||||||
luaD_call(L, ra+2, GETARG_C(i) + 1);
|
setobjs2s(cb+2, ra+2);
|
||||||
|
L->top = cb+3; /* func. + 2 args (state and index) */
|
||||||
|
luaD_call(L, cb, nvar);
|
||||||
L->top = L->ci->top;
|
L->top = L->ci->top;
|
||||||
if (ttisnil(XRA(i)+2)) pc++; /* skip jump (break loop) */
|
ra = XRA(i); /* call may change stack */
|
||||||
else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
|
cb = ra + nvar + 2;
|
||||||
|
if (ttisnil(cb)) /* break loop? */
|
||||||
|
pc++; /* skip jump (break loop) */
|
||||||
|
else {
|
||||||
|
while (nvar--) /* move results to proper positions */
|
||||||
|
setobjs2s(ra+2+nvar, cb+nvar);
|
||||||
|
dojump(pc, GETARG_sBx(*pc) + 1); /* jump back */
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_TFORPREP: { /* for compatibility only */
|
case OP_TFORPREP: { /* for compatibility only */
|
||||||
|
|||||||
Reference in New Issue
Block a user