aboutsummaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-10-31 14:54:45 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-10-31 14:54:45 -0300
commit947a372f5860a76fcafb4a2845abc322e440d6fc (patch)
treee35840847207c850af5262f93f863f583d2af76d /lparser.c
parente073cbc2e538369e0611abfc9948f301aea6aef3 (diff)
downloadlua-947a372f5860a76fcafb4a2845abc322e440d6fc.tar.gz
lua-947a372f5860a76fcafb4a2845abc322e440d6fc.tar.bz2
lua-947a372f5860a76fcafb4a2845abc322e440d6fc.zip
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.
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c1
1 files changed, 1 insertions, 0 deletions
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) {
1413 /* create control variables */ 1413 /* create control variables */
1414 new_localvarliteral(ls, "(for generator)"); 1414 new_localvarliteral(ls, "(for generator)");
1415 new_localvarliteral(ls, "(for state)"); 1415 new_localvarliteral(ls, "(for state)");
1416 markupval(fs, fs->nactvar); /* state may create an upvalue */
1416 new_localvarliteral(ls, "(for control)"); 1417 new_localvarliteral(ls, "(for control)");
1417 /* create declared variables */ 1418 /* create declared variables */
1418 new_localvar(ls, indexname); 1419 new_localvar(ls, indexname);