process: use common operations to define browser globals
Extracts: - `exposeNamespace`: https://heycam.github.io/webidl/#es-namespaces - `exposeInterface`: https://heycam.github.io/webidl/#es-interfaces - `defineOperation`: https://heycam.github.io/webidl/#define-the-operations into functions to define browser globals. PR-URL: https://github.com/nodejs/node/pull/26230 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
79a3348d14
commit
bba27a11c5
@ -182,11 +182,34 @@ if (config.hasInspector) {
|
|||||||
|
|
||||||
const browserGlobals = !process._noBrowserGlobals;
|
const browserGlobals = !process._noBrowserGlobals;
|
||||||
if (browserGlobals) {
|
if (browserGlobals) {
|
||||||
setupGlobalTimeouts();
|
// Override global console from the one provided by the VM
|
||||||
setupGlobalConsole();
|
// to the one implemented by Node.js
|
||||||
setupGlobalURL();
|
// https://console.spec.whatwg.org/#console-namespace
|
||||||
setupGlobalEncoding();
|
exposeNamespace(global, 'console', createGlobalConsole(global.console));
|
||||||
|
|
||||||
|
const { URL, URLSearchParams } = NativeModule.require('internal/url');
|
||||||
|
// https://url.spec.whatwg.org/#url
|
||||||
|
exposeInterface(global, 'URL', URL);
|
||||||
|
// https://url.spec.whatwg.org/#urlsearchparams
|
||||||
|
exposeInterface(global, 'URLSearchParams', URLSearchParams);
|
||||||
|
|
||||||
|
const { TextEncoder, TextDecoder } = NativeModule.require('util');
|
||||||
|
// https://encoding.spec.whatwg.org/#textencoder
|
||||||
|
exposeInterface(global, 'TextEncoder', TextEncoder);
|
||||||
|
// https://encoding.spec.whatwg.org/#textdecoder
|
||||||
|
exposeInterface(global, 'TextDecoder', TextDecoder);
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
|
||||||
|
const timers = NativeModule.require('timers');
|
||||||
|
defineOperation(global, 'clearInterval', timers.clearInterval);
|
||||||
|
defineOperation(global, 'clearTimeout', timers.clearTimeout);
|
||||||
|
defineOperation(global, 'setInterval', timers.setInterval);
|
||||||
|
defineOperation(global, 'setTimeout', timers.setTimeout);
|
||||||
setupQueueMicrotask();
|
setupQueueMicrotask();
|
||||||
|
|
||||||
|
// Non-standard extensions:
|
||||||
|
defineOperation(global, 'clearImmediate', timers.clearImmediate);
|
||||||
|
defineOperation(global, 'setImmediate', timers.setImmediate);
|
||||||
}
|
}
|
||||||
|
|
||||||
setupDOMException();
|
setupDOMException();
|
||||||
@ -376,29 +399,9 @@ function setupBuffer() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupGlobalTimeouts() {
|
function createGlobalConsole(consoleFromVM) {
|
||||||
const timers = NativeModule.require('timers');
|
|
||||||
global.clearImmediate = timers.clearImmediate;
|
|
||||||
global.clearInterval = timers.clearInterval;
|
|
||||||
global.clearTimeout = timers.clearTimeout;
|
|
||||||
global.setImmediate = timers.setImmediate;
|
|
||||||
global.setInterval = timers.setInterval;
|
|
||||||
global.setTimeout = timers.setTimeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupGlobalConsole() {
|
|
||||||
const consoleFromVM = global.console;
|
|
||||||
const consoleFromNode =
|
const consoleFromNode =
|
||||||
NativeModule.require('internal/console/global');
|
NativeModule.require('internal/console/global');
|
||||||
// Override global console from the one provided by the VM
|
|
||||||
// to the one implemented by Node.js
|
|
||||||
Object.defineProperty(global, 'console', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: false,
|
|
||||||
value: consoleFromNode,
|
|
||||||
writable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
if (config.hasInspector) {
|
if (config.hasInspector) {
|
||||||
const inspector = NativeModule.require('internal/util/inspector');
|
const inspector = NativeModule.require('internal/util/inspector');
|
||||||
// This will be exposed by `require('inspector').console` later.
|
// This will be exposed by `require('inspector').console` later.
|
||||||
@ -410,42 +413,7 @@ function setupGlobalConsole() {
|
|||||||
// Setup inspector command line API.
|
// Setup inspector command line API.
|
||||||
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
|
setConsoleExtensionInstaller(inspector.installConsoleExtensions);
|
||||||
}
|
}
|
||||||
}
|
return consoleFromNode;
|
||||||
|
|
||||||
function setupGlobalURL() {
|
|
||||||
const { URL, URLSearchParams } = NativeModule.require('internal/url');
|
|
||||||
Object.defineProperties(global, {
|
|
||||||
URL: {
|
|
||||||
value: URL,
|
|
||||||
writable: true,
|
|
||||||
configurable: true,
|
|
||||||
enumerable: false
|
|
||||||
},
|
|
||||||
URLSearchParams: {
|
|
||||||
value: URLSearchParams,
|
|
||||||
writable: true,
|
|
||||||
configurable: true,
|
|
||||||
enumerable: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupGlobalEncoding() {
|
|
||||||
const { TextEncoder, TextDecoder } = NativeModule.require('util');
|
|
||||||
Object.defineProperties(global, {
|
|
||||||
TextEncoder: {
|
|
||||||
value: TextEncoder,
|
|
||||||
writable: true,
|
|
||||||
configurable: true,
|
|
||||||
enumerable: false
|
|
||||||
},
|
|
||||||
TextDecoder: {
|
|
||||||
value: TextDecoder,
|
|
||||||
writable: true,
|
|
||||||
configurable: true,
|
|
||||||
enumerable: false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupQueueMicrotask() {
|
function setupQueueMicrotask() {
|
||||||
@ -483,3 +451,33 @@ function setupDOMException() {
|
|||||||
const { registerDOMException } = internalBinding('messaging');
|
const { registerDOMException } = internalBinding('messaging');
|
||||||
registerDOMException(DOMException);
|
registerDOMException(DOMException);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://heycam.github.io/webidl/#es-namespaces
|
||||||
|
function exposeNamespace(target, name, namespaceObject) {
|
||||||
|
Object.defineProperty(target, name, {
|
||||||
|
writable: true,
|
||||||
|
enumerable: false,
|
||||||
|
configurable: true,
|
||||||
|
value: namespaceObject
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://heycam.github.io/webidl/#es-interfaces
|
||||||
|
function exposeInterface(target, name, interfaceObject) {
|
||||||
|
Object.defineProperty(target, name, {
|
||||||
|
writable: true,
|
||||||
|
enumerable: false,
|
||||||
|
configurable: true,
|
||||||
|
value: interfaceObject
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://heycam.github.io/webidl/#define-the-operations
|
||||||
|
function defineOperation(target, name, method) {
|
||||||
|
Object.defineProperty(target, name, {
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true,
|
||||||
|
value: method
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user