get-iterator-method-only-once.js (1502B)
1 // |reftest| shell-option(--enable-iterator-sequencing) skip-if(!Iterator.concat||!xulRuntime.shell) -- iterator-sequencing is not enabled unconditionally, requires shell-options 2 // Copyright (C) 2024 André Bargull. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 5 /*--- 6 esid: sec-iterator.concat 7 description: > 8 Gets the iterator method from the input iterables only once. 9 info: | 10 Iterator.concat ( ...items ) 11 12 1. Let iterables be a new empty List. 13 2. For each element item of items, do 14 a. If item is not an Object, throw a TypeError exception. 15 b. Let method be ? GetMethod(item, %Symbol.iterator%). 16 c. If method is undefined, throw a TypeError exception. 17 d. Append the Record { [[OpenMethod]]: method, [[Iterable]]: item } to iterables. 18 ... 19 features: [iterator-sequencing] 20 includes: [compareArray.js] 21 ---*/ 22 23 let iteratorGets = 0; 24 let iteratorCalls = 0; 25 let array = [1, 2, 3]; 26 27 class CountingIterable { 28 get [Symbol.iterator]() { 29 ++iteratorGets; 30 31 return function () { 32 ++iteratorCalls; 33 return array[Symbol.iterator](); 34 }; 35 } 36 } 37 38 let iterable = new CountingIterable(); 39 40 assert.sameValue(iteratorGets, 0); 41 assert.sameValue(iteratorCalls, 0); 42 43 let iter = Iterator.concat(iterable); 44 45 assert.sameValue(iteratorGets, 1); 46 assert.sameValue(iteratorCalls, 0); 47 48 let result = [...iter]; 49 50 assert.sameValue(iteratorGets, 1); 51 assert.sameValue(iteratorCalls, 1); 52 53 assert.compareArray(result, array); 54 55 reportCompare(0, 0);