Skip to content

Commit

Permalink
Fix es-iterator-helpers test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Oct 26, 2024
1 parent 8c1b23f commit bef3860
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 79 deletions.
33 changes: 21 additions & 12 deletions packages/npm/es-iterator-helpers/Iterator.concat/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ module.exports =
typeof IteratorConcat === 'function'
? IteratorConcat
: function concat(...iterables) {
// ECMAScript Standard Built-in Objects
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
// Built-in function objects that are not identified as constructors do
// not implement the [[Construct]] internal method unless otherwise
// specified in the description of a particular function.
if (new.target) {
throw new TypeErrorCtor('`Iterator.concat` is not a constructor')
}
Expand All @@ -36,37 +34,48 @@ module.exports =
for (let i = 0; i < length; i += 1) {
const iterable = iterables[i]
// Step 2.a: If item is not an Object, throw a TypeError exception.
ensureObject(iterable, 'iterable')
// Step 2.b: Let method be GetMethod(item, %Symbol.iterator%).
const method = getMethod(iterable, SymbolIterator)
// Step 2.c: If method is undefined, throw a TypeError exception.
if (method === undefined) {
throw new TypeErrorCtor(
'`Iterator.concat` requires all arguments to be iterable'
)
}
// (Handled by getMethod, which throws if method is missing or not callable.)
records[i] = {
iterable,
openMethod: getMethod(iterable, SymbolIterator)
}
}
let index = 0
let innerIterator = null
// Step 3: Let closure be a new Abstract Closure that captures iterables.
const closure = {
// This closure will perform the steps defined in Step 3.a.
next() {
const { length } = records
while (index < length) {
const { iterable, openMethod } = records[index]
// Step 3.a.i: Let iter be Call(iterable.[[OpenMethod]], iterable.[[Iterable]]).
const iterator = ReflectApply(openMethod, iterable, [])
// Step 3.a.ii: If iter is not an Object, throw a TypeError exception.
ensureObject(iterator)
if (!innerIterator) {
// If we haven't initialized an inner iterator yet, do so
const { iterable, openMethod } = records[index]
// Step 3.a.i: Let iter be Call(iterable.[[OpenMethod]], iterable.[[Iterable]]).
innerIterator = ReflectApply(openMethod, iterable, [])
// Step 3.a.ii: If iter is not an Object, throw a TypeError exception.
ensureObject(innerIterator, 'iterator')
}
// Step 3.a.iii: Let iteratorRecord be GetIteratorDirect(iter).
const { next } = getIteratorDirect(iterator)
const { next } = getIteratorDirect(innerIterator)
// Step 3.a.iv: Let innerAlive be true.
// (Handled by the loop structure)
// Step 3.a.v: Repeat, while innerAlive is true.
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(next, innerIterator, [])
if (!result.done) {
// Step 3.a.v.3.a: Yield the value of the iterator.
return result
}
// Step 3.a.v.2: If innerValue is done, move to the next iterable.
innerIterator = null
index += 1
}
// Step 3.b: Return Completion(undefined).
Expand Down
3 changes: 1 addition & 2 deletions packages/npm/es-iterator-helpers/Iterator.concat/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const impl = require('./implementation')
const Iterator = require('../Iterator/implementation')

module.exports = function getPolyfill() {
return typeof Iterator.concat === 'function' ? Iterator.concat : impl
return impl
}
6 changes: 3 additions & 3 deletions packages/npm/es-iterator-helpers/Iterator.concat/shim.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

const getPolyfill = require('./polyfill')
const Iterator = require('../Iterator/implementation')
const IteratorCtor = require('../Iterator/implementation')

const { defineProperty: ObjectDefineProperty } = Object

module.exports = function shimIteratorConcat() {
const polyfill = getPolyfill()
if (Iterator.concat !== polyfill) {
ObjectDefineProperty(Iterator, 'concat', {
if (IteratorCtor.concat !== polyfill) {
ObjectDefineProperty(IteratorCtor, 'concat', {
__proto__: null,
configurable: true,
enumerable: false,
Expand Down
50 changes: 34 additions & 16 deletions packages/npm/es-iterator-helpers/Iterator.from/implementation.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
'use strict'

const IteratorCtor = require('../Iterator/implementation')
const {
ReflectApply,
IteratorCtor: IteratorCtorRaw,
ObjectCreate,
TypeErrorCtor,
createIteratorFromClosure,
WrapForValidIteratorPrototype,
getIteratorFlattenable,
setUnderlyingIterator
setIterated
} = require('../shared')

module.exports = function from(O) {
if (new.target) {
throw new TypeErrorCtor('`Iterator.from` is not a constructor')
}
const { iterator, next } = getIteratorFlattenable(O)
const wrapper = createIteratorFromClosure({
next() {
return ReflectApply(next, iterator, [])
}
})
setUnderlyingIterator(wrapper, iterator)
return wrapper
}
const IteratorFrom = IteratorCtorRaw?.from

// Based specification text:
// https://tc39.es/ecma262/#sec-iterator.from
module.exports =
typeof IteratorFrom === 'function'
? IteratorFrom
: function from(O) {
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
if (new.target) {
throw new TypeErrorCtor('`Iterator.from` is not a constructor')
}
// Step 1: Let iteratorRecord be GetIteratorFlattenable(O, iterate-string-primitives).
const { iterator, next } = getIteratorFlattenable(O, true)
// Step 2: Let hasInstance be OrdinaryHasInstance(%Iterator%, iteratorRecord.[[Iterator]]).
// Step 3: If hasInstance is true, then
if (iterator instanceof IteratorCtor) {
// Step 3.a: Return iteratorRecord.[[Iterator]].
return iterator
}
// Step 4: Let wrapper be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, << [[Iterated]] >>).
const wrapper = ObjectCreate(WrapForValidIteratorPrototype)
// Step 5: Set wrapper.[[Iterated]] to iteratorRecord.
setIterated(wrapper, { iterator, next })
// Step 6: Return wrapper.
return wrapper
}
4 changes: 2 additions & 2 deletions packages/npm/es-iterator-helpers/Iterator.from/shim.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

const getPolyfill = require('./polyfill')
const Iterator = require('../Iterator/implementation')
const IteratorCtor = require('../Iterator/implementation')

const { defineProperty: ObjectDefineProperty } = Object

module.exports = function shimIteratorFrom() {
const polyfill = getPolyfill()
if (Iterator.from !== polyfill) {
ObjectDefineProperty(Iterator, 'from', {
ObjectDefineProperty(IteratorCtor, 'from', {
__proto__: null,
configurable: true,
enumerable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ const {
// Based specification text:
// https://tc39.es/ecma262/#sec-iterator.prototype.drop
module.exports = function drop(limit) {
// ECMAScript Standard Built-in Objects
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
// Built-in function objects that are not identified as constructors do
// not implement the [[Construct]] internal method unless otherwise
// specified in the description of a particular function.
if (new.target) {
throw new TypeErrorCtor('`drop` is not a constructor')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ const {
// Based specification text:
// https://tc39.es/ecma262/#sec-iterator.prototype.filter
module.exports = function filter(predicate) {
// ECMAScript Standard Built-in Objects
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
// Built-in function objects that are not identified as constructors do
// not implement the [[Construct]] internal method unless otherwise
// specified in the description of a particular function.
if (new.target) {
throw new TypeErrorCtor('`filter` is not a constructor')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ const {
} = require('../shared')

module.exports = function flatMap(mapper) {
// ECMAScript Standard Built-in Objects
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
// Built-in function objects that are not identified as constructors do
// not implement the [[Construct]] internal method unless otherwise
// specified in the description of a particular function.
if (new.target) {
throw new TypeErrorCtor('`flatMap` is not a constructor')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ const {
// Based on the specification text:
// https://tc39.es/ecma262/#sec-iterator.prototype.map
module.exports = function map(mapper) {
// ECMAScript Standard Built-in Objects
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
// Built-in function objects that are not identified as constructors do
// not implement the [[Construct]] internal method unless otherwise
// specified in the description of a particular function.
if (new.target) {
throw new TypeErrorCtor('`Iterator.map` is not a constructor')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ const {
} = require('../shared')

module.exports = function toArray() {
// Built-in functions that are not identified as constructors do
// not implement [[Construct]] unless otherwise specified.
// https://tc39.es/ecma262/#sec-ecmascript-standard-built-in-objects
if (new.target) {
throw new TypeErrorCtor('`toArray` is not a constructor')
}
ensureObject(this)
// Step 1: Let O be the this value.
const O = this
// Step 2: If O is not an Object, throw a TypeError exception.
ensureObject(O)
// Step 3: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(this)
// Step 4: Let items be a new empty List.
const items = []
// Step 5: Repeat.
while (true) {
// Step 5.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
// Step 5.b: If value is done, return CreateArrayFromList(items).
if (result.done) {
return items
}
// Step 5.c: Append value to items.
items[items.length] = result.value
}
}
6 changes: 3 additions & 3 deletions packages/npm/es-iterator-helpers/Iterator.prototype/shim.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

const getPolyfill = require('./polyfill')
const Iterator = require('../Iterator/implementation')
const IteratorCtor = require('../Iterator/implementation')

const { defineProperty: ObjectDefineProperty } = Object

module.exports = function shimIteratorProto() {
const polyfill = getPolyfill()
if (Iterator.prototype !== polyfill) {
ObjectDefineProperty(Iterator, 'prototype', {
if (IteratorCtor.prototype !== polyfill) {
ObjectDefineProperty(IteratorCtor, 'prototype', {
__proto__: null,
configurable: true,
enumerable: false,
Expand Down
37 changes: 21 additions & 16 deletions packages/npm/es-iterator-helpers/shared.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
declare interface IteratorResult<T> {
value: T
done: boolean
}
declare interface Iterator<T> {
next(value?: any): IteratorResult<T>
return?(value?: any): IteratorResult<T>
[Symbol.iterator](): Iterator<T>
}
declare interface IteratorHelper<T> extends Iterator<T> {
[Symbol.toStringTag](): 'Iterator Helper'
}
declare interface IteratorRecord<T> {
iterator: Iterator<T>
next: () => IteratorResult<T>
}
declare interface IteratorResult<T> {
value: T
done: boolean
}
declare interface InternalShared {
SLOT: WeakMap<any, any>
SLOT_GENERATOR_CONTEXT: string
SLOT_GENERATOR_STATE: string
SLOT_ITERATED: string
SLOT_UNDERLYING_ITERATOR: string
GENERATOR_STATE_COMPLETED: string
GENERATOR_STATE_SUSPENDED_STARTED: string
IteratorCtor: typeof globalThis.Iterator
IteratorPrototype: any
IteratorHelperPrototype: any
ArrayCtor: ArrayConstructor
IteratorPrototype: Iterator<any>
IteratorHelperPrototype: IteratorHelper<any>
WrapForValidIteratorPrototype: Iterator<any>
NumberCtor: NumberConstructor
MathTrunc: (x: number) => number
NegativeInfinity: number
Expand All @@ -30,17 +40,11 @@ declare interface InternalShared {
SymbolToStringTag: symbol
TypeErrorCtor: typeof TypeError
abruptCloseIterator(iterator: Iterator<any>, error: any): void
closeIterator<T>(iterator: Iterator<any>, completion: T): T
closeIterator<T>(iterator: Iterator<T>, completion: T): T
createIteratorFromClosure<T>(closure: Iterator<T>): Iterator<T>
ensureObject(thisArg: any, what?: string): void
getIteratorDirect<T>(obj: any): {
next: () => IteratorResult<T>
iterator: Iterator<T>
}
getIteratorFlattenable(obj: any): {
next: () => IteratorResult<any>
iterator: Iterator<any>
}
getIteratorDirect<T>(obj: any): IteratorRecord<T>
getIteratorFlattenable(obj: any): IteratorRecord<any>
getMethod(
obj: any,
key: string | symbol
Expand All @@ -53,7 +57,8 @@ declare interface InternalShared {
isObjectType(value: any): boolean
resolveSlots(O: any, slot: string): any
setSlot(O: any, slot: string, value: any): void
setUnderlyingIterator(generator: any, iterator: any): void
setUnderlyingIterator(generator: Iterator<any>, iterator: Iterator<any>): void
setIterated(wrapper: Iterator<any>, record: IteratorRecord<any>): void
toIntegerOrInfinity(value: any): number
}
declare const shared: InternalShared
Expand Down
Loading

0 comments on commit bef3860

Please sign in to comment.