To-be-closed variable in 'for' loop separated from the state

The variable to be closed in a generic 'for' loop now is the
4th value produced in the loop initialization, instead of being
the loop state (the 2nd value produced). That allows a loop to
use a state with a '__toclose' metamethod but do not close it.
(As an example, 'f:lines()' might use the file 'f' as a state
for the loop, but it should not close the file when the loop ends.)
This commit is contained in:
Roberto Ierusalimschy
2018-11-07 14:42:05 -02:00
parent b8fed93215
commit 7f6f70853c
6 changed files with 76 additions and 29 deletions

View File

@@ -386,8 +386,10 @@ static int io_lines (lua_State *L) {
}
aux_lines(L, toclose); /* push iteration function */
if (toclose) {
lua_pushvalue(L, 1); /* file will be second result */
return 2;
lua_pushnil(L); /* state */
lua_pushnil(L); /* control */
lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */
return 4;
}
else
return 1;