diff --git a/packages/regenerator-runtime/runtime.js b/packages/regenerator-runtime/runtime.js index 1859369d7..15fdacce8 100644 --- a/packages/regenerator-runtime/runtime.js +++ b/packages/regenerator-runtime/runtime.js @@ -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"; @@ -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. @@ -370,10 +390,6 @@ // unified ._invoke helper method. defineIteratorMethods(Gp); - Gp[iteratorSymbol] = function() { - return this; - }; - Gp[toStringTagSymbol] = "Generator"; Gp.toString = function() { diff --git a/test/non-native.js b/test/non-native.js index bd7ad702f..694ff1953 100644 --- a/test/non-native.js +++ b/test/non-native.js @@ -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); }); });