Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit edb9248

Browse filesBrowse files
legendecaspetamoriken
authored andcommitted
lib: make domexception a native error
Co-Authored-By: Kenta Moriuchi <moriken@kimamass.com> PR-URL: #58691 Backport-PR-URL: #59957 Fixes: #56497 Refs: v8/v8@e3df60f Refs: #58138 Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d289b1d commit edb9248
Copy full SHA for edb9248

File tree

Expand file treeCollapse file tree

1 file changed

+26
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+26
-9
lines changed
Open diff view settings
Collapse file

‎lib/internal/per_context/domexception.js‎

Copy file name to clipboardExpand all lines: lib/internal/per_context/domexception.js
+26-9Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const {
4-
ErrorCaptureStackTrace,
4+
Error,
55
ErrorPrototype,
66
ObjectDefineProperties,
77
ObjectDefineProperty,
@@ -60,20 +60,33 @@ const disusedNamesSet = new SafeSet()
6060
.add('NoDataAllowedError')
6161
.add('ValidationError');
6262

63+
let DOMExceptionPrototype;
64+
// The DOMException WebIDL interface defines that:
65+
// - ObjectGetPrototypeOf(DOMException) === Function.
66+
// - ObjectGetPrototypeOf(DOMException.prototype) === Error.prototype.
67+
// Thus, we can not simply use the pattern of `class DOMException extends Error` and call
68+
// `super()` to construct an object. The `super` in `super()` call in the constructor will
69+
// be resolved to `Function`, instead of `Error`. Use the trick of return overriding to
70+
// create an object with the `[[ErrorData]]` internal slot.
71+
// Ref: https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-getsuperconstructor
6372
class DOMException {
6473
constructor(message = '', options = 'Error') {
65-
this[transfer_mode_private_symbol] = kCloneable;
66-
ErrorCaptureStackTrace(this);
74+
// Invokes the Error constructor to create an object with the [[ErrorData]]
75+
// internal slot.
76+
// eslint-disable-next-line no-restricted-syntax
77+
const self = new Error();
78+
ObjectSetPrototypeOf(self, DOMExceptionPrototype);
79+
self[transfer_mode_private_symbol] = kCloneable;
6780

6881
if (options && typeof options === 'object') {
6982
const { name } = options;
70-
internalsMap.set(this, {
83+
internalsMap.set(self, {
7184
message: `${message}`,
7285
name: `${name}`,
7386
});
7487

7588
if ('cause' in options) {
76-
ObjectDefineProperty(this, 'cause', {
89+
ObjectDefineProperty(self, 'cause', {
7790
__proto__: null,
7891
value: options.cause,
7992
configurable: true,
@@ -82,11 +95,14 @@ class DOMException {
8295
});
8396
}
8497
} else {
85-
internalsMap.set(this, {
98+
internalsMap.set(self, {
8699
message: `${message}`,
87100
name: `${options}`,
88101
});
89102
}
103+
// Return the error object as the return overriding of the constructor.
104+
// eslint-disable-next-line no-constructor-return
105+
return self;
90106
}
91107

92108
[messaging_clone_symbol]() {
@@ -142,8 +158,9 @@ class DOMException {
142158
}
143159
}
144160

145-
ObjectSetPrototypeOf(DOMException.prototype, ErrorPrototype);
146-
ObjectDefineProperties(DOMException.prototype, {
161+
DOMExceptionPrototype = DOMException.prototype;
162+
ObjectSetPrototypeOf(DOMExceptionPrototype, ErrorPrototype);
163+
ObjectDefineProperties(DOMExceptionPrototype, {
147164
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'DOMException' },
148165
name: { __proto__: null, enumerable: true, configurable: true },
149166
message: { __proto__: null, enumerable: true, configurable: true },
@@ -181,7 +198,7 @@ for (const { 0: name, 1: codeName, 2: value } of [
181198
]) {
182199
const desc = { enumerable: true, value };
183200
ObjectDefineProperty(DOMException, codeName, desc);
184-
ObjectDefineProperty(DOMException.prototype, codeName, desc);
201+
ObjectDefineProperty(DOMExceptionPrototype, codeName, desc);
185202
nameToCodeMap.set(name, value);
186203
}
187204

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.