diff --git a/lib/internal/per_context/domexception.js b/lib/internal/per_context/domexception.js index cf9042fec7c341..cd2f541e828366 100644 --- a/lib/internal/per_context/domexception.js +++ b/lib/internal/per_context/domexception.js @@ -1,11 +1,13 @@ 'use strict'; const { - ErrorCaptureStackTrace, + Error, ErrorPrototype, + FunctionPrototype, ObjectDefineProperties, ObjectDefineProperty, ObjectSetPrototypeOf, + ReflectConstruct, SafeMap, SafeSet, SafeWeakMap, @@ -48,19 +50,27 @@ 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') { - ErrorCaptureStackTrace(this); + // The stack property and [[ErrorData]] internal slot are given. + 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 +79,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'))()),