lib: expose DOMException as global

Refs: https://github.com/nodejs/node/issues/39098

PR-URL: https://github.com/nodejs/node/pull/39176
Fixes: https://github.com/nodejs/node/issues/39098
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
XadillaX 2021-06-28 16:29:54 +08:00 committed by James M Snell
parent 0a7f850123
commit e4b1fb5e64
No known key found for this signature in database
GPG Key ID: 7341B15C070877AC
8 changed files with 49 additions and 17 deletions

View File

@ -349,6 +349,7 @@ module.exports = {
BigInt: 'readable',
BigInt64Array: 'readable',
BigUint64Array: 'readable',
DOMException: 'readable',
Event: 'readable',
EventTarget: 'readable',
MessageChannel: 'readable',

View File

@ -380,6 +380,15 @@ added: v0.0.1
[`setTimeout`][] is described in the [timers][] section.
## `DOMException`
<!-- YAML
added: REPLACEME
-->
<!-- type=global -->
The WHATWG `DOMException` class. See [`DOMException`][] for more details.
## `TextDecoder`
<!-- YAML
added: v11.0.0
@ -430,6 +439,7 @@ The object that acts as the namespace for all W3C
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
[`EventTarget` and `Event` API]: events.md#event-target-and-event-api
[`MessageChannel`]: worker_threads.md#worker_threads_class_messagechannel
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent

View File

@ -37,6 +37,8 @@ rules:
message: "Use `const { Atomics } = globalThis;` instead of the global."
- name: Buffer
message: "Use `const { Buffer } = require('buffer');` instead of the global."
- name: DOMException
message: "Use lazy function `const { lazyDOMException } = require('internal/util');` instead of the global."
- name: Event
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
- name: EventTarget

View File

@ -52,7 +52,7 @@ const {
globalThis,
} = primordials;
const config = internalBinding('config');
const { deprecate } = require('internal/util');
const { deprecate, lazyDOMExceptionClass } = require('internal/util');
setupProcessObject();
@ -201,6 +201,12 @@ if (!config.noBrowserGlobals) {
exposeInterface(globalThis, 'URL', URL);
// https://url.spec.whatwg.org/#urlsearchparams
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
exposeGetterAndSetter(globalThis,
'DOMException',
lazyDOMExceptionClass,
(value) => {
exposeInterface(globalThis, 'DOMException', value);
});
const {
TextEncoder, TextDecoder
@ -483,6 +489,15 @@ function exposeInterface(target, name, interfaceObject) {
});
}
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
ObjectDefineProperty(target, name, {
enumerable: false,
configurable: true,
get: getter,
set: setter,
});
}
// https://heycam.github.io/webidl/#define-the-operations
function defineOperation(target, name, method) {
ObjectDefineProperty(target, name, {

View File

@ -442,11 +442,15 @@ function createDeferredPromise() {
return { promise, resolve, reject };
}
let DOMException;
let _DOMException;
const lazyDOMExceptionClass = () => {
_DOMException ??= internalBinding('messaging').DOMException;
return _DOMException;
};
const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
_DOMException ??= internalBinding('messaging').DOMException;
return new _DOMException(message, name);
});
function structuredClone(value) {
@ -481,6 +485,7 @@ module.exports = {
isInsideNodeModules,
join,
lazyDOMException,
lazyDOMExceptionClass,
normalizeEncoding,
once,
promisify,

View File

@ -0,0 +1,11 @@
'use strict';
require('../common');
const assert = require('assert');
assert.strictEqual(typeof DOMException, 'function');
assert.throws(() => {
atob('我要抛错!');
}, DOMException);

View File

@ -12,8 +12,6 @@ runner.setFlags(['--expose-internals']);
runner.setInitScript(`
const { internalBinding } = require('internal/test/binding');
const { atob, btoa } = require('buffer');
const { DOMException } = internalBinding('messaging');
global.DOMException = DOMException;
`);
runner.runJsTests();

View File

@ -5,14 +5,4 @@ const { WPTRunner } = require('../common/wpt');
const runner = new WPTRunner('url');
// Needed to access to DOMException.
runner.setFlags(['--expose-internals']);
// DOMException is needed by urlsearchparams-constructor.any.js
runner.setInitScript(`
const { internalBinding } = require('internal/test/binding');
const { DOMException } = internalBinding('messaging');
global.DOMException = DOMException;
`);
runner.runJsTests();