From 200474ff289dea77c696c0c025f4602405cf3fff Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 17 Mar 2023 11:35:54 -0700 Subject: [PATCH] [Fix] `filter`: IteratorClose needs to rethrow the error --- Iterator.prototype.filter/implementation.js | 3 +- test/Iterator.prototype.filter.js | 56 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Iterator.prototype.filter/implementation.js b/Iterator.prototype.filter/implementation.js index 31ce962..e5b19b5 100644 --- a/Iterator.prototype.filter/implementation.js +++ b/Iterator.prototype.filter/implementation.js @@ -9,6 +9,7 @@ var IsCallable = require('es-abstract/2022/IsCallable'); var IteratorClose = require('../aos/IteratorClose'); var IteratorStep = require('../aos/IteratorStep'); var IteratorValue = require('es-abstract/2022/IteratorValue'); +var ThrowCompletion = require('es-abstract/2022/ThrowCompletion'); var ToBoolean = require('es-abstract/2022/ToBoolean'); var GetIteratorDirect = require('../aos/GetIteratorDirect'); @@ -45,7 +46,7 @@ module.exports = function filter(predicate) { } } catch (e) { // close iterator // step 3.b.v, 3.b.vii - IteratorClose(iterated, true); + IteratorClose(iterated, ThrowCompletion(e)); throw e; } finally { counter += 1; // step 3.b.viii diff --git a/test/Iterator.prototype.filter.js b/test/Iterator.prototype.filter.js index 444e096..2694467 100644 --- a/test/Iterator.prototype.filter.js +++ b/test/Iterator.prototype.filter.js @@ -99,6 +99,62 @@ module.exports = { st.end(); }); + + t.test('262: test/built-ins/Iterator/prototype/filter/predicate-throws', function (st) { + var returnCalls = 0; + + var iter = { + next: function () { + return { + done: false, + value: 1 + }; + }, + 'return': function () { + returnCalls += 1; + return {}; + } + }; + + var callbackCalls = 0; + var iterator = filter(iter, function () { + callbackCalls += 1; + throw new SyntaxError(); + }); + + st['throws'](function () { iterator.next(); }, SyntaxError, 'next() throws'); + + st.equal(callbackCalls, 1); + st.equal(returnCalls, 1); + + st.end(); + }); + + t.test('262: test/built-ins/Iterator/prototype/filter/predicate-throws-then-closing-iterator-also-throws', function (st) { + var iter = { + next: function next() { + return { + done: false, + value: 1 + }; + }, + 'return': function () { + throw new EvalError(); + } + }; + + var iterator = filter(iter, function () { + throw new SyntaxError(); + }); + + st['throws']( + function () { iterator.next(); }, + SyntaxError, + 'when the predicate and return() both throw, the predicate’s exception wins' + ); + + st.end(); + }); }, index: function () { test('Iterator.prototype.' + fnName + ': index', function (t) {