Skip to content

Commit

Permalink
validate arguments before opening iterator in iterator helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 2, 2023
1 parent 9509bdd commit 1a3d541
Show file tree
Hide file tree
Showing 23 changed files with 59 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions packages/core-js/internals/async-iterator-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/internals/async-iterator-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/internals/get-iterator-direct.js
Original file line number Diff line number Diff line change
@@ -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)
};
};
3 changes: 1 addition & 2 deletions packages/core-js/internals/get-iterator-flattenable.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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');

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));
};
4 changes: 3 additions & 1 deletion packages/core-js/internals/iterator-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
};
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.take.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.every.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.some.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.iterator.take.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
3 changes: 2 additions & 1 deletion packages/core-js/modules/esnext.iterator.to-array.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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;
}
});
3 changes: 2 additions & 1 deletion packages/core-js/modules/esnext.iterator.to-async.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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)))));
}
});

0 comments on commit 1a3d541

Please sign in to comment.