Debian Codegraph @

js/src/tests/test262/built-ins/Iterator/from/iterable-primitives.js

raw
1 // |reftest| shell-option(--enable-iterator-helpers) skip-if(!this.hasOwnProperty('Iterator')||!xulRuntime.shell) -- iterator-helpers is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2023 Michael Ficarra. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4 /*---
5 esid: sec-iterator.from
6 description: >
7 Iterator.from does not respect the iterability of any primitive except Strings
8 info: |
9 Iterator.from ( O )
10
11 1. If O is a String, set O to ! ToObject(O).
12 2. Let iteratorRecord be ? GetIteratorFlattenable(O).
13
14 includes: [compareArray.js]
15 features: [iterator-helpers]
16 flags: []
17 ---*/
18
19 function* g() {
20 yield 0;
21 }
22
23 Number.prototype[Symbol.iterator] = function* () {
24 let i = 0;
25 let target = this >>> 0;
26 while (i < target) {
27 yield i;
28 ++i;
29 }
30 };
31
32 assert.compareArray(Array.from(5), [0, 1, 2, 3, 4]);
33
34 assert.throws(TypeError, function () {
35 Iterator.from(5);
36 });
37
38 assert.compareArray(Array.from(Iterator.from(new Number(5))), [0, 1, 2, 3, 4]);
39
40 assert.compareArray(Array.from(Iterator.from('string')), ['s', 't', 'r', 'i', 'n', 'g']);
41
42 const originalStringIterator = String.prototype[Symbol.iterator];
43 let observedType;
44 Object.defineProperty(String.prototype, Symbol.iterator, {
45 get() {
46 'use strict';
47 observedType = typeof this;
48 return originalStringIterator;
49 }
50 });
51 Iterator.from('');
52 assert.sameValue(observedType, 'string');
53 Iterator.from(new String(''));
54 assert.sameValue(observedType, 'object');
55
56 reportCompare(0, 0);