Skip to content

Commit

Permalink
Refactors Error.cause into _InstallErrorCause
Browse files Browse the repository at this point in the history
  • Loading branch information
mhassan1 authored and JakeChampion committed Oct 28, 2022
1 parent 1695c17 commit 91a4700
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 61 deletions.
6 changes: 1 addition & 5 deletions polyfills/Error/cause/config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
aliases = [ "es2022" ]
dependencies = [
"_ESAbstract.CreateMethodProperty",
"_ESAbstract.CreateNonEnumerableDataPropertyOrThrow",
"_ESAbstract.Get",
"_ESAbstract.HasProperty",
"_ESAbstract.Type",
"Object.setPrototypeOf",
"_InstallErrorCause",
]
spec = "https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-installerrorcause"
docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause"
Expand Down
58 changes: 2 additions & 56 deletions polyfills/Error/cause/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@
/* global CreateMethodProperty, CreateNonEnumerableDataPropertyOrThrow, Get, HasProperty, Type */
/* global _GetErrorConstructorWithCauseInstalled, CreateMethodProperty */

(function () {
// 20.5.8.1 InstallErrorCause ( O, options )
function InstallErrorCause(O, options) {
// 1. If options is an Object and ? HasProperty(options, "cause") is true, then
if (Type(options) === 'object' && HasProperty(options, 'cause')) {
// a. Let cause be ? Get(options, "cause").
var cause = Get(options, 'cause');
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
CreateNonEnumerableDataPropertyOrThrow(O, 'cause', cause);
}
// 2. Return unused.
}

// based on https://github.com/es-shims/error-cause/blob/de17ea05/Error/implementation.js#L12-L20
function _makeErrorConstructor (name, _Error) {
var _NativeError = _nativeErrors[name];
var arity = _Error.length;
return function () {
var O = arity === 2
? _NativeError.call(null, arguments[0], arguments[1])
: _NativeError.call(null, arguments[0]);
InstallErrorCause(O, arguments.length > arity && arguments[arity]);
CreateMethodProperty(O, 'constructor', _Error);
return O;
}
}

// based on https://github.com/es-shims/error-cause/blob/de17ea05/Error/implementation.js#L22-L29
function _inheritErrorPrototype (name, _Error) {
Object.setPrototypeOf(_Error, self.Error);
_Error.prototype = _nativeErrors[name].prototype;
Object.defineProperty(_Error, 'prototype', { writable: false });
return _Error;
}

var _errorNames = [
'Error',
'EvalError',
Expand All @@ -45,32 +11,12 @@
'URIError'
];

var _newErrors = {
Error: function Error (_message) { return _errorConstructors.Error.apply(null, arguments); },
EvalError: function EvalError (_message) { return _errorConstructors.EvalError.apply(null, arguments); },
RangeError: function RangeError (_message) { return _errorConstructors.RangeError.apply(null, arguments); },
ReferenceError: function ReferenceError (_message) { return _errorConstructors.ReferenceError.apply(null, arguments); },
SyntaxError: function SyntaxError (_message) { return _errorConstructors.SyntaxError.apply(null, arguments); },
TypeError: function TypeError (_message) { return _errorConstructors.TypeError.apply(null, arguments); },
URIError: function URIError (_message) { return _errorConstructors.URIError.apply(null, arguments); }
};

if ('AggregateError' in self) {
_errorNames.push('AggregateError');
_newErrors.AggregateError = function AggregateError (_errors, _message) { return _errorConstructors.AggregateError.apply(null, arguments); };
}

var _nativeErrors = {};
var _errorConstructors = {};

for (var i = 0; i < _errorNames.length; i++) {
var name = _errorNames[i];
_nativeErrors[name] = self[name];
_errorConstructors[name] = _makeErrorConstructor(name, _newErrors[name]);
if (name === 'Error') {
CreateMethodProperty(self, name, _newErrors[name]);
} else {
CreateMethodProperty(self, name, _inheritErrorPrototype(name, _newErrors[name]));
}
CreateMethodProperty(self, name, _GetErrorConstructorWithCauseInstalled(name));
}
})();
27 changes: 27 additions & 0 deletions polyfills/_InstallErrorCause/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
dependencies = [
"_ESAbstract.CreateMethodProperty",
"_ESAbstract.CreateNonEnumerableDataPropertyOrThrow",
"_ESAbstract.Get",
"_ESAbstract.HasProperty",
"_ESAbstract.Type",
"Object.setPrototypeOf",
]
spec = "https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-installerrorcause"
docs = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause"

[browsers]
android = "*"
bb = "*"
chrome = "*"
edge = "*"
edge_mob = "*"
firefox = "*"
firefox_mob = "*"
ie = "*"
ie_mob = "*"
opera = "*"
op_mob = "*"
op_mini = "*"
safari = "*"
ios_saf = "*"
samsung_mob = "*"
63 changes: 63 additions & 0 deletions polyfills/_InstallErrorCause/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* global CreateMethodProperty, CreateNonEnumerableDataPropertyOrThrow, Get, HasProperty, Type */

// eslint-disable-next-line no-unused-vars
var _GetErrorConstructorWithCauseInstalled;

(function () {
// 20.5.8.1 InstallErrorCause ( O, options )
function InstallErrorCause(O, options) {
// 1. If options is an Object and ? HasProperty(options, "cause") is true, then
if (Type(options) === 'object' && HasProperty(options, 'cause')) {
// a. Let cause be ? Get(options, "cause").
var cause = Get(options, 'cause');
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
CreateNonEnumerableDataPropertyOrThrow(O, 'cause', cause);
}
// 2. Return unused.
}

// based on https://github.com/es-shims/error-cause/blob/de17ea05/Error/implementation.js#L12-L20
function _makeErrorConstructor (name, _Error) {
var _NativeError = _nativeErrors[name];
var arity = _Error.length;
return function () {
var O = arity === 2
? _NativeError.call(null, arguments[0], arguments[1])
: _NativeError.call(null, arguments[0]);
InstallErrorCause(O, arguments.length > arity && arguments[arity]);
CreateMethodProperty(O, 'constructor', _Error);
return O;
}
}

// based on https://github.com/es-shims/error-cause/blob/de17ea05/Error/implementation.js#L22-L29
function _inheritErrorPrototype (name, _Error) {
Object.setPrototypeOf(_Error, self.Error);
_Error.prototype = _nativeErrors[name].prototype;
Object.defineProperty(_Error, 'prototype', { writable: false });
return _Error;
}

var _newErrors = {
Error: function Error (_message) { return _errorConstructors.Error.apply(null, arguments); },
EvalError: function EvalError (_message) { return _errorConstructors.EvalError.apply(null, arguments); },
RangeError: function RangeError (_message) { return _errorConstructors.RangeError.apply(null, arguments); },
ReferenceError: function ReferenceError (_message) { return _errorConstructors.ReferenceError.apply(null, arguments); },
SyntaxError: function SyntaxError (_message) { return _errorConstructors.SyntaxError.apply(null, arguments); },
TypeError: function TypeError (_message) { return _errorConstructors.TypeError.apply(null, arguments); },
URIError: function URIError (_message) { return _errorConstructors.URIError.apply(null, arguments); },
AggregateError: function AggregateError (_errors, _message) { return _errorConstructors.AggregateError.apply(null, arguments); }
};

var _nativeErrors = {};
var _errorConstructors = {};

_GetErrorConstructorWithCauseInstalled = function (name) {
_nativeErrors[name] = self[name];
_errorConstructors[name] = _makeErrorConstructor(name, _newErrors[name]);
if (name !== 'Error') {
_inheritErrorPrototype(name, _newErrors[name]);
}
return _newErrors[name];
}
})();

0 comments on commit 91a4700

Please sign in to comment.