-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add kNoBrowserGlobals flag for Environment
- Loading branch information
Showing
8 changed files
with
177 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperty, | ||
globalThis, | ||
} = primordials; | ||
|
||
const { lazyDOMExceptionClass } = require('internal/util'); | ||
const config = internalBinding('config'); | ||
|
||
// Override global console from the one provided by the VM | ||
// to the one implemented by Node.js | ||
// https://console.spec.whatwg.org/#console-namespace | ||
exposeNamespace(globalThis, 'console', | ||
createGlobalConsole(globalThis.console)); | ||
|
||
const { URL, URLSearchParams } = require('internal/url'); | ||
// https://url.spec.whatwg.org/#url | ||
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 | ||
} = require('internal/encoding'); | ||
// https://encoding.spec.whatwg.org/#textencoder | ||
exposeInterface(globalThis, 'TextEncoder', TextEncoder); | ||
// https://encoding.spec.whatwg.org/#textdecoder | ||
exposeInterface(globalThis, 'TextDecoder', TextDecoder); | ||
|
||
const { | ||
AbortController, | ||
AbortSignal, | ||
} = require('internal/abort_controller'); | ||
exposeInterface(globalThis, 'AbortController', AbortController); | ||
exposeInterface(globalThis, 'AbortSignal', AbortSignal); | ||
|
||
const { | ||
EventTarget, | ||
Event, | ||
} = require('internal/event_target'); | ||
exposeInterface(globalThis, 'EventTarget', EventTarget); | ||
exposeInterface(globalThis, 'Event', Event); | ||
const { | ||
MessageChannel, | ||
MessagePort, | ||
MessageEvent, | ||
} = require('internal/worker/io'); | ||
exposeInterface(globalThis, 'MessageChannel', MessageChannel); | ||
exposeInterface(globalThis, 'MessagePort', MessagePort); | ||
exposeInterface(globalThis, 'MessageEvent', MessageEvent); | ||
|
||
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope | ||
const timers = require('timers'); | ||
defineOperation(globalThis, 'clearInterval', timers.clearInterval); | ||
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout); | ||
defineOperation(globalThis, 'setInterval', timers.setInterval); | ||
defineOperation(globalThis, 'setTimeout', timers.setTimeout); | ||
|
||
const { queueMicrotask } = require('internal/process/task_queues'); | ||
defineOperation(globalThis, 'queueMicrotask', queueMicrotask); | ||
|
||
// https://www.w3.org/TR/hr-time-2/#the-performance-attribute | ||
defineReplacableAttribute(globalThis, 'performance', | ||
require('perf_hooks').performance); | ||
|
||
// Non-standard extensions: | ||
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate); | ||
defineOperation(globalThis, 'setImmediate', timers.setImmediate); | ||
|
||
const { | ||
structuredClone, | ||
} = require('internal/structured_clone'); | ||
defineOperation(globalThis, 'structuredClone', structuredClone); | ||
|
||
function createGlobalConsole(consoleFromVM) { | ||
const consoleFromNode = | ||
require('internal/console/global'); | ||
if (config.hasInspector) { | ||
const inspector = require('internal/util/inspector'); | ||
// This will be exposed by `require('inspector').console` later. | ||
inspector.consoleFromVM = consoleFromVM; | ||
// TODO(joyeecheung): postpone this until the first time inspector | ||
// is activated. | ||
inspector.wrapConsole(consoleFromNode, consoleFromVM); | ||
const { setConsoleExtensionInstaller } = internalBinding('inspector'); | ||
// Setup inspector command line API. | ||
setConsoleExtensionInstaller(inspector.installConsoleExtensions); | ||
} | ||
return consoleFromNode; | ||
} | ||
|
||
// https://heycam.github.io/webidl/#es-namespaces | ||
function exposeNamespace(target, name, namespaceObject) { | ||
ObjectDefineProperty(target, name, { | ||
writable: true, | ||
enumerable: false, | ||
configurable: true, | ||
value: namespaceObject | ||
}); | ||
} | ||
|
||
// https://heycam.github.io/webidl/#es-interfaces | ||
function exposeInterface(target, name, interfaceObject) { | ||
ObjectDefineProperty(target, name, { | ||
writable: true, | ||
enumerable: false, | ||
configurable: true, | ||
value: 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, { | ||
writable: true, | ||
enumerable: true, | ||
configurable: true, | ||
value: method | ||
}); | ||
} | ||
|
||
// https://heycam.github.io/webidl/#Replaceable | ||
function defineReplacableAttribute(target, name, value) { | ||
ObjectDefineProperty(target, name, { | ||
writable: true, | ||
enumerable: true, | ||
configurable: true, | ||
value, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters