Skip to content

Commit 081c708

Browse files
legendecaspetamoriken
authored andcommitted
lib: make domexception a native error
Co-Authored-By: Kenta Moriuchi <[email protected]> PR-URL: #58691 Fixes: #56497 Refs: v8/v8@e3df60f Refs: #58138 Reviewed-By: Jason Zhang <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Ethan Arrowood <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4b61f10 commit 081c708

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

lib/internal/per_context/domexception.js

Lines 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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
{
2-
"DOMException-is-error.any.js": {
3-
"fail": {
4-
"note": "https://github.com/nodejs/node/issues/56497",
5-
"expected": [
6-
"DOMException-is-error"
7-
]
8-
}
9-
}
10-
}
1+
{}

0 commit comments

Comments
 (0)