Skip to content

Commit

Permalink
lib: enforce using primordials.globalThis instead of global
Browse files Browse the repository at this point in the history
PR-URL: #38230
Backport-PR-URL: #39448
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
aduh95 authored and richardlau committed Jul 20, 2021
1 parent ea9003a commit d4f96bb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ rules:
- error
- name: globalThis
message: "Use `const { globalThis } = primordials;` instead of the global."
- name: global
message: "Use `const { globalThis } = primordials;` instead of `global`."
# Custom rules in tools/eslint-rules
node-core/lowercase-name-for-primitive: error
node-core/non-ascii-character: error
Expand Down
32 changes: 17 additions & 15 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
SymbolToStringTag,
globalThis,
} = primordials;
const config = internalBinding('config');
const { deprecate } = require('internal/util');
Expand Down Expand Up @@ -119,34 +120,35 @@ if (!config.noBrowserGlobals) {
// 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(global, 'console', createGlobalConsole(global.console));
exposeNamespace(globalThis, 'console',
createGlobalConsole(globalThis.console));

const { URL, URLSearchParams } = require('internal/url');
// https://url.spec.whatwg.org/#url
exposeInterface(global, 'URL', URL);
exposeInterface(globalThis, 'URL', URL);
// https://url.spec.whatwg.org/#urlsearchparams
exposeInterface(global, 'URLSearchParams', URLSearchParams);
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);

const {
TextEncoder, TextDecoder
} = require('internal/encoding');
// https://encoding.spec.whatwg.org/#textencoder
exposeInterface(global, 'TextEncoder', TextEncoder);
exposeInterface(globalThis, 'TextEncoder', TextEncoder);
// https://encoding.spec.whatwg.org/#textdecoder
exposeInterface(global, 'TextDecoder', TextDecoder);
exposeInterface(globalThis, 'TextDecoder', TextDecoder);

// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
const timers = require('timers');
defineOperation(global, 'clearInterval', timers.clearInterval);
defineOperation(global, 'clearTimeout', timers.clearTimeout);
defineOperation(global, 'setInterval', timers.setInterval);
defineOperation(global, 'setTimeout', timers.setTimeout);
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
defineOperation(globalThis, 'setInterval', timers.setInterval);
defineOperation(globalThis, 'setTimeout', timers.setTimeout);

defineOperation(global, 'queueMicrotask', queueMicrotask);
defineOperation(globalThis, 'queueMicrotask', queueMicrotask);

// Non-standard extensions:
defineOperation(global, 'clearImmediate', timers.clearImmediate);
defineOperation(global, 'setImmediate', timers.setImmediate);
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
defineOperation(globalThis, 'setImmediate', timers.setImmediate);
}

// Set the per-Environment callback that will be called
Expand Down Expand Up @@ -280,7 +282,7 @@ function setupProcessObject() {
value: 'process'
});
// Make process globally available to users by putting it on the global proxy
ObjectDefineProperty(global, 'process', {
ObjectDefineProperty(globalThis, 'process', {
value: process,
enumerable: false,
writable: true,
Expand All @@ -289,7 +291,7 @@ function setupProcessObject() {
}

function setupGlobalProxy() {
ObjectDefineProperty(global, SymbolToStringTag, {
ObjectDefineProperty(globalThis, SymbolToStringTag, {
value: 'global',
writable: false,
enumerable: false,
Expand All @@ -306,7 +308,7 @@ function setupBuffer() {
delete bufferBinding.setBufferPrototype;
delete bufferBinding.zeroFill;

ObjectDefineProperty(global, 'Buffer', {
ObjectDefineProperty(globalThis, 'Buffer', {
value: Buffer,
enumerable: false,
writable: true,
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
SafeMap,
SafeWeakMap,
StringPrototypeStartsWith,
globalThis,
} = primordials;

const {
Expand Down Expand Up @@ -290,7 +291,7 @@ function initializeDeprecations() {
// deprecation path for these in ES Modules.
// See https://github.com/nodejs/node/pull/26334.
let _process = process;
ObjectDefineProperty(global, 'process', {
ObjectDefineProperty(globalThis, 'process', {
get() {
return _process;
},
Expand All @@ -302,7 +303,7 @@ function initializeDeprecations() {
});

let _Buffer = Buffer;
ObjectDefineProperty(global, 'Buffer', {
ObjectDefineProperty(globalThis, 'Buffer', {
get() {
return _Buffer;
},
Expand All @@ -321,7 +322,7 @@ function initializeAbortController() {
AbortController,
AbortSignal
} = require('internal/abort_controller');
ObjectDefineProperties(global, {
ObjectDefineProperties(globalThis, {
AbortController: {
writable: true,
enumerable: false,
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// User passed `-e` or `--eval` arguments to Node without `-i` or
// `--interactive`.

const {
globalThis,
} = primordials;

const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
Expand All @@ -12,7 +16,7 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
const { getOptionValue } = require('internal/options');

prepareMainThreadExecution();
addBuiltinLibsToObject(global);
addBuiltinLibsToObject(globalThis);
markBootstrapComplete();

const source = getOptionValue('--eval');
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ primordials.SafeWeakSet = makeSafe(
'Math',
'Reflect'
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
copyPropsRenamed(global[name], primordials, name);
});

Expand Down Expand Up @@ -235,6 +236,7 @@ primordials.SafeWeakSet = makeSafe(
'WeakMap',
'WeakSet',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
const original = global[name];
primordials[name] = original;
copyPropsRenamed(original, primordials, name);
Expand All @@ -247,6 +249,7 @@ primordials.SafeWeakSet = makeSafe(
[
'Promise',
].forEach((name) => {
// eslint-disable-next-line no-restricted-globals
const original = global[name];
primordials[name] = original;
copyPropsRenamedBound(original, primordials, name);
Expand Down
11 changes: 9 additions & 2 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
Array,
ArrayIsArray,
ArrayPrototypeFilter,
BigIntPrototypeValueOf,
BooleanPrototypeValueOf,
DatePrototypeGetTime,
Expand Down Expand Up @@ -41,7 +42,9 @@ const {
ObjectSetPrototypeOf,
ReflectApply,
RegExp,
RegExpPrototypeExec,
RegExpPrototypeToString,
SafeSet,
SafeStringIterator,
Set,
SetPrototypeGetSize,
Expand All @@ -55,6 +58,7 @@ const {
TypedArrayPrototypeGetLength,
TypedArrayPrototypeGetSymbolToStringTag,
Uint8Array,
globalThis,
uncurryThis,
} = primordials;

Expand Down Expand Up @@ -120,8 +124,11 @@ const { NativeModule } = require('internal/bootstrap/loaders');

let hexSlice;

const builtInObjects = new Set(
ObjectGetOwnPropertyNames(global).filter((e) => /^[A-Z][a-zA-Z0-9]+$/.test(e))
const builtInObjects = new SafeSet(
ArrayPrototypeFilter(
ObjectGetOwnPropertyNames(globalThis),
(e) => RegExpPrototypeExec(/^[A-Z][a-zA-Z0-9]+$/, e) !== null
)
);

// https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot
Expand Down
10 changes: 6 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'use strict';

const {
ArrayPrototypeForEach,
Error,
MathMax,
NumberIsNaN,
Expand All @@ -67,6 +68,7 @@ const {
SyntaxError,
SyntaxErrorPrototype,
WeakSet,
globalThis,
} = primordials;

const {
Expand Down Expand Up @@ -984,7 +986,7 @@ REPLServer.prototype.close = function close() {
REPLServer.prototype.createContext = function() {
let context;
if (this.useGlobal) {
context = global;
context = globalThis;
} else {
sendInspectorCommand((session) => {
session.post('Runtime.enable');
Expand All @@ -996,13 +998,13 @@ REPLServer.prototype.createContext = function() {
}, () => {
context = vm.createContext();
});
for (const name of ObjectGetOwnPropertyNames(global)) {
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => {
// Only set properties that do not already exist as a global builtin.
if (!globalBuiltins.has(name)) {
ObjectDefineProperty(context, name,
ObjectGetOwnPropertyDescriptor(global, name));
ObjectGetOwnPropertyDescriptor(globalThis, name));
}
}
});
context.global = context;
const _console = new Console(this.output);
ObjectDefineProperty(context, 'console', {
Expand Down

0 comments on commit d4f96bb

Please sign in to comment.