generator-close-via-continue.js (1891B)
1 // Copyright (C) 2017 André Bargull. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset 6 description: > 7 Generators should be closed via their `return` method when iteration is 8 interrupted via a `continue` statement. 9 info: | 10 13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet ) 11 ... 12 5. Repeat, 13 ... 14 i. Let result be the result of evaluating stmt. 15 ... 16 k. If LoopContinues(result, labelSet) is false, then 17 i. If iterationKind is enumerate, then 18 ... 19 ii. Else, 20 1. Assert: iterationKind is iterate. 21 2. Return ? IteratorClose(iteratorRecord, UpdateEmpty(result, V)). 22 ... 23 24 features: [generators] 25 ---*/ 26 27 var startedCount = 0; 28 var finallyCount = 0; 29 var iterationCount = 0; 30 function* values() { 31 startedCount += 1; 32 try { 33 yield; 34 throw new Test262Error('This code is unreachable (within `try` block)'); 35 } finally { 36 finallyCount += 1; 37 } 38 throw new Test262Error('This code is unreachable (following `try` statement)'); 39 } 40 var iterable = values(); 41 42 assert.sameValue( 43 startedCount, 0, 'Generator is initialized in suspended state' 44 ); 45 46 L: do { 47 for (var x of iterable) { 48 assert.sameValue( 49 startedCount, 1, 'Generator executes prior to first iteration' 50 ); 51 assert.sameValue( 52 finallyCount, 0, 'Generator is paused during first iteration' 53 ); 54 iterationCount += 1; 55 continue L; 56 } 57 } while (false); 58 59 assert.sameValue( 60 startedCount, 1, 'Generator does not restart following interruption' 61 ); 62 assert.sameValue(iterationCount, 1, 'A single iteration occurs'); 63 assert.sameValue( 64 finallyCount, 1, 'Generator is closed after `continue` statement' 65 ); 66 67 reportCompare(0, 0);