diff --git a/packages/runtime/runtime.js b/packages/runtime/runtime.js index 5593ca59d..47723ebea 100644 --- a/packages/runtime/runtime.js +++ b/packages/runtime/runtime.js @@ -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 && @@ -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" ); @@ -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); diff --git a/test/tests.es6.js b/test/tests.es6.js index b72ddf9cc..5a5cbac23 100644 --- a/test/tests.es6.js +++ b/test/tests.es6.js @@ -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'); @@ -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.") {