Skip to content

Commit

Permalink
[Fix] properly allow subclasses of Iterator to be constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 22, 2023
1 parent c2082fe commit 5cebe2a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Iterator/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ var $TypeError = GetIntrinsic('%TypeError%');
var $defineProperty = hasPropertyDescriptors && GetIntrinsic('%Object.defineProperty%', true);

var iterProto = require('iterator.prototype');
var callBound = require('call-bind/callBound');

var $isPrototypeOf = callBound('Object.prototype.isPrototypeOf');

var $Iterator = typeof Iterator === 'function' ? Iterator : function Iterator() {
throw new $TypeError('`Iterator` can not be called directly');
if (
!(this instanceof Iterator)
|| this.constructor === Iterator
|| !$isPrototypeOf(Iterator, this.constructor)
) {
throw new $TypeError('`Iterator` can not be called or constructed directly');
}
};

if ($Iterator.prototype !== iterProto) {
Expand Down
21 changes: 21 additions & 0 deletions test/Iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ module.exports = {
TypeError,
name + ' throws when Construct-ed'
);

var SubIter;
var SubSubIter;
try {
/* eslint no-new-func: 0 */
SubIter = Function('Iter', 'return class SubIter extends Iter {};')(Iter);
SubSubIter = Function('SubIter', 'return class SubSubIter extends SubIter {};')(SubIter);
} catch (e) { /**/ }

t.test('class inheritance', { skip: !SubIter }, function (st) {
st.doesNotThrow(
function () { return new SubIter(); },
'Extending ' + name + ' does not throw when Construct-ed'
);
st.doesNotThrow(
function () { return new SubSubIter(); },
'Extending ' + name + ' twice does not throw when Construct-ed'
);

st.end();
});
},
index: function () {
test('Iterator: index', function (t) {
Expand Down

0 comments on commit 5cebe2a

Please sign in to comment.