Skip to content

Commit

Permalink
Runtime - Support frozen intrinsics (#411)
Browse files Browse the repository at this point in the history
* Add failing test for runtime with frozen intrinsics
* Add runtime support for frozen intrinsics
* Add runtime support for frozen iterator prototypes
  • Loading branch information
kumavis authored Mar 24, 2021
1 parent 0c2aba1 commit 903a507
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
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);
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",
]);

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

0 comments on commit 903a507

Please sign in to comment.