Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator helpers proposal changes #1101

Merged
merged 26 commits into from Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dccee6c
wrap `%AsyncFromSyncIteratorPrototype%`
zloirock Jul 6, 2022
e26a689
`RangeError` on `NaN` in `.drop` and `.take`
zloirock Jul 6, 2022
b21b574
rename `.asIndexedPairs` to `.indexed`
zloirock Jul 6, 2022
41f834e
don't pass arguments to methods from the extended iterators protocol
zloirock Jul 9, 2022
dcf4817
avoid extra promise creation
zloirock Jul 9, 2022
0730b22
drop `.throw` methods from all wrappers / helpers prototypes
zloirock Jul 9, 2022
f0055f8
fix the difference between wrapper and generator proxies
zloirock Jul 9, 2022
e5968c8
close inner iterators in `.flatMap` on `.return`
zloirock Jul 9, 2022
98f0e55
avoid exposing `AsyncFromSyncIterator` in `Iterator.prototype.toAsync`
zloirock Jul 9, 2022
80f3f55
cleanup some more unnecessary cases passing of arguments
zloirock Jul 9, 2022
932e954
use actual prototypes and tags for helpers / wrappers
zloirock Jul 10, 2022
a528f75
make helpers and wrappers `.next` and `.return` incompatible between …
zloirock Jul 11, 2022
da06219
update the changelog
zloirock Jul 11, 2022
9a41252
fix some cases of async iteration closing
zloirock Jul 12, 2022
a0c110a
fix some more cases of async iteration closing
zloirock Jul 12, 2022
0cebba5
fix one more cases of iteration closing
zloirock Jul 12, 2022
f237c4c
call `.next` on inner iterators of done iterator *wrappers*
zloirock Jul 18, 2022
59682b9
do not validate value in `%WrapForValidIteratorPrototype%.next`
zloirock Jul 18, 2022
a4d11b2
mark generators-wrappers as done on error in `.next`
zloirock Jul 18, 2022
f03ec27
mark async generators-wrappers as done on error in `.next`
zloirock Jul 18, 2022
c9ee9fb
avoid extra tick in `%AsyncIteratorHelper%`
zloirock Jul 18, 2022
5b51af9
avoid an extra promise
zloirock Jul 18, 2022
3b1f77f
rework async generators core
zloirock Jul 19, 2022
4d107e1
fix promise chain
zloirock Jul 19, 2022
ae6018e
simplify
zloirock Jul 19, 2022
5a2d421
improve a changelog note
zloirock Jul 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,17 @@
## Changelog
##### Unreleased
- Recent updates of the [iterator helpers proposal](https://github.com/tc39/proposal-iterator-helpers), [#1101](https://github.com/zloirock/core-js/issues/1101):
- `.asIndexedPairs` renamed to `.indexed`, [proposal-iterator-helpers/183](https://github.com/tc39/proposal-iterator-helpers/pull/183):
- `Iterator.prototype.asIndexedPairs` -> `Iterator.prototype.indexed`
- `AsyncIterator.prototype.asIndexedPairs` -> `AsyncIterator.prototype.indexed`
- Avoid exposing spec fiction `%AsyncFromSyncIteratorPrototype%` in `AsyncIterator.from` and `Iterator.prototype.toAsync`, [proposal-iterator-helpers/182](https://github.com/tc39/proposal-iterator-helpers/pull/182), [proposal-iterator-helpers/202](https://github.com/tc39/proposal-iterator-helpers/pull/202)
- Avoid unnecessary promise creation in `%WrapForValidAsyncIteratorPrototype%.next`, [proposal-iterator-helpers/197](https://github.com/tc39/proposal-iterator-helpers/pull/197)
- Do not validate value in `%WrapForValid(Async)IteratorPrototype%.next`, [proposal-iterator-helpers/197](https://github.com/tc39/proposal-iterator-helpers/pull/197) and [proposal-iterator-helpers/205](https://github.com/tc39/proposal-iterator-helpers/pull/205)
- Do not forward the parameter of `.next` / `.return` to an underlying iterator by the extended iterator protocol, a part of [proposal-iterator-helpers/194](https://github.com/tc39/proposal-iterator-helpers/pull/194)
- `.throw` methods removed from all wrappers / helpers prototypes, a part of [proposal-iterator-helpers/194](https://github.com/tc39/proposal-iterator-helpers/pull/194)
- Close inner iterators in `{ Iterator, AsyncIterator }.prototype.flatMap` on `.return`, [proposal-iterator-helpers/195](https://github.com/tc39/proposal-iterator-helpers/pull/195)
- Throw `RangeError` on `NaN` in `{ Iterator, AsyncIterator }.prototype.{ drop, take }`, [proposal-iterator-helpers/181](https://github.com/tc39/proposal-iterator-helpers/pull/181)
- Many other updates and fixes of this proposal
- Polyfill `Promise` with `unhandledrejection` event support (browser style) in Deno < [1.24](https://github.com/denoland/deno/releases/tag/v1.24.0)
- Added compat data for Bun, [#1103](https://github.com/zloirock/core-js/issues/1103)
- Added compat data for Hermes, [#1099](https://github.com/zloirock/core-js/issues/1099)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -2180,17 +2180,17 @@ correctionNeeded; // => [1, 1, 3]
core-js(-pure)/stage/2
```
##### [Iterator helpers](https://github.com/tc39/proposal-iterator-helpers)[⬆](#index)
Modules [`esnext.async-iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.constructor.js), [`esnext.async-iterator.as-indexed-pairs`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.as-indexed-pairs.js), [`esnext.async-iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.drop.js), [`esnext.async-iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.every.js), [`esnext.async-iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.filter.js), [`esnext.async-iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.find.js), [`esnext.async-iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.flat-map.js), [`esnext.async-iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.for-each.js), [`esnext.async-iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.from.js), [`esnext.async-iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.map.js), [`esnext.async-iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.reduce.js), [`esnext.async-iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.some.js), [`esnext.async-iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.take.js), [`esnext.async-iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.to-array.js), [`esnext.iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.constructor.js), [`esnext.iterator.as-indexed-pairs`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.as-indexed-pairs.js), [`esnext.iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.drop.js), [`esnext.iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.every.js), [`esnext.iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.filter.js), [`esnext.iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.find.js), [`esnext.iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.flat-map.js), [`esnext.iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.for-each.js), [`esnext.iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.from.js), [`esnext.iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.map.js), [`esnext.iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.reduce.js), [`esnext.iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.some.js), [`esnext.iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.take.js), [`esnext.iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-array.js) and [`esnext.iterator.to-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-async.js)
Modules [`esnext.async-iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.constructor.js), [`esnext.async-iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.drop.js), [`esnext.async-iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.every.js), [`esnext.async-iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.filter.js), [`esnext.async-iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.find.js), [`esnext.async-iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.flat-map.js), [`esnext.async-iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.for-each.js), [`esnext.async-iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.from.js), [`esnext.async-iterator.indexed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.indexed.js), [`esnext.async-iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.map.js), [`esnext.async-iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.reduce.js), [`esnext.async-iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.some.js), [`esnext.async-iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.take.js), [`esnext.async-iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.async-iterator.to-array.js), [`esnext.iterator.constructor`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.constructor.js), [`esnext.iterator.drop`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.drop.js), [`esnext.iterator.every`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.every.js), [`esnext.iterator.filter`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.filter.js), [`esnext.iterator.find`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.find.js), [`esnext.iterator.flat-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.flat-map.js), [`esnext.iterator.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.for-each.js), [`esnext.iterator.from`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.from.js), [`esnext.iterator.indexed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.indexed.js), [`esnext.iterator.map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.map.js), [`esnext.iterator.reduce`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.reduce.js), [`esnext.iterator.some`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.some.js), [`esnext.iterator.take`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.take.js), [`esnext.iterator.to-array`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-array.js) and [`esnext.iterator.to-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.to-async.js)
```js
class Iterator {
static from(iterable: Iterable<mixed>): Iterator<any>;
asIndexedPairs(): Iterator<[index, any]>;
drop(limit: uint): Iterator<any>;
every(callbackfn: value: any => boolean): boolean;
filter(callbackfn: value: any => boolean): Iterator<any>;
find(callbackfn: value: any => boolean)): any;
flatMap(callbackfn: value => any: Iterable): Iterator<any>;
forEach(callbackfn: value => void): void;
indexed(): Iterator<[index, any]>;
map(callbackfn: value => any): Iterator<any>;
reduce(callbackfn: (memo: any, value: any) => any, initialValue: any): any;
some(callbackfn: value: any => boolean): boolean;
Expand All @@ -2202,13 +2202,13 @@ class Iterator {

class AsyncIterator {
static from(iterable: Iterable<mixed>): AsyncIterator<any>;
asIndexedPairs(): AsyncIterator<[index, any]>;
drop(limit: uint): AsyncIterator<any>;
every(async callbackfn: value: any => boolean): Promise<boolean>;
filter(async callbackfn: value: any => boolean): AsyncIterator<any>;
find(async callbackfn: value: any => boolean)): Promise<any>;
flatMap(async callbackfn: value => any: Iterable): AsyncIterator<any>;
forEach(async callbackfn: value => void): Promise<void>;
indexed(): AsyncIterator<[index, any]>;
map(async callbackfn: value => any): AsyncIterator<any>;
reduce(async callbackfn: (memo: any, value: any) => any, initialValue: any): Promise<any>;
some(async callbackfn: value: any => boolean): Promise<boolean>;
Expand All @@ -2221,28 +2221,28 @@ class AsyncIterator {
```js
core-js/proposals/iterator-helpers
core-js(-pure)/full/async-iterator
core-js(-pure)/full/async-iterator/as-indexed-pairs
core-js(-pure)/full/async-iterator/drop
core-js(-pure)/full/async-iterator/every
core-js(-pure)/full/async-iterator/filter
core-js(-pure)/full/async-iterator/find
core-js(-pure)/full/async-iterator/flat-map
core-js(-pure)/full/async-iterator/for-each
core-js(-pure)/full/async-iterator/from
core-js(-pure)/full/async-iterator/indexed
core-js(-pure)/full/async-iterator/map
core-js(-pure)/full/async-iterator/reduce
core-js(-pure)/full/async-iterator/some
core-js(-pure)/full/async-iterator/take
core-js(-pure)/full/async-iterator/to-array
core-js(-pure)/full/iterator
core-js(-pure)/full/iterator/as-indexed-pairs
core-js(-pure)/full/iterator/drop
core-js(-pure)/full/iterator/every
core-js(-pure)/full/iterator/filter
core-js(-pure)/full/iterator/find
core-js(-pure)/full/iterator/flat-map
core-js(-pure)/full/iterator/for-each
core-js(-pure)/full/iterator/from
core-js(-pure)/full/iterator/indexed
core-js(-pure)/full/iterator/map
core-js(-pure)/full/iterator/reduce
core-js(-pure)/full/iterator/some
Expand Down
6 changes: 6 additions & 0 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -2080,6 +2080,7 @@ export const data = {
},
'esnext.async-iterator.constructor': {
},
// TODO: Remove from `core-js@4`
'esnext.async-iterator.as-indexed-pairs': {
},
'esnext.async-iterator.drop': {
Expand All @@ -2096,6 +2097,8 @@ export const data = {
},
'esnext.async-iterator.from': {
},
'esnext.async-iterator.indexed': {
},
'esnext.async-iterator.map': {
},
'esnext.async-iterator.reduce': {
Expand All @@ -2122,6 +2125,7 @@ export const data = {
'esnext.global-this': null,
'esnext.iterator.constructor': {
},
// TODO: Remove from `core-js@4`
'esnext.iterator.as-indexed-pairs': {
},
'esnext.iterator.drop': {
Expand All @@ -2138,6 +2142,8 @@ export const data = {
},
'esnext.iterator.from': {
},
'esnext.iterator.indexed': {
},
'esnext.iterator.map': {
},
'esnext.iterator.reduce': {
Expand Down
4 changes: 4 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Expand Up @@ -150,4 +150,8 @@ export default {
'esnext.array.group-to-map',
'esnext.symbol.metadata-key',
],
3.24: [
'esnext.async-iterator.indexed',
'esnext.iterator.indexed',
],
};
2 changes: 2 additions & 0 deletions packages/core-js/full/async-iterator/index.js
Expand Up @@ -3,6 +3,7 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.async-iterator.constructor');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.async-iterator.as-indexed-pairs');
require('../../modules/esnext.async-iterator.drop');
require('../../modules/esnext.async-iterator.every');
Expand All @@ -11,6 +12,7 @@ require('../../modules/esnext.async-iterator.find');
require('../../modules/esnext.async-iterator.flat-map');
require('../../modules/esnext.async-iterator.for-each');
require('../../modules/esnext.async-iterator.from');
require('../../modules/esnext.async-iterator.indexed');
require('../../modules/esnext.async-iterator.map');
require('../../modules/esnext.async-iterator.reduce');
require('../../modules/esnext.async-iterator.some');
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/full/async-iterator/indexed.js
@@ -0,0 +1,8 @@
require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/esnext.async-iterator.constructor');
require('../../modules/esnext.async-iterator.indexed');

var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('AsyncIterator', 'indexed');
2 changes: 2 additions & 0 deletions packages/core-js/full/iterator/index.js
Expand Up @@ -3,6 +3,7 @@ require('../../modules/es.object.to-string');
require('../../modules/es.promise');
require('../../modules/es.string.iterator');
require('../../modules/esnext.iterator.constructor');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.iterator.as-indexed-pairs');
require('../../modules/esnext.iterator.drop');
require('../../modules/esnext.iterator.every');
Expand All @@ -11,6 +12,7 @@ require('../../modules/esnext.iterator.find');
require('../../modules/esnext.iterator.flat-map');
require('../../modules/esnext.iterator.for-each');
require('../../modules/esnext.iterator.from');
require('../../modules/esnext.iterator.indexed');
require('../../modules/esnext.iterator.map');
require('../../modules/esnext.iterator.reduce');
require('../../modules/esnext.iterator.some');
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/full/iterator/indexed.js
@@ -0,0 +1,8 @@
require('../../modules/es.object.to-string');
require('../../modules/esnext.iterator.constructor');
require('../../modules/esnext.iterator.indexed');

var entryUnbind = require('../../internals/entry-unbind');

module.exports = entryUnbind('Iterator', 'indexed');

24 changes: 6 additions & 18 deletions packages/core-js/internals/async-from-sync-iterator.js
@@ -1,5 +1,5 @@
'use strict';
var apply = require('../internals/function-apply');
var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
var create = require('../internals/object-create');
var getMethod = require('../internals/get-method');
Expand Down Expand Up @@ -30,31 +30,19 @@ var AsyncFromSyncIterator = function AsyncIterator(iterator) {
};

AsyncFromSyncIterator.prototype = defineBuiltIns(create(AsyncIteratorPrototype), {
next: function next(arg) {
next: function next() {
var state = getInternalState(this);
var hasArg = !!arguments.length;
return new Promise(function (resolve, reject) {
var result = anObject(apply(state.next, state.iterator, hasArg ? [arg] : []));
var result = anObject(call(state.next, state.iterator));
asyncFromSyncIteratorContinuation(result, resolve, reject);
});
},
'return': function (arg) {
'return': function () {
var iterator = getInternalState(this).iterator;
var hasArg = !!arguments.length;
return new Promise(function (resolve, reject) {
var $return = getMethod(iterator, 'return');
if ($return === undefined) return resolve({ done: true, value: arg });
var result = anObject(apply($return, iterator, hasArg ? [arg] : []));
asyncFromSyncIteratorContinuation(result, resolve, reject);
});
},
'throw': function (arg) {
var iterator = getInternalState(this).iterator;
var hasArg = !!arguments.length;
return new Promise(function (resolve, reject) {
var $throw = getMethod(iterator, 'throw');
if ($throw === undefined) return reject(arg);
var result = anObject(apply($throw, iterator, hasArg ? [arg] : []));
if ($return === undefined) return resolve({ done: true, value: undefined });
var result = anObject(call($return, iterator));
asyncFromSyncIteratorContinuation(result, resolve, reject);
});
}
Expand Down
18 changes: 18 additions & 0 deletions packages/core-js/internals/async-iterator-close.js
@@ -0,0 +1,18 @@
var call = require('../internals/function-call');
var getBuiltIn = require('../internals/get-built-in');
var getMethod = require('../internals/get-method');

module.exports = function (iterator, method, argument, reject) {
try {
var returnMethod = getMethod(iterator, 'return');
if (returnMethod) {
return getBuiltIn('Promise').resolve(call(returnMethod, iterator)).then(function () {
method(argument);
}, function (error) {
reject(error);
});
}
} catch (error2) {
return reject(error2);
} method(argument);
};