From 903a50726da73d0ffd5b57a7617bbcc2fd4bbc99 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 25 Mar 2021 02:23:55 +0800 Subject: [PATCH] Runtime - Support frozen intrinsics (#411) * Add failing test for runtime with frozen intrinsics * Add runtime support for frozen intrinsics * Add runtime support for frozen iterator prototypes --- packages/regenerator-runtime/runtime.js | 21 ++++++------- test/frozen-intrinsics.js | 39 +++++++++++++++++++++++++ test/run.js | 6 ++++ 3 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 test/frozen-intrinsics.js diff --git a/packages/regenerator-runtime/runtime.js b/packages/regenerator-runtime/runtime.js index 547b8c6af..f6a62dd3d 100644 --- a/packages/regenerator-runtime/runtime.js +++ b/packages/regenerator-runtime/runtime.js @@ -86,9 +86,9 @@ var runtime = (function (exports) { // This is a polyfill for %IteratorPrototype% for environments that // don't natively support it. var IteratorPrototype = {}; - IteratorPrototype[iteratorSymbol] = function () { + define(IteratorPrototype, iteratorSymbol, function () { return this; - }; + }); var getProto = Object.getPrototypeOf; var NativeIteratorPrototype = getProto && getProto(getProto(values([]))); @@ -102,8 +102,9 @@ var runtime = (function (exports) { var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); - GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype; - GeneratorFunctionPrototype.constructor = GeneratorFunction; + GeneratorFunction.prototype = GeneratorFunctionPrototype; + define(Gp, "constructor", GeneratorFunctionPrototype); + define(GeneratorFunctionPrototype, "constructor", GeneratorFunction); GeneratorFunction.displayName = define( GeneratorFunctionPrototype, toStringTagSymbol, @@ -217,9 +218,9 @@ var runtime = (function (exports) { } defineIteratorMethods(AsyncIterator.prototype); - AsyncIterator.prototype[asyncIteratorSymbol] = function () { + define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; - }; + }); exports.AsyncIterator = AsyncIterator; // Note that simple async functions are implemented on top of @@ -412,13 +413,13 @@ var runtime = (function (exports) { // iterator prototype chain incorrectly implement this, causing the Generator // object to not be returned from this call. This ensures that doesn't happen. // See /~https://github.com/facebook/regenerator/issues/274 for more details. - Gp[iteratorSymbol] = function() { + define(Gp, iteratorSymbol, function() { return this; - }; + }); - Gp.toString = function() { + define(Gp, "toString", function() { return "[object Generator]"; - }; + }); function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; diff --git a/test/frozen-intrinsics.js b/test/frozen-intrinsics.js new file mode 100644 index 000000000..9ae9ce303 --- /dev/null +++ b/test/frozen-intrinsics.js @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +var assert = require("assert"); + +var getPrototypeOf = Reflect.getPrototypeOf; +function getConstructorOf(obj) { + return getPrototypeOf(obj).constructor; +} + +var SymbolIterator = (typeof Symbol && Symbol.iterator) || '@@iterator'; +var ArrayIteratorObject = new Array()[SymbolIterator](); +var ArrayIteratorPrototype = getPrototypeOf(ArrayIteratorObject); +var IteratorPrototype = getPrototypeOf(ArrayIteratorPrototype); +async function* AsyncGeneratorFunctionInstance() {} +var AsyncGeneratorFunction = getConstructorOf( + AsyncGeneratorFunctionInstance, +); +var AsyncGenerator = AsyncGeneratorFunction.prototype; +var AsyncGeneratorPrototype = AsyncGenerator.prototype; +var AsyncIteratorPrototype = getPrototypeOf(AsyncGeneratorPrototype); + +// freeze relevant intrinsics +Object.freeze(Object.prototype); +Object.freeze(IteratorPrototype); +Object.freeze(ArrayIteratorPrototype); +Object.freeze(AsyncGenerator); +Object.freeze(AsyncGeneratorPrototype); +Object.freeze(AsyncIteratorPrototype); + +describe("Frozen intrinsics test", function () { + it("regenerator-runtime doesn't fail to initialize when Object prototype is frozen", function() { + require("./runtime.js"); + }); +}); diff --git a/test/run.js b/test/run.js index 7ff9291fe..7496f5774 100644 --- a/test/run.js +++ b/test/run.js @@ -141,6 +141,12 @@ if (semver.gte(process.version, "4.0.0")) { ]); } +enqueue("mocha", [ + "--harmony", + "--reporter", "spec", + "./test/frozen-intrinsics.js", +]); + enqueue(convert, [ "./test/tests.es6.js", "./test/tests.es5.js"