From 7f6f70853c8a2730fca2e95d5968ad52cf470bda Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Nov 2018 14:42:05 -0200 Subject: 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.) --- liolib.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'liolib.c') diff --git a/liolib.c b/liolib.c index b2a2fec8..7d6d51e6 100644 --- a/liolib.c +++ b/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; -- cgit v1.2.3-55-g6feb