Skip to content

Commit

Permalink
fix some cases of safe iteration closing
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Aug 29, 2021
1 parent 9b260b3 commit 21bb08d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- `AsyncFromSyncIterator` made stricter, related mainly to `AsyncIterator.from` and `AsyncIterator.prototype.flatMap`
- Handling of optional `.next` arguments in `(Async)Iterator` methods is aligned with the current spec draft (mainly - ignoring the first passed to `.next` argument in built-in generators)
- Behavior of `.next`, `.return`, `.throw` methods on `AsyncIterator` helpers proxy iterators aligned with the current spec draft (built-in async generators) (mainly - some early errors moved to returned promises)
- Fixed some cases of safe iteration closing

##### 3.16.3 - 2021.08.25
- Fixed `CreateAsyncFromSyncIterator` semantic in `AsyncIterator.from`, related to [#765](https://github.com/zloirock/core-js/issues/765)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = function (iterator, fn, value, ENTRIES) {
try {
return ENTRIES ? fn(anObject(value)[0], value[1]) : fn(value);
} catch (error) {
iteratorClose(iterator);
throw error;
iteratorClose(iterator, 'throw', error);
}
};
5 changes: 2 additions & 3 deletions packages/core-js/internals/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (iterable, unboundFunction, options) {
var iterator, iterFn, index, length, result, next, step;

var stop = function (condition) {
if (iterator) iteratorClose(iterator);
if (iterator) iteratorClose(iterator, 'return', condition);
return new Result(true, condition);
};

Expand Down Expand Up @@ -51,8 +51,7 @@ module.exports = function (iterable, unboundFunction, options) {
try {
result = callFn(step.value);
} catch (error) {
iteratorClose(iterator);
throw error;
iteratorClose(iterator, 'throw', error);
}
if (typeof result == 'object' && result && result instanceof Result) return result;
} return new Result(false);
Expand Down
21 changes: 17 additions & 4 deletions packages/core-js/internals/iterator-close.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
var anObject = require('../internals/an-object');

module.exports = function (iterator) {
var returnMethod = iterator['return'];
if (returnMethod !== undefined) {
return anObject(returnMethod.call(iterator)).value;
module.exports = function (iterator, kind, value) {
var innerResult, innerError;
anObject(iterator);
try {
innerResult = iterator['return'];
if (innerResult === undefined) {
if (kind === 'throw') throw value;
return value;
}
innerResult = innerResult.call(iterator);
} catch (error) {
innerError = true;
innerResult = error;
}
if (kind === 'throw') throw value;
if (innerError) throw innerResult;
anObject(innerResult);
return value;
};
3 changes: 1 addition & 2 deletions packages/core-js/modules/esnext.iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ var IteratorProxy = createIteratorProxy(function () {
this.innerIterator = innerIterator = anObject(iteratorMethod.call(mapped));
this.innerNext = aFunction(innerIterator.next);
} catch (error) {
iteratorClose(iterator);
throw error;
iteratorClose(iterator, 'throw', error);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.iterator.take.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var IteratorProxy = createIteratorProxy(function (args) {
var iterator = this.iterator;
if (!this.remaining--) {
this.done = true;
return iteratorClose(iterator);
return iteratorClose(iterator, 'return', undefined);
}
var result = anObject(this.next.apply(iterator, args));
var done = this.done = !!result.done;
Expand Down

0 comments on commit 21bb08d

Please sign in to comment.