Skip to content

Commit e4b1fb5

Browse files
XadillaXjasnell
authored andcommitted
lib: expose DOMException as global
Refs: #39098 PR-URL: #39176 Fixes: #39098 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0a7f850 commit e4b1fb5

File tree

8 files changed

+49
-17
lines changed

8 files changed

+49
-17
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ module.exports = {
349349
BigInt: 'readable',
350350
BigInt64Array: 'readable',
351351
BigUint64Array: 'readable',
352+
DOMException: 'readable',
352353
Event: 'readable',
353354
EventTarget: 'readable',
354355
MessageChannel: 'readable',

doc/api/globals.md

+10
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ added: v0.0.1
380380

381381
[`setTimeout`][] is described in the [timers][] section.
382382

383+
## `DOMException`
384+
<!-- YAML
385+
added: REPLACEME
386+
-->
387+
388+
<!-- type=global -->
389+
390+
The WHATWG `DOMException` class. See [`DOMException`][] for more details.
391+
383392
## `TextDecoder`
384393
<!-- YAML
385394
added: v11.0.0
@@ -430,6 +439,7 @@ The object that acts as the namespace for all W3C
430439
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
431440

432441
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
442+
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
433443
[`EventTarget` and `Event` API]: events.md#event-target-and-event-api
434444
[`MessageChannel`]: worker_threads.md#worker_threads_class_messagechannel
435445
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent

lib/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ rules:
3737
message: "Use `const { Atomics } = globalThis;` instead of the global."
3838
- name: Buffer
3939
message: "Use `const { Buffer } = require('buffer');` instead of the global."
40+
- name: DOMException
41+
message: "Use lazy function `const { lazyDOMException } = require('internal/util');` instead of the global."
4042
- name: Event
4143
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
4244
- name: EventTarget

lib/internal/bootstrap/node.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const {
5252
globalThis,
5353
} = primordials;
5454
const config = internalBinding('config');
55-
const { deprecate } = require('internal/util');
55+
const { deprecate, lazyDOMExceptionClass } = require('internal/util');
5656

5757
setupProcessObject();
5858

@@ -201,6 +201,12 @@ if (!config.noBrowserGlobals) {
201201
exposeInterface(globalThis, 'URL', URL);
202202
// https://url.spec.whatwg.org/#urlsearchparams
203203
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
204+
exposeGetterAndSetter(globalThis,
205+
'DOMException',
206+
lazyDOMExceptionClass,
207+
(value) => {
208+
exposeInterface(globalThis, 'DOMException', value);
209+
});
204210

205211
const {
206212
TextEncoder, TextDecoder
@@ -483,6 +489,15 @@ function exposeInterface(target, name, interfaceObject) {
483489
});
484490
}
485491

492+
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
493+
ObjectDefineProperty(target, name, {
494+
enumerable: false,
495+
configurable: true,
496+
get: getter,
497+
set: setter,
498+
});
499+
}
500+
486501
// https://heycam.github.io/webidl/#define-the-operations
487502
function defineOperation(target, name, method) {
488503
ObjectDefineProperty(target, name, {

lib/internal/util.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,15 @@ function createDeferredPromise() {
442442
return { promise, resolve, reject };
443443
}
444444

445-
let DOMException;
445+
let _DOMException;
446+
const lazyDOMExceptionClass = () => {
447+
_DOMException ??= internalBinding('messaging').DOMException;
448+
return _DOMException;
449+
};
450+
446451
const lazyDOMException = hideStackFrames((message, name) => {
447-
if (DOMException === undefined)
448-
DOMException = internalBinding('messaging').DOMException;
449-
return new DOMException(message, name);
452+
_DOMException ??= internalBinding('messaging').DOMException;
453+
return new _DOMException(message, name);
450454
});
451455

452456
function structuredClone(value) {
@@ -481,6 +485,7 @@ module.exports = {
481485
isInsideNodeModules,
482486
join,
483487
lazyDOMException,
488+
lazyDOMExceptionClass,
484489
normalizeEncoding,
485490
once,
486491
promisify,
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
7+
assert.strictEqual(typeof DOMException, 'function');
8+
9+
assert.throws(() => {
10+
atob('我要抛错!');
11+
}, DOMException);

test/wpt/test-atob.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ runner.setFlags(['--expose-internals']);
1212
runner.setInitScript(`
1313
const { internalBinding } = require('internal/test/binding');
1414
const { atob, btoa } = require('buffer');
15-
const { DOMException } = internalBinding('messaging');
16-
global.DOMException = DOMException;
1715
`);
1816

1917
runner.runJsTests();

test/wpt/test-url.js

-10
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,4 @@ const { WPTRunner } = require('../common/wpt');
55

66
const runner = new WPTRunner('url');
77

8-
// Needed to access to DOMException.
9-
runner.setFlags(['--expose-internals']);
10-
11-
// DOMException is needed by urlsearchparams-constructor.any.js
12-
runner.setInitScript(`
13-
const { internalBinding } = require('internal/test/binding');
14-
const { DOMException } = internalBinding('messaging');
15-
global.DOMException = DOMException;
16-
`);
17-
188
runner.runJsTests();

0 commit comments

Comments
 (0)