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

Commit f48768c

Browse files
authored
Merge pull request #402 from EB-Forks/fix/use-define-for-tostringtag
runtime: Use `[[Define]]` for the `Symbol.toStringTag` property
2 parents 81042af + d4868b6 commit f48768c

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

Diff for: packages/regenerator-runtime/runtime.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ var runtime = (function (exports) {
1616
var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
1717
var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
1818

19+
function define(obj, key, value) {
20+
Object.defineProperty(obj, key, {
21+
value: value,
22+
enumerable: true,
23+
configurable: true,
24+
writable: true
25+
});
26+
return obj[key];
27+
}
28+
try {
29+
// IE 8 has a broken Object.defineProperty that only works on DOM objects.
30+
define({}, "");
31+
} catch (err) {
32+
define = function(obj, key, value) {
33+
return obj[key] = value;
34+
};
35+
}
36+
1937
function wrap(innerFn, outerFn, self, tryLocsList) {
2038
// If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
2139
var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
@@ -82,29 +100,23 @@ var runtime = (function (exports) {
82100
IteratorPrototype = NativeIteratorPrototype;
83101
}
84102

85-
function ensureDefaultToStringTag(object, defaultValue) {
86-
// https://bugzilla.mozilla.org/show_bug.cgi?id=1644581#c6
87-
return toStringTagSymbol in object
88-
? object[toStringTagSymbol]
89-
: object[toStringTagSymbol] = defaultValue;
90-
}
91-
92103
var Gp = GeneratorFunctionPrototype.prototype =
93104
Generator.prototype = Object.create(IteratorPrototype);
94105
GeneratorFunction.prototype = Gp.constructor = GeneratorFunctionPrototype;
95106
GeneratorFunctionPrototype.constructor = GeneratorFunction;
96-
GeneratorFunction.displayName = ensureDefaultToStringTag(
107+
GeneratorFunction.displayName = define(
97108
GeneratorFunctionPrototype,
109+
toStringTagSymbol,
98110
"GeneratorFunction"
99111
);
100112

101113
// Helper for defining the .next, .throw, and .return methods of the
102114
// Iterator interface in terms of a single ._invoke method.
103115
function defineIteratorMethods(prototype) {
104116
["next", "throw", "return"].forEach(function(method) {
105-
prototype[method] = function(arg) {
117+
define(prototype, method, function(arg) {
106118
return this._invoke(method, arg);
107-
};
119+
});
108120
});
109121
}
110122

@@ -123,7 +135,7 @@ var runtime = (function (exports) {
123135
Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
124136
} else {
125137
genFun.__proto__ = GeneratorFunctionPrototype;
126-
ensureDefaultToStringTag(genFun, "GeneratorFunction");
138+
define(genFun, toStringTagSymbol, "GeneratorFunction");
127139
}
128140
genFun.prototype = Object.create(Gp);
129141
return genFun;
@@ -393,7 +405,7 @@ var runtime = (function (exports) {
393405
// unified ._invoke helper method.
394406
defineIteratorMethods(Gp);
395407

396-
ensureDefaultToStringTag(Gp, "Generator");
408+
define(Gp, toStringTagSymbol, "Generator");
397409

398410
// A Generator should always return itself as the iterator object when the
399411
// @@iterator function is called on it. Some browsers' implementations of the

Diff for: test/non-writable-tostringtag-property.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
var hasOwn = Object.prototype.hasOwnProperty;
8+
var assert = require("assert");
9+
910
var getProto = Object.getPrototypeOf;
1011
var NativeIteratorPrototype =
1112
typeof Symbol === "function" &&
@@ -20,8 +21,16 @@ Object.defineProperty(NativeIteratorPrototype, Symbol.toStringTag, {
2021
value: "Iterator",
2122
});
2223

23-
describe("Symbol.toStringTag safety (#399, #400)", function () {
24+
(NativeIteratorPrototype ? describe : describe.skip)("Symbol.toStringTag safety (#399, #400)", function () {
2425
it("regenerator-runtime doesn't fail to initialize when native iterator prototype has a non-writable @@toStringTag property", function() {
2526
require("./runtime.js");
2627
});
28+
29+
it("regenerator-runtime's polyfilled generator prototype has the correct @@toStringTag value", function() {
30+
require("./runtime.js");
31+
function foo() {}
32+
regeneratorRuntime.mark(foo);
33+
34+
assert.strictEqual(foo.prototype[Symbol.toStringTag], "Generator");
35+
});
2736
});

0 commit comments

Comments
 (0)