Skip to content

Commit b5bf1ac

Browse files
committed
src: bootstrap Web [Exposed=*] APIs in the shadow realm
This is the initial work to bootstrap Web interfaces that are defined with extended attributes `[Exposed=*]`. The ShadowRealm instances are garbage-collected once it is unreachable. However, V8 can not infer the reference cycles between the per-realm strong persistent function handles and the realm's context handle. To allow the context to be gc-ed once it is not reachable, the per-realm persistent handles are attached to the context's global object and the persistent handles are set as weak.
1 parent c2c61e0 commit b5bf1ac

26 files changed

+669
-260
lines changed

lib/internal/bootstrap/browser.js

Lines changed: 0 additions & 145 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict';
2+
3+
/**
4+
* This file exposes web interfaces that is defined with the WebIDL
5+
* [Exposed=*] extended attribute.
6+
* See more details at https://webidl.spec.whatwg.org/#Exposed.
7+
*/
8+
9+
const {
10+
globalThis,
11+
} = primordials;
12+
13+
const {
14+
exposeInterface,
15+
lazyDOMExceptionClass,
16+
exposeLazyInterfaces,
17+
exposeGetterAndSetter,
18+
} = require('internal/util');
19+
20+
const { URL, URLSearchParams } = require('internal/url');
21+
// https://url.spec.whatwg.org/#url
22+
exposeInterface(globalThis, 'URL', URL);
23+
// https://url.spec.whatwg.org/#urlsearchparams
24+
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
25+
exposeGetterAndSetter(globalThis,
26+
'DOMException',
27+
lazyDOMExceptionClass,
28+
(value) => {
29+
exposeInterface(globalThis, 'DOMException', value);
30+
});
31+
32+
// https://dom.spec.whatwg.org/#interface-abortcontroller
33+
// Lazy ones.
34+
exposeLazyInterfaces(globalThis, 'internal/abort_controller', [
35+
'AbortController', 'AbortSignal',
36+
]);
37+
// https://dom.spec.whatwg.org/#interface-eventtarget
38+
const {
39+
EventTarget, Event,
40+
} = require('internal/event_target');
41+
exposeInterface(globalThis, 'Event', Event);
42+
exposeInterface(globalThis, 'EventTarget', EventTarget);
43+
44+
// https://encoding.spec.whatwg.org/#textencoder
45+
// https://encoding.spec.whatwg.org/#textdecoder
46+
exposeLazyInterfaces(globalThis,
47+
'internal/encoding',
48+
['TextEncoder', 'TextDecoder']);
49+
50+
// Web Streams API
51+
exposeLazyInterfaces(
52+
globalThis,
53+
'internal/webstreams/transformstream',
54+
['TransformStream', 'TransformStreamDefaultController']);
55+
56+
exposeLazyInterfaces(
57+
globalThis,
58+
'internal/webstreams/writablestream',
59+
['WritableStream', 'WritableStreamDefaultController', 'WritableStreamDefaultWriter']);
60+
61+
exposeLazyInterfaces(
62+
globalThis,
63+
'internal/webstreams/readablestream',
64+
[
65+
'ReadableStream', 'ReadableStreamDefaultReader',
66+
'ReadableStreamBYOBReader', 'ReadableStreamBYOBRequest',
67+
'ReadableByteStreamController', 'ReadableStreamDefaultController',
68+
]);
69+
70+
exposeLazyInterfaces(
71+
globalThis,
72+
'internal/webstreams/queuingstrategies',
73+
[
74+
'ByteLengthQueuingStrategy', 'CountQueuingStrategy',
75+
]);
76+
77+
exposeLazyInterfaces(
78+
globalThis,
79+
'internal/webstreams/encoding',
80+
[
81+
'TextEncoderStream', 'TextDecoderStream',
82+
]);
83+
84+
exposeLazyInterfaces(
85+
globalThis,
86+
'internal/webstreams/compression',
87+
[
88+
'CompressionStream', 'DecompressionStream',
89+
]);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const {
4+
globalThis,
5+
} = primordials;
6+
7+
const {
8+
defineOperation,
9+
defineLazyProperties,
10+
defineReplaceableLazyAttribute,
11+
exposeLazyInterfaces,
12+
exposeNamespace,
13+
} = require('internal/util');
14+
const config = internalBinding('config');
15+
16+
// https://console.spec.whatwg.org/#console-namespace
17+
exposeNamespace(globalThis, 'console',
18+
createGlobalConsole());
19+
20+
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
21+
const timers = require('timers');
22+
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
23+
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
24+
defineOperation(globalThis, 'setInterval', timers.setInterval);
25+
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
26+
27+
exposeLazyInterfaces(globalThis, 'internal/worker/io', [
28+
'MessageChannel', 'MessagePort', 'MessageEvent',
29+
]);
30+
defineLazyProperties(globalThis, 'buffer', ['atob', 'btoa']);
31+
// https://www.w3.org/TR/FileAPI/#dfn-Blob
32+
exposeLazyInterfaces(globalThis, 'internal/blob', ['Blob']);
33+
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute
34+
exposeLazyInterfaces(globalThis, 'perf_hooks', [
35+
'Performance', 'PerformanceEntry', 'PerformanceMark', 'PerformanceMeasure',
36+
'PerformanceObserver', 'PerformanceObserverEntryList', 'PerformanceResourceTiming',
37+
]);
38+
39+
defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
40+
41+
const { installObjectURLMethods } = require('internal/url');
42+
installObjectURLMethods();
43+
44+
function createGlobalConsole() {
45+
const consoleFromNode =
46+
require('internal/console/global');
47+
if (config.hasInspector) {
48+
const inspector = require('internal/util/inspector');
49+
// TODO(joyeecheung): postpone this until the first time inspector
50+
// is activated.
51+
inspector.wrapConsole(consoleFromNode);
52+
const { setConsoleExtensionInstaller } = internalBinding('inspector');
53+
// Setup inspector command line API.
54+
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
55+
}
56+
return consoleFromNode;
57+
}

lib/internal/process/pre_execution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function setupFetch() {
262262
});
263263
}
264264

265-
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
265+
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
266266
// removed.
267267
function setupWebCrypto() {
268268
if (process.config.variables.node_no_browser_globals ||
@@ -310,7 +310,7 @@ function setupCodeCoverage() {
310310
}
311311
}
312312

313-
// TODO(daeyeon): move this to internal/bootstrap/browser when the CLI flag is
313+
// TODO(daeyeon): move this to internal/bootstrap/web/* when the CLI flag is
314314
// removed.
315315
function setupCustomEvent() {
316316
if (process.config.variables.node_no_browser_globals ||

0 commit comments

Comments
 (0)