From 947a372f5860a76fcafb4a2845abc322e440d6fc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Oct 2018 14:54:45 -0300 Subject: State in generic 'for' acts as a to-be-closed variable The implicit variable 'state' in a generic 'for' is marked as a to-be-closed variable, so that the state will be closed as soon as the loop ends, no matter how. Taking advantage of this new facility, the call 'io.lines(filename)' now returns the open file as a second result. Therefore, an iteraction like 'for l in io.lines(name)...' will close the file even when the loop ends with a break or an error. --- lparser.c | 1 + 1 file changed, 1 insertion(+) (limited to 'lparser.c') diff --git a/lparser.c b/lparser.c index 9419f880..a5b84aa1 100644 --- a/lparser.c +++ b/lparser.c @@ -1413,6 +1413,7 @@ static void forlist (LexState *ls, TString *indexname) { /* create control variables */ new_localvarliteral(ls, "(for generator)"); new_localvarliteral(ls, "(for state)"); + markupval(fs, fs->nactvar); /* state may create an upvalue */ new_localvarliteral(ls, "(for control)"); /* create declared variables */ new_localvar(ls, indexname); -- cgit v1.2.3-55-g6feb