Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the Iterator prototype have its own constructor #730

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make the Iterator prototype have its own constructor
  • Loading branch information
nikolaybotev committed Sep 13, 2024
commit 232e54543273cd1d8c156b5d86f78298465ad819
12 changes: 10 additions & 2 deletions packages/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,20 @@ var runtime = (function (exports) {

// This is a polyfill for %IteratorPrototype% for environments that
// don't natively support it.
var IteratorPrototype = {};
var getProto = Object.getPrototypeOf;
function Iterator() {
if (!IteratorPrototype.isPrototypeOf(this)) {
throw new TypeError("Iterator constructor called on non-iterator object");
}
if (getProto && getProto(this) === IteratorPrototype) {
throw new TypeError("Cannot instantiate abstract Iterator class");
}
}
var IteratorPrototype = Iterator.prototype;
define(IteratorPrototype, iteratorSymbol, function () {
return this;
});

var getProto = Object.getPrototypeOf;
var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
if (NativeIteratorPrototype &&
NativeIteratorPrototype !== Op &&
Expand Down
6 changes: 6 additions & 0 deletions test/tests.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,9 @@ describe("generator function prototype", function() {

it("should follow the expected object model", function() {
var GeneratorFunctionPrototype = getProto(f);
var GeneratorPrototype = GeneratorFunctionPrototype.prototype;
var IteratorPrototype = getProto(GeneratorPrototype);
var Iterator = IteratorPrototype.constructor;
var GeneratorFunction = GeneratorFunctionPrototype.constructor;

assert.strictEqual(GeneratorFunction.name, 'GeneratorFunction');
Expand All @@ -2636,6 +2639,9 @@ describe("generator function prototype", function() {
getProto(f.prototype));
assert.strictEqual(getProto(GeneratorFunctionPrototype),
Function.prototype);
assert.notStrictEqual(IteratorPrototype, Object);
assert.throws(() => Iterator.call({}));
assert.throws(() => new Iterator());

if (typeof process === "undefined" ||
process.version.slice(1, 3) === "0.") {
Expand Down