Skip to content

Commit d756d2b

Browse files
committed
lib: enforce using primordials.globalThis instead of global
PR-URL: #38230 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]>
1 parent 7d688d4 commit d756d2b

File tree

7 files changed

+44
-30
lines changed

7 files changed

+44
-30
lines changed

Diff for: 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

Diff for: lib/internal/bootstrap/node.js

+25-23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const {
4949
ReflectGet,
5050
ReflectSet,
5151
SymbolToStringTag,
52+
globalThis,
5253
} = primordials;
5354
const config = internalBinding('config');
5455
const { deprecate } = require('internal/util');
@@ -189,61 +190,62 @@ if (!config.noBrowserGlobals) {
189190
// Override global console from the one provided by the VM
190191
// to the one implemented by Node.js
191192
// https://console.spec.whatwg.org/#console-namespace
192-
exposeNamespace(global, 'console', createGlobalConsole(global.console));
193+
exposeNamespace(globalThis, 'console',
194+
createGlobalConsole(globalThis.console));
193195

194196
const { URL, URLSearchParams } = require('internal/url');
195197
// https://url.spec.whatwg.org/#url
196-
exposeInterface(global, 'URL', URL);
198+
exposeInterface(globalThis, 'URL', URL);
197199
// https://url.spec.whatwg.org/#urlsearchparams
198-
exposeInterface(global, 'URLSearchParams', URLSearchParams);
200+
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
199201

200202
const {
201203
TextEncoder, TextDecoder
202204
} = require('internal/encoding');
203205
// https://encoding.spec.whatwg.org/#textencoder
204-
exposeInterface(global, 'TextEncoder', TextEncoder);
206+
exposeInterface(globalThis, 'TextEncoder', TextEncoder);
205207
// https://encoding.spec.whatwg.org/#textdecoder
206-
exposeInterface(global, 'TextDecoder', TextDecoder);
208+
exposeInterface(globalThis, 'TextDecoder', TextDecoder);
207209

208210
const {
209211
AbortController,
210212
AbortSignal,
211213
} = require('internal/abort_controller');
212-
exposeInterface(global, 'AbortController', AbortController);
213-
exposeInterface(global, 'AbortSignal', AbortSignal);
214+
exposeInterface(globalThis, 'AbortController', AbortController);
215+
exposeInterface(globalThis, 'AbortSignal', AbortSignal);
214216

215217
const {
216218
EventTarget,
217219
Event,
218220
} = require('internal/event_target');
219-
exposeInterface(global, 'EventTarget', EventTarget);
220-
exposeInterface(global, 'Event', Event);
221+
exposeInterface(globalThis, 'EventTarget', EventTarget);
222+
exposeInterface(globalThis, 'Event', Event);
221223
const {
222224
MessageChannel,
223225
MessagePort,
224226
MessageEvent,
225227
} = require('internal/worker/io');
226-
exposeInterface(global, 'MessageChannel', MessageChannel);
227-
exposeInterface(global, 'MessagePort', MessagePort);
228-
exposeInterface(global, 'MessageEvent', MessageEvent);
228+
exposeInterface(globalThis, 'MessageChannel', MessageChannel);
229+
exposeInterface(globalThis, 'MessagePort', MessagePort);
230+
exposeInterface(globalThis, 'MessageEvent', MessageEvent);
229231

230232
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
231233
const timers = require('timers');
232-
defineOperation(global, 'clearInterval', timers.clearInterval);
233-
defineOperation(global, 'clearTimeout', timers.clearTimeout);
234-
defineOperation(global, 'setInterval', timers.setInterval);
235-
defineOperation(global, 'setTimeout', timers.setTimeout);
234+
defineOperation(globalThis, 'clearInterval', timers.clearInterval);
235+
defineOperation(globalThis, 'clearTimeout', timers.clearTimeout);
236+
defineOperation(globalThis, 'setInterval', timers.setInterval);
237+
defineOperation(globalThis, 'setTimeout', timers.setTimeout);
236238

237-
defineOperation(global, 'queueMicrotask', queueMicrotask);
239+
defineOperation(globalThis, 'queueMicrotask', queueMicrotask);
238240

239-
defineLazyGlobal(global, 'performance', () => {
241+
defineLazyGlobal(globalThis, 'performance', () => {
240242
const { performance } = require('perf_hooks');
241243
return performance;
242244
});
243245

244246
// Non-standard extensions:
245-
defineOperation(global, 'clearImmediate', timers.clearImmediate);
246-
defineOperation(global, 'setImmediate', timers.setImmediate);
247+
defineOperation(globalThis, 'clearImmediate', timers.clearImmediate);
248+
defineOperation(globalThis, 'setImmediate', timers.setImmediate);
247249
}
248250

249251
// Set the per-Environment callback that will be called
@@ -388,7 +390,7 @@ function setupProcessObject() {
388390
value: 'process'
389391
});
390392
// Make process globally available to users by putting it on the global proxy
391-
ObjectDefineProperty(global, 'process', {
393+
ObjectDefineProperty(globalThis, 'process', {
392394
value: process,
393395
enumerable: false,
394396
writable: true,
@@ -397,7 +399,7 @@ function setupProcessObject() {
397399
}
398400

399401
function setupGlobalProxy() {
400-
ObjectDefineProperty(global, SymbolToStringTag, {
402+
ObjectDefineProperty(globalThis, SymbolToStringTag, {
401403
value: 'global',
402404
writable: false,
403405
enumerable: false,
@@ -418,7 +420,7 @@ function setupBuffer() {
418420
delete bufferBinding.setBufferPrototype;
419421
delete bufferBinding.zeroFill;
420422

421-
ObjectDefineProperties(global, {
423+
ObjectDefineProperties(globalThis, {
422424
'Buffer': {
423425
value: Buffer,
424426
enumerable: false,

Diff for: lib/internal/bootstrap/pre_execution.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
SafeMap,
77
SafeWeakMap,
88
StringPrototypeStartsWith,
9+
globalThis,
910
} = primordials;
1011

1112
const {
@@ -302,7 +303,7 @@ function initializeDeprecations() {
302303
// deprecation path for these in ES Modules.
303304
// See https://github.com/nodejs/node/pull/26334.
304305
let _process = process;
305-
ObjectDefineProperty(global, 'process', {
306+
ObjectDefineProperty(globalThis, 'process', {
306307
get() {
307308
return _process;
308309
},
@@ -314,7 +315,7 @@ function initializeDeprecations() {
314315
});
315316

316317
let _Buffer = Buffer;
317-
ObjectDefineProperty(global, 'Buffer', {
318+
ObjectDefineProperty(globalThis, 'Buffer', {
318319
get() {
319320
return _Buffer;
320321
},

Diff for: 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');

Diff for: lib/internal/per_context/primordials.js

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function copyPrototype(src, dest, prefix) {
155155
'Math',
156156
'Reflect',
157157
].forEach((name) => {
158+
// eslint-disable-next-line no-restricted-globals
158159
copyPropsRenamed(global[name], primordials, name);
159160
});
160161

@@ -198,6 +199,7 @@ function copyPrototype(src, dest, prefix) {
198199
'WeakRef',
199200
'WeakSet',
200201
].forEach((name) => {
202+
// eslint-disable-next-line no-restricted-globals
201203
const original = global[name];
202204
primordials[name] = original;
203205
copyPropsRenamed(original, primordials, name);
@@ -210,6 +212,7 @@ function copyPrototype(src, dest, prefix) {
210212
[
211213
'Promise',
212214
].forEach((name) => {
215+
// eslint-disable-next-line no-restricted-globals
213216
const original = global[name];
214217
primordials[name] = original;
215218
copyPropsRenamedBound(original, primordials, name);

Diff for: lib/internal/util/inspect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const {
7474
TypedArrayPrototypeGetLength,
7575
TypedArrayPrototypeGetSymbolToStringTag,
7676
Uint8Array,
77+
globalThis,
7778
uncurryThis,
7879
} = primordials;
7980

@@ -144,7 +145,7 @@ let hexSlice;
144145

145146
const builtInObjects = new SafeSet(
146147
ArrayPrototypeFilter(
147-
ObjectGetOwnPropertyNames(global),
148+
ObjectGetOwnPropertyNames(globalThis),
148149
(e) => RegExpPrototypeTest(/^[A-Z][a-zA-Z0-9]+$/, e)
149150
)
150151
);

Diff for: lib/repl.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const {
9898
Symbol,
9999
SyntaxError,
100100
SyntaxErrorPrototype,
101+
globalThis,
101102
} = primordials;
102103

103104
const {
@@ -1009,7 +1010,7 @@ REPLServer.prototype.close = function close() {
10091010
REPLServer.prototype.createContext = function() {
10101011
let context;
10111012
if (this.useGlobal) {
1012-
context = global;
1013+
context = globalThis;
10131014
} else {
10141015
sendInspectorCommand((session) => {
10151016
session.post('Runtime.enable');
@@ -1021,11 +1022,11 @@ REPLServer.prototype.createContext = function() {
10211022
}, () => {
10221023
context = vm.createContext();
10231024
});
1024-
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(global), (name) => {
1025+
ArrayPrototypeForEach(ObjectGetOwnPropertyNames(globalThis), (name) => {
10251026
// Only set properties that do not already exist as a global builtin.
10261027
if (!globalBuiltins.has(name)) {
10271028
ObjectDefineProperty(context, name,
1028-
ObjectGetOwnPropertyDescriptor(global, name));
1029+
ObjectGetOwnPropertyDescriptor(globalThis, name));
10291030
}
10301031
});
10311032
context.global = context;

0 commit comments

Comments
 (0)