Skip to content

Commit d4f96bb

Browse files
aduh95richardlau
authored andcommitted
lib: enforce using primordials.globalThis instead of global
PR-URL: #38230 Backport-PR-URL: #39448 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent ea9003a commit d4f96bb

File tree

7 files changed

+46
-25
lines changed

7 files changed

+46
-25
lines changed

lib/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ rules:
2929
- error
3030
- name: globalThis
3131
message: "Use `const { globalThis } = primordials;` instead of the global."
32+
- name: global
33+
message: "Use `const { globalThis } = primordials;` instead of `global`."
3234
# Custom rules in tools/eslint-rules
3335
node-core/lowercase-name-for-primitive: error
3436
node-core/non-ascii-character: error

lib/internal/bootstrap/node.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
ObjectGetPrototypeOf,
4646
ObjectSetPrototypeOf,
4747
SymbolToStringTag,
48+
globalThis,
4849
} = primordials;
4950
const config = internalBinding('config');
5051
const { deprecate } = require('internal/util');
@@ -119,34 +120,35 @@ if (!config.noBrowserGlobals) {
119120
// Override global console from the one provided by the VM
120121
// to the one implemented by Node.js
121122
// https://console.spec.whatwg.org/#console-namespace
122-
exposeNamespace(global, 'console', createGlobalConsole(global.console));
123+
exposeNamespace(globalThis, 'console',
124+
createGlobalConsole(globalThis.console));
123125

124126
const { URL, URLSearchParams } = require('internal/url');
125127
// https://url.spec.whatwg.org/#url
126-
exposeInterface(global, 'URL', URL);
128+
exposeInterface(globalThis, 'URL', URL);
127129
// https://url.spec.whatwg.org/#urlsearchparams
128-
exposeInterface(global, 'URLSearchParams', URLSearchParams);
130+
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
129131

130132
const {
131133
TextEncoder, TextDecoder
132134
} = require('internal/encoding');
133135
// https://encoding.spec.whatwg.org/#textencoder
134-
exposeInterface(global, 'TextEncoder', TextEncoder);
136+
exposeInterface(globalThis, 'TextEncoder', TextEncoder);
135137
// https://encoding.spec.whatwg.org/#textdecoder
136-
exposeInterface(global, 'TextDecoder', TextDecoder);
138+
exposeInterface(globalThis, 'TextDecoder', TextDecoder);
137139

138140
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
139141
const timers = require('timers');
140-
defineOperation(global, 'clearInterval', timers.clearInterval);
141-
defineOperation(global, 'clearTimeout', timers.clearTimeout);
142-
defineOperation(global, 'setInterval', timers.setInterval);
143-
defineOperation(global, 'setTimeout', timers.setTimeout);
142+
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
143+
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
144+
defineOperation(globalThis, 'setInterval', timers.setInterval);
145+
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
144146

145-
defineOperation(global, 'queueMicrotask', queueMicrotask);
147+
defineOperation(globalThis, 'queueMicrotask', queueMicrotask);
146148

147149
// Non-standard extensions:
148-
defineOperation(global, 'clearImmediate', timers.clearImmediate);
149-
defineOperation(global, 'setImmediate', timers.setImmediate);
150+
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
151+
defineOperation(globalThis, 'setImmediate', timers.setImmediate);
150152
}
151153

152154
// Set the per-Environment callback that will be called
@@ -280,7 +282,7 @@ function setupProcessObject() {
280282
value: 'process'
281283
});
282284
// Make process globally available to users by putting it on the global proxy
283-
ObjectDefineProperty(global, 'process', {
285+
ObjectDefineProperty(globalThis, 'process', {
284286
value: process,
285287
enumerable: false,
286288
writable: true,
@@ -289,7 +291,7 @@ function setupProcessObject() {
289291
}
290292

291293
function setupGlobalProxy() {
292-
ObjectDefineProperty(global, SymbolToStringTag, {
294+
ObjectDefineProperty(globalThis, SymbolToStringTag, {
293295
value: 'global',
294296
writable: false,
295297
enumerable: false,
@@ -306,7 +308,7 @@ function setupBuffer() {
306308
delete bufferBinding.setBufferPrototype;
307309
delete bufferBinding.zeroFill;
308310

309-
ObjectDefineProperty(global, 'Buffer', {
311+
ObjectDefineProperty(globalThis, 'Buffer', {
310312
value: Buffer,
311313
enumerable: false,
312314
writable: true,

lib/internal/bootstrap/pre_execution.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
SafeMap,
88
SafeWeakMap,
99
StringPrototypeStartsWith,
10+
globalThis,
1011
} = primordials;
1112

1213
const {
@@ -290,7 +291,7 @@ function initializeDeprecations() {
290291
// deprecation path for these in ES Modules.
291292
// See https://github.com/nodejs/node/pull/26334.
292293
let _process = process;
293-
ObjectDefineProperty(global, 'process', {
294+
ObjectDefineProperty(globalThis, 'process', {
294295
get() {
295296
return _process;
296297
},
@@ -302,7 +303,7 @@ function initializeDeprecations() {
302303
});
303304

304305
let _Buffer = Buffer;
305-
ObjectDefineProperty(global, 'Buffer', {
306+
ObjectDefineProperty(globalThis, 'Buffer', {
306307
get() {
307308
return _Buffer;
308309
},
@@ -321,7 +322,7 @@ function initializeAbortController() {
321322
AbortController,
322323
AbortSignal
323324
} = require('internal/abort_controller');
324-
ObjectDefineProperties(global, {
325+
ObjectDefineProperties(globalThis, {
325326
AbortController: {
326327
writable: true,
327328
enumerable: false,

lib/internal/main/eval_string.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// User passed `-e` or `--eval` arguments to Node without `-i` or
44
// `--interactive`.
55

6+
const {
7+
globalThis,
8+
} = primordials;
9+
610
const {
711
prepareMainThreadExecution
812
} = require('internal/bootstrap/pre_execution');
@@ -12,7 +16,7 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
1216
const { getOptionValue } = require('internal/options');
1317

1418
prepareMainThreadExecution();
15-
addBuiltinLibsToObject(global);
19+
addBuiltinLibsToObject(globalThis);
1620
markBootstrapComplete();
1721

1822
const source = getOptionValue('--eval');

lib/internal/per_context/primordials.js

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ primordials.SafeWeakSet = makeSafe(
195195
'Math',
196196
'Reflect'
197197
].forEach((name) => {
198+
// eslint-disable-next-line no-restricted-globals
198199
copyPropsRenamed(global[name], primordials, name);
199200
});
200201

@@ -235,6 +236,7 @@ primordials.SafeWeakSet = makeSafe(
235236
'WeakMap',
236237
'WeakSet',
237238
].forEach((name) => {
239+
// eslint-disable-next-line no-restricted-globals
238240
const original = global[name];
239241
primordials[name] = original;
240242
copyPropsRenamed(original, primordials, name);
@@ -247,6 +249,7 @@ primordials.SafeWeakSet = makeSafe(
247249
[
248250
'Promise',
249251
].forEach((name) => {
252+
// eslint-disable-next-line no-restricted-globals
250253
const original = global[name];
251254
primordials[name] = original;
252255
copyPropsRenamedBound(original, primordials, name);

lib/internal/util/inspect.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
Array,
55
ArrayIsArray,
6+
ArrayPrototypeFilter,
67
BigIntPrototypeValueOf,
78
BooleanPrototypeValueOf,
89
DatePrototypeGetTime,
@@ -41,7 +42,9 @@ const {
4142
ObjectSetPrototypeOf,
4243
ReflectApply,
4344
RegExp,
45+
RegExpPrototypeExec,
4446
RegExpPrototypeToString,
47+
SafeSet,
4548
SafeStringIterator,
4649
Set,
4750
SetPrototypeGetSize,
@@ -55,6 +58,7 @@ const {
5558
TypedArrayPrototypeGetLength,
5659
TypedArrayPrototypeGetSymbolToStringTag,
5760
Uint8Array,
61+
globalThis,
5862
uncurryThis,
5963
} = primordials;
6064

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

121125
let hexSlice;
122126

123-
const builtInObjects = new Set(
124-
ObjectGetOwnPropertyNames(global).filter((e) => /^[A-Z][a-zA-Z0-9]+$/.test(e))
127+
const builtInObjects = new SafeSet(
128+
ArrayPrototypeFilter(
129+
ObjectGetOwnPropertyNames(globalThis),
130+
(e) => RegExpPrototypeExec(/^[A-Z][a-zA-Z0-9]+$/, e) !== null
131+
)
125132
);
126133

127134
// https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot

lib/repl.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'use strict';
4444

4545
const {
46+
ArrayPrototypeForEach,
4647
Error,
4748
MathMax,
4849
NumberIsNaN,
@@ -67,6 +68,7 @@ const {
6768
SyntaxError,
6869
SyntaxErrorPrototype,
6970
WeakSet,
71+
globalThis,
7072
} = primordials;
7173

7274
const {
@@ -984,7 +986,7 @@ REPLServer.prototype.close = function close() {
984986
REPLServer.prototype.createContext = function() {
985987
let context;
986988
if (this.useGlobal) {
987-
context = global;
989+
context = globalThis;
988990
} else {
989991
sendInspectorCommand((session) => {
990992
session.post('Runtime.enable');
@@ -996,13 +998,13 @@ REPLServer.prototype.createContext = function() {
996998
}, () => {
997999
context = vm.createContext();
9981000
});
999-
for (const name of ObjectGetOwnPropertyNames(global)) {
1001+
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => {
10001002
// Only set properties that do not already exist as a global builtin.
10011003
if (!globalBuiltins.has(name)) {
10021004
ObjectDefineProperty(context, name,
1003-
ObjectGetOwnPropertyDescriptor(global, name));
1005+
ObjectGetOwnPropertyDescriptor(globalThis, name));
10041006
}
1005-
}
1007+
});
10061008
context.global = context;
10071009
const _console = new Console(this.output);
10081010
ObjectDefineProperty(context, 'console', {

0 commit comments

Comments
 (0)