Skip to content

Commit

Permalink
lib: make DOMException in globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Jun 28, 2021
1 parent 2de139b commit b3e19fb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ module.exports = {
BigInt: 'readable',
BigInt64Array: 'readable',
BigUint64Array: 'readable',
DOMException: 'readable',
Event: 'readable',
EventTarget: 'readable',
MessageChannel: 'readable',
Expand Down
9 changes: 9 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

## `TextDecoder`
<!-- YAML
added: v11.0.0
Expand Down
17 changes: 16 additions & 1 deletion lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const {
globalThis,
} = primordials;
const config = internalBinding('config');
const { deprecate } = require('internal/util');
const { deprecate, lazyDOMExceptionClass } = require('internal/util');

setupProcessObject();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -484,6 +490,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, {
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ function createDeferredPromise() {
}

let DOMException;
const lazyDOMExceptionClass = () => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return DOMException;
};

const lazyDOMException = hideStackFrames((message, name) => {
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
Expand All @@ -466,6 +472,7 @@ module.exports = {
isInsideNodeModules,
join,
lazyDOMException,
lazyDOMExceptionClass,
normalizeEncoding,
once,
promisify,
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-global-domexception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

require('../common');

const assert = require('assert');

assert.strictEqual(typeof DOMException, 'function');

assert.throws(() => {
atob('我要抛错!');
}, DOMException);
2 changes: 0 additions & 2 deletions test/wpt/test-atob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
10 changes: 0 additions & 10 deletions test/wpt/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit b3e19fb

Please sign in to comment.