diff --git a/CHANGELOG.md b/CHANGELOG.md index 4180cc76563b..fe44d5f42d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ##### Unreleased - [`Set` methods proposal](https://github.com/tc39/proposal-set-methods): - Removed sort from `Set.prototype.intersection`, [March 2023 TC39 meeting](https://github.com/babel/proposals/issues/87#issuecomment-1478610425), [proposal-set-methods/94](https://github.com/tc39/proposal-set-methods/pull/94) +- (Async) Iterator Helpers proposals ([sync](https://github.com/tc39/proposal-iterator-helpers), [async](https://github.com/tc39/proposal-async-iterator-helpers)): + - Validate arguments before opening iterator, [March 2023 TC39 meeting](https://github.com/babel/proposals/issues/87#issuecomment-1478412430), [proposal-iterator-helpers/265](https://github.com/tc39/proposal-iterator-helpers/pull/265) - (Async) Explicit Resource Management proposals ([sync](https://github.com/tc39/proposal-explicit-resource-management), [async](https://github.com/tc39/proposal-async-explicit-resource-management)): - `(Async)DisposableStack.prototype.move` marks the original stack as disposed, [#1226](https://github.com/zloirock/core-js/issues/1226) - Some simplifications like [proposal-explicit-resource-management/150](https://github.com/tc39/proposal-explicit-resource-management/pull/150) diff --git a/packages/core-js/internals/async-iterator-iteration.js b/packages/core-js/internals/async-iterator-iteration.js index 381bf236854d..4651467ad8eb 100644 --- a/packages/core-js/internals/async-iterator-iteration.js +++ b/packages/core-js/internals/async-iterator-iteration.js @@ -16,13 +16,14 @@ var createMethod = function (TYPE) { var IS_EVERY = TYPE == 2; var IS_SOME = TYPE == 3; return function (object, fn, target) { + anObject(object); + var MAPPING = fn !== undefined; + if (MAPPING || !IS_TO_ARRAY) aCallable(fn); var record = getIteratorDirect(object); var Promise = getBuiltIn('Promise'); var iterator = record.iterator; var next = record.next; var counter = 0; - var MAPPING = fn !== undefined; - if (MAPPING || !IS_TO_ARRAY) aCallable(fn); return new Promise(function (resolve, reject) { var ifAbruptCloseAsyncIterator = function (error) { diff --git a/packages/core-js/internals/async-iterator-map.js b/packages/core-js/internals/async-iterator-map.js index 6707ec958b89..6e333f950a19 100644 --- a/packages/core-js/internals/async-iterator-map.js +++ b/packages/core-js/internals/async-iterator-map.js @@ -49,7 +49,9 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) { // `AsyncIterator.prototype.map` method // https://github.com/tc39/proposal-iterator-helpers module.exports = function map(mapper) { + anObject(this); + aCallable(mapper); return new AsyncIteratorProxy(getIteratorDirect(this), { - mapper: aCallable(mapper) + mapper: mapper }); }; diff --git a/packages/core-js/internals/get-async-iterator-flattenable.js b/packages/core-js/internals/get-async-iterator-flattenable.js index e3d9ea1353dc..640d961cb6b2 100644 --- a/packages/core-js/internals/get-async-iterator-flattenable.js +++ b/packages/core-js/internals/get-async-iterator-flattenable.js @@ -18,7 +18,7 @@ module.exports = function from(obj) { method = getIteratorMethod(object); alreadyAsync = false; } - if (isCallable(method)) { + if (method !== undefined) { iterator = call(method, object); } else { iterator = object; diff --git a/packages/core-js/internals/get-iterator-direct.js b/packages/core-js/internals/get-iterator-direct.js index a21297df8d2e..b0f3b01ce1d8 100644 --- a/packages/core-js/internals/get-iterator-direct.js +++ b/packages/core-js/internals/get-iterator-direct.js @@ -1,9 +1,8 @@ var aCallable = require('../internals/a-callable'); -var anObject = require('../internals/an-object'); module.exports = function (obj) { return { iterator: obj, - next: aCallable(anObject(obj).next) + next: aCallable(obj.next) }; }; diff --git a/packages/core-js/internals/get-iterator-flattenable.js b/packages/core-js/internals/get-iterator-flattenable.js index c2eb28deb528..640cc09bd249 100644 --- a/packages/core-js/internals/get-iterator-flattenable.js +++ b/packages/core-js/internals/get-iterator-flattenable.js @@ -1,5 +1,4 @@ var call = require('../internals/function-call'); -var isCallable = require('../internals/is-callable'); var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); var getIteratorMethod = require('../internals/get-iterator-method'); @@ -7,5 +6,5 @@ var getIteratorMethod = require('../internals/get-iterator-method'); module.exports = function (obj) { var object = anObject(obj); var method = getIteratorMethod(object); - return getIteratorDirect(anObject(isCallable(method) ? call(method, object) : object)); + return getIteratorDirect(anObject(method !== undefined ? call(method, object) : object)); }; diff --git a/packages/core-js/internals/iterator-map.js b/packages/core-js/internals/iterator-map.js index 14612f76a29a..005abe03b2f5 100644 --- a/packages/core-js/internals/iterator-map.js +++ b/packages/core-js/internals/iterator-map.js @@ -16,7 +16,9 @@ var IteratorProxy = createIteratorProxy(function () { // `Iterator.prototype.map` method // https://github.com/tc39/proposal-iterator-helpers module.exports = function map(mapper) { + anObject(this); + aCallable(mapper); return new IteratorProxy(getIteratorDirect(this), { - mapper: aCallable(mapper) + mapper: mapper }); }; diff --git a/packages/core-js/modules/esnext.async-iterator.drop.js b/packages/core-js/modules/esnext.async-iterator.drop.js index 87a31446cf44..aca586d135b4 100644 --- a/packages/core-js/modules/esnext.async-iterator.drop.js +++ b/packages/core-js/modules/esnext.async-iterator.drop.js @@ -41,8 +41,10 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) { // https://github.com/tc39/proposal-async-iterator-helpers $({ target: 'AsyncIterator', proto: true, real: true }, { drop: function drop(limit) { + anObject(this); + var remaining = toPositiveInteger(notANaN(+limit)); return new AsyncIteratorProxy(getIteratorDirect(this), { - remaining: toPositiveInteger(notANaN(+limit)) + remaining: remaining }); } }); diff --git a/packages/core-js/modules/esnext.async-iterator.filter.js b/packages/core-js/modules/esnext.async-iterator.filter.js index 6476976526b5..bb4d4ab6218c 100644 --- a/packages/core-js/modules/esnext.async-iterator.filter.js +++ b/packages/core-js/modules/esnext.async-iterator.filter.js @@ -57,8 +57,10 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) { // https://github.com/tc39/proposal-async-iterator-helpers $({ target: 'AsyncIterator', proto: true, real: true }, { filter: function filter(predicate) { + anObject(this); + aCallable(predicate); return new AsyncIteratorProxy(getIteratorDirect(this), { - predicate: aCallable(predicate) + predicate: predicate }); } }); diff --git a/packages/core-js/modules/esnext.async-iterator.flat-map.js b/packages/core-js/modules/esnext.async-iterator.flat-map.js index 91b585767d02..e92c6adc8124 100644 --- a/packages/core-js/modules/esnext.async-iterator.flat-map.js +++ b/packages/core-js/modules/esnext.async-iterator.flat-map.js @@ -77,8 +77,10 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) { // https://github.com/tc39/proposal-async-iterator-helpers $({ target: 'AsyncIterator', proto: true, real: true }, { flatMap: function flatMap(mapper) { + anObject(this); + aCallable(mapper); return new AsyncIteratorProxy(getIteratorDirect(this), { - mapper: aCallable(mapper), + mapper: mapper, inner: null }); } diff --git a/packages/core-js/modules/esnext.async-iterator.reduce.js b/packages/core-js/modules/esnext.async-iterator.reduce.js index 6adbed13782a..0764aa2f4bfc 100644 --- a/packages/core-js/modules/esnext.async-iterator.reduce.js +++ b/packages/core-js/modules/esnext.async-iterator.reduce.js @@ -15,13 +15,14 @@ var $TypeError = TypeError; // https://github.com/tc39/proposal-async-iterator-helpers $({ target: 'AsyncIterator', proto: true, real: true }, { reduce: function reduce(reducer /* , initialValue */) { + anObject(this); + aCallable(reducer); var record = getIteratorDirect(this); var iterator = record.iterator; var next = record.next; var noInitial = arguments.length < 2; var accumulator = noInitial ? undefined : arguments[1]; var counter = 0; - aCallable(reducer); return new Promise(function (resolve, reject) { var ifAbruptCloseAsyncIterator = function (error) { diff --git a/packages/core-js/modules/esnext.async-iterator.take.js b/packages/core-js/modules/esnext.async-iterator.take.js index deb84c672499..34e40515fa71 100644 --- a/packages/core-js/modules/esnext.async-iterator.take.js +++ b/packages/core-js/modules/esnext.async-iterator.take.js @@ -38,8 +38,10 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) { // https://github.com/tc39/proposal-async-iterator-helpers $({ target: 'AsyncIterator', proto: true, real: true }, { take: function take(limit) { + anObject(this); + var remaining = toPositiveInteger(notANaN(+limit)); return new AsyncIteratorProxy(getIteratorDirect(this), { - remaining: toPositiveInteger(notANaN(+limit)) + remaining: remaining }); } }); diff --git a/packages/core-js/modules/esnext.iterator.drop.js b/packages/core-js/modules/esnext.iterator.drop.js index 5ec43dcab67f..5500812e04aa 100644 --- a/packages/core-js/modules/esnext.iterator.drop.js +++ b/packages/core-js/modules/esnext.iterator.drop.js @@ -26,8 +26,10 @@ var IteratorProxy = createIteratorProxy(function () { // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { drop: function drop(limit) { + anObject(this); + var remaining = toPositiveInteger(notANaN(+limit)); return new IteratorProxy(getIteratorDirect(this), { - remaining: toPositiveInteger(notANaN(+limit)) + remaining: remaining }); } }); diff --git a/packages/core-js/modules/esnext.iterator.every.js b/packages/core-js/modules/esnext.iterator.every.js index b66ab3320d43..32f470e0db20 100644 --- a/packages/core-js/modules/esnext.iterator.every.js +++ b/packages/core-js/modules/esnext.iterator.every.js @@ -2,15 +2,17 @@ var $ = require('../internals/export'); var iterate = require('../internals/iterate'); var aCallable = require('../internals/a-callable'); +var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); // `Iterator.prototype.every` method // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { every: function every(predicate) { + anObject(this); + aCallable(predicate); var record = getIteratorDirect(this); var counter = 0; - aCallable(predicate); return !iterate(record, function (value, stop) { if (!predicate(value, counter++)) return stop(); }, { IS_RECORD: true, INTERRUPTED: true }).stopped; diff --git a/packages/core-js/modules/esnext.iterator.filter.js b/packages/core-js/modules/esnext.iterator.filter.js index 8bf544f1280a..6001c246acd9 100644 --- a/packages/core-js/modules/esnext.iterator.filter.js +++ b/packages/core-js/modules/esnext.iterator.filter.js @@ -25,8 +25,10 @@ var IteratorProxy = createIteratorProxy(function () { // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { filter: function filter(predicate) { + anObject(this); + aCallable(predicate); return new IteratorProxy(getIteratorDirect(this), { - predicate: aCallable(predicate) + predicate: predicate }); } }); diff --git a/packages/core-js/modules/esnext.iterator.find.js b/packages/core-js/modules/esnext.iterator.find.js index 92d3101d33ce..37a717b7cb92 100644 --- a/packages/core-js/modules/esnext.iterator.find.js +++ b/packages/core-js/modules/esnext.iterator.find.js @@ -2,15 +2,17 @@ var $ = require('../internals/export'); var iterate = require('../internals/iterate'); var aCallable = require('../internals/a-callable'); +var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); // `Iterator.prototype.find` method // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { find: function find(predicate) { + anObject(this); + aCallable(predicate); var record = getIteratorDirect(this); var counter = 0; - aCallable(predicate); return iterate(record, function (value, stop) { if (predicate(value, counter++)) return stop(value); }, { IS_RECORD: true, INTERRUPTED: true }).result; diff --git a/packages/core-js/modules/esnext.iterator.flat-map.js b/packages/core-js/modules/esnext.iterator.flat-map.js index b0a722864481..8c0212eaad93 100644 --- a/packages/core-js/modules/esnext.iterator.flat-map.js +++ b/packages/core-js/modules/esnext.iterator.flat-map.js @@ -34,8 +34,10 @@ var IteratorProxy = createIteratorProxy(function () { // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { flatMap: function flatMap(mapper) { + anObject(this); + aCallable(mapper); return new IteratorProxy(getIteratorDirect(this), { - mapper: aCallable(mapper), + mapper: mapper, inner: null }); } diff --git a/packages/core-js/modules/esnext.iterator.for-each.js b/packages/core-js/modules/esnext.iterator.for-each.js index 105187eb424c..6fa7bc008472 100644 --- a/packages/core-js/modules/esnext.iterator.for-each.js +++ b/packages/core-js/modules/esnext.iterator.for-each.js @@ -2,15 +2,17 @@ var $ = require('../internals/export'); var iterate = require('../internals/iterate'); var aCallable = require('../internals/a-callable'); +var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); // `Iterator.prototype.forEach` method // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { forEach: function forEach(fn) { + anObject(this); + aCallable(fn); var record = getIteratorDirect(this); var counter = 0; - aCallable(fn); iterate(record, function (value) { fn(value, counter++); }, { IS_RECORD: true }); diff --git a/packages/core-js/modules/esnext.iterator.reduce.js b/packages/core-js/modules/esnext.iterator.reduce.js index 7cd8faf30e89..4c011e93d24f 100644 --- a/packages/core-js/modules/esnext.iterator.reduce.js +++ b/packages/core-js/modules/esnext.iterator.reduce.js @@ -2,6 +2,7 @@ var $ = require('../internals/export'); var iterate = require('../internals/iterate'); var aCallable = require('../internals/a-callable'); +var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); var $TypeError = TypeError; @@ -10,8 +11,9 @@ var $TypeError = TypeError; // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { reduce: function reduce(reducer /* , initialValue */) { - var record = getIteratorDirect(this); + anObject(this); aCallable(reducer); + var record = getIteratorDirect(this); var noInitial = arguments.length < 2; var accumulator = noInitial ? undefined : arguments[1]; var counter = 0; diff --git a/packages/core-js/modules/esnext.iterator.some.js b/packages/core-js/modules/esnext.iterator.some.js index 848d8fec5ad4..7d4454991d05 100644 --- a/packages/core-js/modules/esnext.iterator.some.js +++ b/packages/core-js/modules/esnext.iterator.some.js @@ -2,15 +2,17 @@ var $ = require('../internals/export'); var iterate = require('../internals/iterate'); var aCallable = require('../internals/a-callable'); +var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); // `Iterator.prototype.some` method // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { some: function some(predicate) { + anObject(this); + aCallable(predicate); var record = getIteratorDirect(this); var counter = 0; - aCallable(predicate); return iterate(record, function (value, stop) { if (predicate(value, counter++)) return stop(); }, { IS_RECORD: true, INTERRUPTED: true }).stopped; diff --git a/packages/core-js/modules/esnext.iterator.take.js b/packages/core-js/modules/esnext.iterator.take.js index 313832ca50d7..35f39cf811db 100644 --- a/packages/core-js/modules/esnext.iterator.take.js +++ b/packages/core-js/modules/esnext.iterator.take.js @@ -23,8 +23,10 @@ var IteratorProxy = createIteratorProxy(function () { // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { take: function take(limit) { + anObject(this); + var remaining = toPositiveInteger(notANaN(+limit)); return new IteratorProxy(getIteratorDirect(this), { - remaining: toPositiveInteger(notANaN(+limit)) + remaining: remaining }); } }); diff --git a/packages/core-js/modules/esnext.iterator.to-array.js b/packages/core-js/modules/esnext.iterator.to-array.js index 40f51ec36c3b..e34c35bcca43 100644 --- a/packages/core-js/modules/esnext.iterator.to-array.js +++ b/packages/core-js/modules/esnext.iterator.to-array.js @@ -1,5 +1,6 @@ 'use strict'; var $ = require('../internals/export'); +var anObject = require('../internals/an-object'); var iterate = require('../internals/iterate'); var getIteratorDirect = require('../internals/get-iterator-direct'); @@ -10,7 +11,7 @@ var push = [].push; $({ target: 'Iterator', proto: true, real: true }, { toArray: function toArray() { var result = []; - iterate(getIteratorDirect(this), push, { that: result, IS_RECORD: true }); + iterate(getIteratorDirect(anObject(this)), push, { that: result, IS_RECORD: true }); return result; } }); diff --git a/packages/core-js/modules/esnext.iterator.to-async.js b/packages/core-js/modules/esnext.iterator.to-async.js index 8a13480dfcbe..8f09e00442e6 100644 --- a/packages/core-js/modules/esnext.iterator.to-async.js +++ b/packages/core-js/modules/esnext.iterator.to-async.js @@ -1,5 +1,6 @@ 'use strict'; var $ = require('../internals/export'); +var anObject = require('../internals/an-object'); var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator'); var WrapAsyncIterator = require('../internals/async-iterator-wrap'); var getIteratorDirect = require('../internals/get-iterator-direct'); @@ -8,6 +9,6 @@ var getIteratorDirect = require('../internals/get-iterator-direct'); // https://github.com/tc39/proposal-async-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { toAsync: function toAsync() { - return new WrapAsyncIterator(getIteratorDirect(new AsyncFromSyncIterator(getIteratorDirect(this)))); + return new WrapAsyncIterator(getIteratorDirect(new AsyncFromSyncIterator(getIteratorDirect(anObject(this))))); } });