New compile option LUA_COMPAT_LOOPVAR

When on, this option makes for-loop control variables not read only.
This commit is contained in:
Roberto I
2026-01-16 16:38:44 -03:00
parent 2a7cf4f319
commit f5d1e8639b
2 changed files with 18 additions and 2 deletions

View File

@@ -1682,13 +1682,22 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
}
/*
** Control whether for-loop control variables are read-only
*/
#if defined(LUA_COMPAT_LOOPVAR)
#define LOOPVARKIND VDKREG
#else /* by default, these variables are read only */
#define LOOPVARKIND RDKCONST
#endif
static void fornum (LexState *ls, TString *varname, int line) {
/* fornum -> NAME = exp,exp[,exp] forbody */
FuncState *fs = ls->fs;
int base = fs->freereg;
new_localvarliteral(ls, "(for state)");
new_localvarliteral(ls, "(for state)");
new_varkind(ls, varname, RDKCONST); /* control variable */
new_varkind(ls, varname, LOOPVARKIND); /* control variable */
checknext(ls, '=');
exp1(ls); /* initial value */
checknext(ls, ',');
@@ -1715,7 +1724,7 @@ static void forlist (LexState *ls, TString *indexname) {
new_localvarliteral(ls, "(for state)"); /* iterator function */
new_localvarliteral(ls, "(for state)"); /* state */
new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */
new_varkind(ls, indexname, RDKCONST); /* control variable */
new_varkind(ls, indexname, LOOPVARKIND); /* control variable */
/* other declared variables */
while (testnext(ls, ',')) {
new_localvar(ls, str_checkname(ls));