Closed Bug 1360824 Opened 9 years ago Closed 9 years ago

Break in for-of loop over generator prevents generator from being resumed

Categories

(Core :: JavaScript Engine, defect)

53 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: brady.j.garvin, Assigned: shu)

References

Details

User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0 Build ID: 20170418123106 Steps to reproduce: 1. Navigate to about:blank in Firefox 53 2. Open Web Console 3. Run the following JavaScript code: function* f() { yield 0; console.log('Between 0 and 1'); yield 1; } const x = f(); for (const i of x) { console.log(i); break; } console.log(x.next().value); Actual results: Web Console displays: 0 undefined undefined Expected results: Web Console should display: 0 Between 0 and 1 1 undefined A workaround is to wrap the generator with a looped yield. For example: function* protect(x) { for (const i of x) { yield i; } } function* f() { yield 0; console.log('Between 0 and 1'); yield 1; } const x = f(); for (const i of protect(x)) { console.log(i); break; } console.log(x.next().value); The simpler function* protect(x) { yield* x; } while tempting, is not a workaround.
it's because of IteratorClose [1], that is implicitly performed when you escape the for-of loop [2] by break [3] > 7.4.6 IteratorClose ( iterator, completion ) > ... > 3. Let return be ? GetMethod(iterator, "return"). > 4. If return is undefined, return Completion(completion). > 5. Let innerResult be Call(return, iterator, « »). > 13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iterator, iterationKind, lhsKind, labelSet ) > ... > 5. Repeat, > ... > h. If status is an abrupt completion, then > ... > iii. Else, > .. > ii. Return ? IteratorClose(iterator, status). > 13.9.3 Runtime Semantics: Evaluation > BreakStatement: break; > 1. Return Completion{[[Type]]: break, [[Value]]: empty, [[Target]]: empty}. by IteratorClose, Generator.prototype.return [4] is called on the generator returned by "f()", and the generator returns from yield [5], and the generator.[[GeneratorState]] is set to "completed". > 25.3.1.3 Generator.prototype.return ( value ) > ... > 2. Let C be Completion{[[Type]]: return, [[Value]]: value, [[Target]]: empty}. > 3. Return ? GeneratorResumeAbrupt(g, C). > 25.3.3.5 GeneratorYield ( iterNextObj ) > ... > 7. Set the code evaluation state of genContext such that when evaluation is > resumed with a Completion resumptionValue the following steps will be performed: > a. Return resumptionValue. > 14.4.13 Runtime Semantics: Evaluation > YieldExpression: yield > 1. Return ? GeneratorYield(CreateIterResultObject(undefined, false)). the next Generator.prototype.next [7] does GeneratorResume [8], but it returns undefined because the state is "completed". > 25.3.1.2 Generator.prototype.next ( value ) > ... > 2. Return ? GeneratorResume(g, value). > 25.3.3.3 GeneratorResume ( generator, value ) > ... > 2. If state is "completed", return CreateIterResultObject(undefined, true). so, the actual bug here is that there's a workaround for it. (specifically, returning from yield inside for-of doesn't close the iterator) the testcase shouldn't print "Between 0 and 1" even if you wrap the generator. I'll file a bug for it. [1] https://tc39.github.io/ecma262/#sec-iteratorclose [2] https://tc39.github.io/ecma262/#sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset [3] https://tc39.github.io/ecma262/#sec-break-statement-runtime-semantics-evaluation [4] https://tc39.github.io/ecma262/#sec-generator.prototype.return [5] https://tc39.github.io/ecma262/#sec-generatoryield [6] https://tc39.github.io/ecma262/#sec-generatorstart [7] https://tc39.github.io/ecma262/#sec-generator.prototype.next [8] https://tc39.github.io/ecma262/#sec-generatorresume
See Also: → 1360839
to be clear, the "Actual results" you observed with the testcase is expected, per the spec, and the fact that it has a workaround is the implementation's bug (tracked in bug 1360839).
Status: UNCONFIRMED → RESOLVED
Closed: 9 years ago
Resolution: --- → INVALID
I'll take this.
Assignee: nobody → shu
Wait, I took the wrong bug. Meant to take 1360839.
You need to log in before you can comment on or make changes to this bug.