Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Fix generator prototype chain to more correctly match ES2015 spec #264

Merged
merged 4 commits into from
Dec 8, 2016
Merged
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
30 changes: 23 additions & 7 deletions packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
!(function(global) {
"use strict";

var hasOwn = Object.prototype.hasOwnProperty;
var Op = Object.prototype;
var hasOwn = Op.hasOwnProperty;
var undefined; // More compressible than void 0.
var $Symbol = typeof Symbol === "function" ? Symbol : {};
var iteratorSymbol = $Symbol.iterator || "@@iterator";
Expand Down Expand Up @@ -83,10 +84,29 @@
function GeneratorFunction() {}
function GeneratorFunctionPrototype() {}

var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype;
// This is a polyfill for %IteratorPrototype% for environments that
// don't natively support it.
var IteratorPrototype = {};
IteratorPrototype[iteratorSymbol] = function () {
return this;
};

var getProto = Object.getPrototypeOf;
var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
if (NativeIteratorPrototype &&
NativeIteratorPrototype !== Op &&
hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
// This environment has a native %IteratorPrototype%; use it instead
// of the polyfill.
IteratorPrototype = NativeIteratorPrototype;
}

var Gp = GeneratorFunctionPrototype.prototype =
Generator.prototype = Object.create(IteratorPrototype);
GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
GeneratorFunctionPrototype.constructor = GeneratorFunction;
GeneratorFunctionPrototype[toStringTagSymbol] = GeneratorFunction.displayName = "GeneratorFunction";
GeneratorFunctionPrototype[toStringTagSymbol] =
GeneratorFunction.displayName = "GeneratorFunction";

// Helper for defining the .next, .throw, and .return methods of the
// Iterator interface in terms of a single ._invoke method.
Expand Down Expand Up @@ -370,10 +390,6 @@
// unified ._invoke helper method.
defineIteratorMethods(Gp);

Gp[iteratorSymbol] = function() {
return this;
};

Gp[toStringTagSymbol] = "Generator";

Gp.toString = function() {
Expand Down
4 changes: 2 additions & 2 deletions test/non-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe("@@iterator", function() {
var iterator = gen();
assert.ok(!iterator.hasOwnProperty(Symbol.iterator));
assert.ok(!Object.getPrototypeOf(iterator).hasOwnProperty(Symbol.iterator));
assert.ok(Object.getPrototypeOf(
assert.ok(Object.getPrototypeOf(Object.getPrototypeOf(
Object.getPrototypeOf(iterator)
).hasOwnProperty(Symbol.iterator));
)).hasOwnProperty(Symbol.iterator));
assert.strictEqual(iterator[Symbol.iterator](), iterator);
});
});
Expand Down