Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
Merge pull request #400 from ljharb/patch-1
Browse files Browse the repository at this point in the history
Conditionally set `Symbol.toStringTag`, since it may be nonwritable if already defined.
  • Loading branch information
benjamn authored Jul 21, 2020
2 parents 5c4ae4e + e8df307 commit 6a38750
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,21 @@ var runtime = (function (exports) {
IteratorPrototype = NativeIteratorPrototype;
}

function ensureDefaultToStringTag(object, defaultValue) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1644581#c6
return toStringTagSymbol in object
? object[toStringTagSymbol]
: object[toStringTagSymbol] = defaultValue;
}

var Gp = GeneratorFunctionPrototype.prototype =
Generator.prototype = Object.create(IteratorPrototype);
GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
GeneratorFunctionPrototype.constructor = GeneratorFunction;
GeneratorFunctionPrototype[toStringTagSymbol] =
GeneratorFunction.displayName = "GeneratorFunction";
GeneratorFunction.displayName = ensureDefaultToStringTag(
GeneratorFunctionPrototype,
"GeneratorFunction"
);

// Helper for defining the .next, .throw, and .return methods of the
// Iterator interface in terms of a single ._invoke method.
Expand All @@ -114,9 +123,7 @@ var runtime = (function (exports) {
Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
} else {
genFun.__proto__ = GeneratorFunctionPrototype;
if (!(toStringTagSymbol in genFun)) {
genFun[toStringTagSymbol] = "GeneratorFunction";
}
ensureDefaultToStringTag(genFun, "GeneratorFunction");
}
genFun.prototype = Object.create(Gp);
return genFun;
Expand Down Expand Up @@ -386,7 +393,7 @@ var runtime = (function (exports) {
// unified ._invoke helper method.
defineIteratorMethods(Gp);

Gp[toStringTagSymbol] = "Generator";
ensureDefaultToStringTag(Gp, "Generator");

// A Generator should always return itself as the iterator object when the
// @@iterator function is called on it. Some browsers' implementations of the
Expand Down
27 changes: 27 additions & 0 deletions test/non-writable-tostringtag-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 hasOwn = Object.prototype.hasOwnProperty;
var getProto = Object.getPrototypeOf;
var NativeIteratorPrototype =
typeof Symbol === "function" &&
typeof Symbol.iterator === "symbol" &&
typeof [][Symbol.iterator] === "function" &&
getProto &&
getProto(getProto([][Symbol.iterator]()));

// https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype-@@tostringtag
Object.defineProperty(NativeIteratorPrototype, Symbol.toStringTag, {
configurable: true,
value: "Iterator",
});

describe("Symbol.toStringTag safety (#399, #400)", function () {
it("regenerator-runtime doesn't fail to initialize when native iterator prototype has a non-writable @@toStringTag property", function() {
require("./runtime.js");
});
});
8 changes: 8 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ if (semver.gte(process.version, "4.0.0")) {
]);
}

if (semver.gte(process.version, "4.0.0")) {
enqueue("mocha", [
"--harmony",
"--reporter", "spec",
"./test/non-writable-tostringtag-property.js",
]);
}

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

0 comments on commit 6a38750

Please sign in to comment.