generator-close-via-throw.js (1321B)
1 // Copyright (C) 2015 the V8 project authors. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 es6id: 13.6.4.13 5 description: > 6 Generators should be closed via their `return` method when iteration is 7 interrupted via a `throw` statement. 8 features: [generators] 9 ---*/ 10 11 var startedCount = 0; 12 var finallyCount = 0; 13 var iterationCount = 0; 14 function* values() { 15 startedCount += 1; 16 try { 17 yield; 18 throw new Test262Error('This code is unreachable (within `try` block)'); 19 } finally { 20 finallyCount += 1; 21 } 22 throw new Test262Error('This code is unreachable (following `try` statement)'); 23 } 24 var iterable = values(); 25 26 assert.sameValue( 27 startedCount, 0, 'Generator is initialized in suspended state' 28 ); 29 30 try { 31 for (var x of iterable) { 32 assert.sameValue( 33 startedCount, 1, 'Generator executes prior to first iteration' 34 ); 35 assert.sameValue( 36 finallyCount, 0, 'Generator is paused during first iteration' 37 ); 38 iterationCount += 1; 39 throw 0; 40 } 41 } catch(err) {} 42 43 assert.sameValue( 44 startedCount, 1, 'Generator does not restart following interruption' 45 ); 46 assert.sameValue(iterationCount, 1, 'A single iteration occurs'); 47 assert.sameValue( 48 finallyCount, 1, 'Generator is closed after `throw` statement' 49 ); 50 51 reportCompare(0, 0);