From 865c57669f52dc350a9cc4b2da822ea842a74a4f Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sat, 3 May 2025 21:55:46 +0900 Subject: [PATCH 1/3] errors: add a `[[ErrorData]]` internal slot to DOMException --- lib/internal/per_context/domexception.js | 13 ++++++++----- test/parallel/test-util.js | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js index cf9042fec7c341..8dcdfb621a2b4c 100644 --- a/lib/internal/per_context/domexception.js +++ b/lib/internal/per_context/domexception.js @@ -1,11 +1,12 @@ 'use strict'; const { - ErrorCaptureStackTrace, + Error, ErrorPrototype, ObjectDefineProperties, ObjectDefineProperty, ObjectSetPrototypeOf, + ReflectConstruct, SafeMap, SafeSet, SafeWeakMap, @@ -50,17 +51,17 @@ const disusedNamesSet = new SafeSet() class DOMException { constructor(message = '', options = 'Error') { - ErrorCaptureStackTrace(this); + const error = ReflectConstruct(Error, [], new.target); if (options && typeof options === 'object') { const { name } = options; - internalsMap.set(this, { + internalsMap.set(error, { message: `${message}`, name: `${name}`, }); if ('cause' in options) { - ObjectDefineProperty(this, 'cause', { + ObjectDefineProperty(error, 'cause', { __proto__: null, value: options.cause, configurable: true, @@ -69,11 +70,13 @@ class DOMException { }); } } else { - internalsMap.set(this, { + internalsMap.set(error, { message: `${message}`, name: `${options}`, }); } + + return error; } get name() { diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js index 1ecddc829a0fdb..8515840fdf77b4 100644 --- a/test/parallel/test-util.js +++ b/test/parallel/test-util.js @@ -55,6 +55,7 @@ assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd'); assert.strictEqual(util.types.isNativeError(new Error()), true); assert.strictEqual(util.types.isNativeError(new TypeError()), true); assert.strictEqual(util.types.isNativeError(new SyntaxError()), true); + assert.strictEqual(util.types.isNativeError(new DOMException()), true); assert.strictEqual(util.types.isNativeError(new (context('Error'))()), true); assert.strictEqual( util.types.isNativeError(new (context('TypeError'))()), From 4102de98c9fdee3d686a2b757f15ca5ec9527ed9 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sun, 4 May 2025 18:33:04 +0900 Subject: [PATCH 2/3] add comments --- lib/internal/per_context/domexception.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js index 8dcdfb621a2b4c..5b12411f7fd170 100644 --- a/lib/internal/per_context/domexception.js +++ b/lib/internal/per_context/domexception.js @@ -51,6 +51,9 @@ const disusedNamesSet = new SafeSet() class DOMException { constructor(message = '', options = 'Error') { + // The DOMException class must not inherit from the Error class, + // but its instance inherit from Error.prototype. + // The stack property and [[ErrorData]] internal slot are given. const error = ReflectConstruct(Error, [], new.target); if (options && typeof options === 'object') { From d342ccdf4387b7d0d97112f9f4a84cd83cd8144e Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Mon, 5 May 2025 01:48:12 +0900 Subject: [PATCH 3/3] review --- lib/internal/per_context/domexception.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js index 5b12411f7fd170..cd2f541e828366 100644 --- a/lib/internal/per_context/domexception.js +++ b/lib/internal/per_context/domexception.js @@ -3,6 +3,7 @@ const { Error, ErrorPrototype, + FunctionPrototype, ObjectDefineProperties, ObjectDefineProperty, ObjectSetPrototypeOf, @@ -49,10 +50,15 @@ const disusedNamesSet = new SafeSet() .add('NoDataAllowedError') .add('ValidationError'); -class DOMException { +// The DOMException class must not inherit from the Error class, +// but its instance inherit from Error.prototype. +// Make the runtime aware not to allocate `this` instance unnecessarily. +class DOMException extends Error { + static { + ObjectSetPrototypeOf(DOMException, FunctionPrototype); + } + constructor(message = '', options = 'Error') { - // The DOMException class must not inherit from the Error class, - // but its instance inherit from Error.prototype. // The stack property and [[ErrorData]] internal slot are given. const error = ReflectConstruct(Error, [], new.target);