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.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user