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 all commits
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
28 changes: 17 additions & 11 deletions packages/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,23 @@ var runtime = (function (exports) {
// minifier not to mangle the names of these two functions.
function Generator() {}
function GeneratorFunction() {}
function GeneratorFunctionPrototype() {}

// 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 All @@ -101,17 +108,16 @@ var runtime = (function (exports) {
IteratorPrototype = NativeIteratorPrototype;
}

var Gp = GeneratorFunctionPrototype.prototype =
Generator.prototype = Object.create(IteratorPrototype);
GeneratorFunction.prototype = GeneratorFunctionPrototype;
defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: true });
var Gp = Generator.prototype = Object.create(IteratorPrototype);
GeneratorFunction.prototype = Generator;
defineProperty(Gp, "constructor", { value: Generator, configurable: true });
defineProperty(
GeneratorFunctionPrototype,
Generator,
"constructor",
{ value: GeneratorFunction, configurable: true }
);
GeneratorFunction.displayName = define(
GeneratorFunctionPrototype,
Generator,
toStringTagSymbol,
"GeneratorFunction"
);
Expand All @@ -138,9 +144,9 @@ var runtime = (function (exports) {

exports.mark = function(genFun) {
if (Object.setPrototypeOf) {
Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
Object.setPrototypeOf(genFun, Generator);
} else {
genFun.__proto__ = GeneratorFunctionPrototype;
genFun.__proto__ = Generator;
define(genFun, toStringTagSymbol, "GeneratorFunction");
}
genFun.prototype = Object.create(Gp);
Expand Down
7 changes: 7 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,10 @@ 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());
assert.strictEqual(f() instanceof f, true);

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