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

Commit

Permalink
runtime: Use [[Define]] for the Symbol.toStringTag property (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Jul 22, 2020
1 parent 81042af commit 980ca64
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var runtime = (function (exports) {
"use strict";

var define = Object.defineProperty;
var Op = Object.prototype;
var hasOwn = Op.hasOwnProperty;
var undefined; // More compressible than void 0.
Expand All @@ -16,6 +17,15 @@ var runtime = (function (exports) {
var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";

if (define) {
try {
// IE 8 has a broken Object.defineProperty that only works on DOM objects
define({}, '', {});
} catch (err) {
define = undefined;
}
}

function wrap(innerFn, outerFn, self, tryLocsList) {
// If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
Expand Down Expand Up @@ -85,7 +95,13 @@ var runtime = (function (exports) {
function ensureDefaultToStringTag(object, defaultValue) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1644581#c6
return toStringTagSymbol in object
? object[toStringTagSymbol]
? (define && define(object, toStringTagSymbol, {
value: defaultValue,
enumerable: true,
configurable: true,
writable: true
}),
defaultValue)
: object[toStringTagSymbol] = defaultValue;
}

Expand Down
13 changes: 11 additions & 2 deletions test/non-writable-tostringtag-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

var hasOwn = Object.prototype.hasOwnProperty;
var assert = require("assert");

var getProto = Object.getPrototypeOf;
var NativeIteratorPrototype =
typeof Symbol === "function" &&
Expand All @@ -20,8 +21,16 @@ Object.defineProperty(NativeIteratorPrototype, Symbol.toStringTag, {
value: "Iterator",
});

describe("Symbol.toStringTag safety (#399, #400)", function () {
(NativeIteratorPrototype ? describe : describe.skip)("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");
});

it("regenerator-runtime's polyfilled generator prototype has the correct @@toStringTag value", function() {
require("./runtime.js");
function foo() {}
regeneratorRuntime.mark(foo);

assert.strictEqual(foo.prototype[Symbol.toStringTag], "Generator");
});
});

0 comments on commit 980ca64

Please sign in to comment.