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

Commit

Permalink
Make sure define(obj, key, value) always works, even in IE8.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jul 22, 2020
1 parent 980ca64 commit d4868b6
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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 @@ -17,13 +16,22 @@ 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 define(obj, key, value) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
return obj[key];
}
try {
// IE 8 has a broken Object.defineProperty that only works on DOM objects.
define({}, "");
} catch (err) {
define = function(obj, key, value) {
return obj[key] = value;
};
}

function wrap(innerFn, outerFn, self, tryLocsList) {
Expand Down Expand Up @@ -92,35 +100,23 @@ var runtime = (function (exports) {
IteratorPrototype = NativeIteratorPrototype;
}

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

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

// Helper for defining the .next, .throw, and .return methods of the
// Iterator interface in terms of a single ._invoke method.
function defineIteratorMethods(prototype) {
["next", "throw", "return"].forEach(function(method) {
prototype[method] = function(arg) {
define(prototype, method, function(arg) {
return this._invoke(method, arg);
};
});
});
}

Expand All @@ -139,7 +135,7 @@ var runtime = (function (exports) {
Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
} else {
genFun.__proto__ = GeneratorFunctionPrototype;
ensureDefaultToStringTag(genFun, "GeneratorFunction");
define(genFun, toStringTagSymbol, "GeneratorFunction");
}
genFun.prototype = Object.create(Gp);
return genFun;
Expand Down Expand Up @@ -409,7 +405,7 @@ var runtime = (function (exports) {
// unified ._invoke helper method.
defineIteratorMethods(Gp);

ensureDefaultToStringTag(Gp, "Generator");
define(Gp, toStringTagSymbol, "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 d4868b6

Please sign in to comment.