iterator-close-via-throw.js (1151B)
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 Iterators should be closed via their `return` method when iteration is 7 interrupted via a `throw` statement. 8 features: [Symbol.iterator] 9 ---*/ 10 11 var startedCount = 0; 12 var returnCount = 0; 13 var iterationCount = 0; 14 var iterable = {}; 15 16 iterable[Symbol.iterator] = function() { 17 return { 18 next: function() { 19 startedCount += 1; 20 return { done: false, value: null }; 21 }, 22 return: function() { 23 returnCount += 1; 24 return {}; 25 } 26 }; 27 }; 28 29 try { 30 for (var x of iterable) { 31 assert.sameValue( 32 startedCount, 1, 'Value is retrieved' 33 ); 34 assert.sameValue( 35 returnCount, 0, 'Iterator is not closed' 36 ); 37 iterationCount += 1; 38 throw 0; 39 } 40 } catch (err) {} 41 42 assert.sameValue( 43 startedCount, 1, 'Iterator does not restart following interruption' 44 ); 45 assert.sameValue(iterationCount, 1, 'A single iteration occurs'); 46 assert.sameValue( 47 returnCount, 1, 'Iterator is closed after `throw` statement' 48 ); 49 50 reportCompare(0, 0);