Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.
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
21 changes: 11 additions & 10 deletions packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([])));
Expand All @@ -102,8 +102,9 @@ var runtime = (function (exports) {

var Gp = GeneratorFunctionPrototype.prototype =
Generator.prototype = Object.create(IteratorPrototype);
Comment on lines 103 to 104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this line also perhaps trigger the same issue, since .prototype is also locked down?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is covered by the added tests. function-syntax classes have their own writable prototype property, so it is not affected by the override mistake

function Animal () {}
Reflect.getOwnPropertyDescriptor(Animal, 'prototype')
//=> Object { value: {…}, writable: true, enumerable: false, configurable: false }

GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
GeneratorFunctionPrototype.constructor = GeneratorFunction;
GeneratorFunction.prototype = GeneratorFunctionPrototype;
define(Gp, "constructor", GeneratorFunctionPrototype);
define(GeneratorFunctionPrototype, "constructor", GeneratorFunction);
GeneratorFunction.displayName = define(
GeneratorFunctionPrototype,
toStringTagSymbol,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] };
Expand Down
39 changes: 39 additions & 0 deletions test/frozen-intrinsics.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
6 changes: 6 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ if (semver.gte(process.version, "4.0.0")) {
]);
}

enqueue("mocha", [
"--harmony",
"--reporter", "spec",
"./test/frozen-intrinsics.js",
]);
Comment on lines +144 to +148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumavis Nice, this enqeue will run the tests you added in an isolated child process, so the Object.freeze stuff doesn't leak into other tests.


enqueue(convert, [
"./test/tests.es6.js",
"./test/tests.es5.js"
Expand Down