Skip to content

Commit

Permalink
Use helper function to define Symbol.toStringTag unobtrusively.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jul 21, 2020
1 parent 1a0bcbf commit e8df307
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +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;
if (!(toStringTagSymbol in GeneratorFunctionPrototype)) {
GeneratorFunctionPrototype[toStringTagSymbol] = "GeneratorFunction";
}
GeneratorFunction.displayName = GeneratorFunctionPrototype[toStringTagSymbol];
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 @@ -116,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 @@ -388,9 +393,7 @@ var runtime = (function (exports) {
// unified ._invoke helper method.
defineIteratorMethods(Gp);

if (!(toStringTagSymbol in 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

0 comments on commit e8df307

Please sign in to comment.