tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

iterable-primitives.js (1332B)


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