From bb94e594f68da118c040783c0a13c4d964958be6 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 12 Apr 2021 16:02:33 +0200 Subject: [PATCH] lib: add `globalThis` to primordials PR-URL: https://github.com/nodejs/node/pull/38211 Reviewed-By: Bradley Farias Reviewed-By: James M Snell --- lib/.eslintrc.yaml | 4 ++++ lib/internal/modules/esm/loader.js | 1 + lib/internal/per_context/primordials.js | 8 ++++++++ lib/internal/process/execution.js | 4 ++++ lib/internal/test/binding.js | 4 ++++ lib/internal/util/iterable_weak_map.js | 1 + 6 files changed, 22 insertions(+) diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 05142865e12802..626bac70930703 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -25,6 +25,10 @@ rules: message: "Please use `require('internal/errors').hideStackFrames()` instead." - selector: "AssignmentExpression:matches([left.name='prepareStackTrace'], [left.property.name='prepareStackTrace'])" message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'." + no-restricted-globals: + - error + - name: globalThis + message: "Use `const { globalThis } = primordials;` instead of the global." # Custom rules in tools/eslint-rules node-core/lowercase-name-for-primitive: error node-core/non-ascii-character: error diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 14e66dcb9fdf62..8b2c89ba130aa7 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -9,6 +9,7 @@ const { RegExpPrototypeExec, SafeWeakMap, StringPrototypeStartsWith, + globalThis, } = primordials; const { diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 6492da782a9edb..595be166546e1b 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -171,6 +171,14 @@ primordials.SafeWeakSet = makeSafe( } ); +// Create copies of configurable value properties of the global object +[ + 'globalThis', +].forEach((name) => { + // eslint-disable-next-line no-restricted-globals + primordials[name] = globalThis[name]; +}); + // Create copies of URI handling functions [ decodeURI, diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 314b8b0a03c161..e370770643ca6f 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -1,5 +1,9 @@ 'use strict'; +const { + globalThis, +} = primordials; + const path = require('path'); const { diff --git a/lib/internal/test/binding.js b/lib/internal/test/binding.js index 063b9b5c900aad..d00ef89a16111b 100644 --- a/lib/internal/test/binding.js +++ b/lib/internal/test/binding.js @@ -1,5 +1,9 @@ 'use strict'; +const { + globalThis, +} = primordials; + process.emitWarning( 'These APIs are for internal testing only. Do not use them.', 'internal/test/binding'); diff --git a/lib/internal/util/iterable_weak_map.js b/lib/internal/util/iterable_weak_map.js index 464ba74281127b..06d0001970635f 100644 --- a/lib/internal/util/iterable_weak_map.js +++ b/lib/internal/util/iterable_weak_map.js @@ -6,6 +6,7 @@ const { SafeSet, SafeWeakMap, SymbolIterator, + globalThis, } = primordials; // TODO(aduh95): Add FinalizationRegistry to primordials