Skip to content

Commit

Permalink
[Fix] filter: IteratorClose needs to rethrow the error
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 17, 2023
1 parent 14aa2d8 commit 200474f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Iterator.prototype.filter/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions test/Iterator.prototype.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 200474f

Please sign in to comment.