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:
6
liolib.c
6
liolib.c
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user