Skip to content

Commit

Permalink
use getIteratorDirect for proper order of error throwing on iterato…
Browse files Browse the repository at this point in the history
…r helpers
  • Loading branch information
zloirock committed Jul 7, 2022
1 parent cd4c263 commit aeb28b6
Show file tree
Hide file tree
Showing 27 changed files with 78 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Changelog
##### Unreleased
- Nothing
- Fixed order of errors throwing on iterator helpers

##### [3.23.3 - 2022.06.26](https://github.com/zloirock/core-js/releases/tag/v3.23.3)
- Changed the order of operations in `%TypedArray%.prototype.toSpliced` following [proposal-change-array-by-copy/89](https://github.com/tc39/proposal-change-array-by-copy/issues/89)
Expand Down
6 changes: 3 additions & 3 deletions packages/core-js/internals/async-iterator-create-proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var create = require('../internals/object-create');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
Expand All @@ -20,9 +19,10 @@ var getInternalState = InternalStateModule.getterFor(ASYNC_ITERATOR_PROXY);
var TO_STRING_TAG = wellKnownSymbol('toStringTag');

module.exports = function (nextHandler, IS_ITERATOR) {
var AsyncIteratorProxy = function AsyncIterator(state) {
var AsyncIteratorProxy = function AsyncIterator(record, state) {
state.type = ASYNC_ITERATOR_PROXY;
state.next = aCallable(state.iterator.next);
state.iterator = record.iterator;
state.next = record.next;
state.done = false;
state.ignoreArgument = !IS_ITERATOR;
setInternalState(this, state);
Expand Down
8 changes: 5 additions & 3 deletions packages/core-js/internals/async-iterator-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
var getBuiltIn = require('../internals/get-built-in');
var getIteratorDirect = require('../internals/get-iterator-direct');
var getMethod = require('../internals/get-method');

var createMethod = function (TYPE) {
var IS_TO_ARRAY = TYPE == 0;
var IS_FOR_EACH = TYPE == 1;
var IS_EVERY = TYPE == 2;
var IS_SOME = TYPE == 3;
return function (iterator, fn, target) {
anObject(iterator);
return function (object, fn, target) {
var record = getIteratorDirect(object);
var Promise = getBuiltIn('Promise');
var next = aCallable(iterator.next);
var iterator = record.iterator;
var next = record.next;
var index = 0;
var MAPPING = fn !== undefined;
if (MAPPING || !IS_TO_ARRAY) aCallable(fn);
Expand Down
9 changes: 9 additions & 0 deletions packages/core-js/internals/get-iterator-direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');

module.exports = function (obj) {
return {
iterator: obj,
next: aCallable(anObject(obj).next)
};
};
7 changes: 5 additions & 2 deletions packages/core-js/internals/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var ResultPrototype = Result.prototype;
module.exports = function (iterable, unboundFunction, options) {
var that = options && options.that;
var AS_ENTRIES = !!(options && options.AS_ENTRIES);
var IS_RECORD = !!(options && options.IS_RECORD);
var IS_ITERATOR = !!(options && options.IS_ITERATOR);
var INTERRUPTED = !!(options && options.INTERRUPTED);
var fn = bind(unboundFunction, that);
Expand All @@ -38,7 +39,9 @@ module.exports = function (iterable, unboundFunction, options) {
} return INTERRUPTED ? fn(value, stop) : fn(value);
};

if (IS_ITERATOR) {
if (IS_RECORD) {
iterator = iterable.iterator;
} else if (IS_ITERATOR) {
iterator = iterable;
} else {
iterFn = getIteratorMethod(iterable);
Expand All @@ -53,7 +56,7 @@ module.exports = function (iterable, unboundFunction, options) {
iterator = getIterator(iterable, iterFn);
}

next = iterator.next;
next = IS_RECORD ? iterable.next : iterator.next;
while (!(step = call(next, iterator)).done) {
try {
result = callFn(step.value);
Expand Down
6 changes: 3 additions & 3 deletions packages/core-js/internals/iterator-create-proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var create = require('../internals/object-create');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
Expand All @@ -17,9 +16,10 @@ var getInternalState = InternalStateModule.getterFor(ITERATOR_PROXY);
var TO_STRING_TAG = wellKnownSymbol('toStringTag');

module.exports = function (nextHandler, IS_ITERATOR) {
var IteratorProxy = function Iterator(state) {
var IteratorProxy = function Iterator(record, state) {
state.type = ITERATOR_PROXY;
state.next = aCallable(state.iterator.next);
state.iterator = record.iterator;
state.next = record.next;
state.done = false;
state.ignoreArg = !IS_ITERATOR;
setInternalState(this, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');

var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
Expand All @@ -20,8 +21,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
asIndexedPairs: function asIndexedPairs() {
return new AsyncIteratorProxy({
iterator: anObject(this),
return new AsyncIteratorProxy(getIteratorDirect(this), {
index: 0
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var toPositiveInteger = require('../internals/to-positive-integer');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');

Expand Down Expand Up @@ -34,8 +35,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
drop: function drop(limit) {
return new AsyncIteratorProxy({
iterator: anObject(this),
return new AsyncIteratorProxy(getIteratorDirect(this), {
remaining: toPositiveInteger(limit)
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');

var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
Expand Down Expand Up @@ -35,8 +36,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
filter: function filter(filterer) {
return new AsyncIteratorProxy({
iterator: anObject(this),
return new AsyncIteratorProxy(getIteratorDirect(this), {
filterer: aCallable(filterer)
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
var getAsyncIterator = require('../internals/get-async-iterator');

Expand Down Expand Up @@ -55,8 +56,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
flatMap: function flatMap(mapper) {
return new AsyncIteratorProxy({
iterator: anObject(this),
return new AsyncIteratorProxy(getIteratorDirect(this), {
mapper: aCallable(mapper),
innerIterator: null,
innerNext: null
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var AsyncIteratorPrototype = require('../internals/async-iterator-prototype');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
var getAsyncIterator = require('../internals/get-async-iterator');
var getIterator = require('../internals/get-iterator');
var getIteratorDirect = require('../internals/get-iterator-direct');
var getIteratorMethod = require('../internals/get-iterator-method');
var getMethod = require('../internals/get-method');
var wellKnownSymbol = require('../internals/well-known-symbol');
Expand All @@ -32,6 +33,6 @@ $({ target: 'AsyncIterator', stat: true, forced: true }, {
usingIterator = getIteratorMethod(object);
if (usingIterator) return new AsyncFromSyncIterator(getIterator(object, usingIterator));
}
return new AsyncIteratorProxy({ iterator: iterator !== undefined ? iterator : object });
return new AsyncIteratorProxy(getIteratorDirect(iterator !== undefined ? iterator : object), {});
}
});
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');

var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {
Expand All @@ -23,8 +24,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
map: function map(mapper) {
return new AsyncIteratorProxy({
iterator: anObject(this),
return new AsyncIteratorProxy(getIteratorDirect(this), {
mapper: aCallable(mapper)
});
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getBuiltIn = require('../internals/get-built-in');
var getIteratorDirect = require('../internals/get-iterator-direct');

var Promise = getBuiltIn('Promise');
var $TypeError = TypeError;

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
reduce: function reduce(reducer /* , initialValue */) {
var iterator = anObject(this);
var next = aCallable(iterator.next);
var record = getIteratorDirect(this);
var iterator = record.iterator;
var next = record.next;
var noInitial = arguments.length < 2;
var accumulator = noInitial ? undefined : arguments[1];
aCallable(reducer);
Expand Down
5 changes: 2 additions & 3 deletions packages/core-js/modules/esnext.async-iterator.take.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var toPositiveInteger = require('../internals/to-positive-integer');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');

Expand All @@ -25,8 +25,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise, args) {

$({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
take: function take(limit) {
return new AsyncIteratorProxy({
iterator: anObject(this),
return new AsyncIteratorProxy(getIteratorDirect(this), {
remaining: toPositiveInteger(limit)
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.iterator.as-indexed-pairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createIteratorProxy = require('../internals/iterator-create-proxy');

var IteratorProxy = createIteratorProxy(function (args) {
Expand All @@ -13,8 +14,7 @@ var IteratorProxy = createIteratorProxy(function (args) {

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
asIndexedPairs: function asIndexedPairs() {
return new IteratorProxy({
iterator: anObject(this),
return new IteratorProxy(getIteratorDirect(this), {
index: 0
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.iterator.drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var toPositiveInteger = require('../internals/to-positive-integer');
var createIteratorProxy = require('../internals/iterator-create-proxy');

Expand All @@ -24,8 +25,7 @@ var IteratorProxy = createIteratorProxy(function (args) {

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
drop: function drop(limit) {
return new IteratorProxy({
iterator: anObject(this),
return new IteratorProxy(getIteratorDirect(this), {
remaining: toPositiveInteger(limit)
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core-js/modules/esnext.iterator.every.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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');

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
every: function every(fn) {
anObject(this);
var record = getIteratorDirect(this);
aCallable(fn);
return !iterate(this, function (value, stop) {
return !iterate(record, function (value, stop) {
if (!fn(value)) return stop();
}, { IS_ITERATOR: true, INTERRUPTED: true }).stopped;
}, { IS_RECORD: true, INTERRUPTED: true }).stopped;
}
});
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var apply = require('../internals/function-apply');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createIteratorProxy = require('../internals/iterator-create-proxy');
var callWithSafeIterationClosing = require('../internals/call-with-safe-iteration-closing');

Expand All @@ -23,8 +24,7 @@ var IteratorProxy = createIteratorProxy(function (args) {

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
filter: function filter(filterer) {
return new IteratorProxy({
iterator: anObject(this),
return new IteratorProxy(getIteratorDirect(this), {
filterer: aCallable(filterer)
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core-js/modules/esnext.iterator.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
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');

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
find: function find(fn) {
anObject(this);
var record = getIteratorDirect(this);
aCallable(fn);
return iterate(this, function (value, stop) {
return iterate(record, function (value, stop) {
if (fn(value)) return stop(value);
}, { IS_ITERATOR: true, INTERRUPTED: true }).result;
}, { IS_RECORD: true, INTERRUPTED: true }).result;
}
});
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var getIteratorMethod = require('../internals/get-iterator-method');
var createIteratorProxy = require('../internals/iterator-create-proxy');
var iteratorClose = require('../internals/iterator-close');
Expand Down Expand Up @@ -44,8 +45,7 @@ var IteratorProxy = createIteratorProxy(function () {

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
flatMap: function flatMap(mapper) {
return new IteratorProxy({
iterator: anObject(this),
return new IteratorProxy(getIteratorDirect(this), {
mapper: aCallable(mapper),
innerIterator: null,
innerNext: null
Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/modules/esnext.iterator.for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// https://github.com/tc39/proposal-iterator-helpers
var $ = require('../internals/export');
var iterate = require('../internals/iterate');
var anObject = require('../internals/an-object');
var getIteratorDirect = require('../internals/get-iterator-direct');

$({ target: 'Iterator', proto: true, real: true, forced: true }, {
forEach: function forEach(fn) {
iterate(anObject(this), fn, { IS_ITERATOR: true });
iterate(getIteratorDirect(this), fn, { IS_RECORD: true });
}
});
3 changes: 2 additions & 1 deletion packages/core-js/modules/esnext.iterator.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var isPrototypeOf = require('../internals/object-is-prototype-of');
var IteratorPrototype = require('../internals/iterators-core').IteratorPrototype;
var createIteratorProxy = require('../internals/iterator-create-proxy');
var getIterator = require('../internals/get-iterator');
var getIteratorDirect = require('../internals/get-iterator-direct');
var getIteratorMethod = require('../internals/get-iterator-method');

var IteratorProxy = createIteratorProxy(function (args) {
Expand All @@ -25,6 +26,6 @@ $({ target: 'Iterator', stat: true, forced: true }, {
if (isPrototypeOf(IteratorPrototype, iterator)) return iterator;
} else {
iterator = object;
} return new IteratorProxy({ iterator: iterator });
} return new IteratorProxy(getIteratorDirect(iterator), {});
}
});
Loading

0 comments on commit aeb28b6

Please sign in to comment.