Skip to content

Commit 6d272f7

Browse files
committed
lib: add internal/primordials-staging module
Signed-off-by: LiviaMedeiros <livia@cirno.name>
1 parent 2ebf533 commit 6d272f7

9 files changed

Lines changed: 105 additions & 13 deletions

File tree

lib/internal/freeze_intrinsics.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,12 @@ const {
128128
globalThis,
129129
unescape,
130130
} = primordials;
131-
132131
const {
133132
Intl,
134133
SharedArrayBuffer,
135134
Temporal,
136135
WebAssembly,
137-
} = globalThis;
136+
} = require('internal/primordials_staging');
138137

139138
module.exports = function() {
140139
const { Console } = require('internal/console/constructor');

lib/internal/fs/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const {
2323
Symbol,
2424
TypedArrayPrototypeAt,
2525
TypedArrayPrototypeIncludes,
26-
globalThis,
2726
} = primordials;
27+
const primordialsStaging = require('internal/primordials_staging');
2828

2929
const { Buffer } = require('buffer');
3030
const {
@@ -441,15 +441,15 @@ function nsFromTimeSpecBigInt(sec, nsec) {
441441
let TemporalInstant;
442442

443443
function instantFromNs(nsec) {
444-
TemporalInstant ??= globalThis.Temporal?.Instant;
444+
TemporalInstant ??= primordialsStaging.Temporal?.Instant;
445445
if (TemporalInstant === undefined) {
446446
throw new ERR_NO_TEMPORAL();
447447
}
448448
return new TemporalInstant(nsec);
449449
}
450450

451451
function instantFromTimeSpecMs(msec, nsec) {
452-
TemporalInstant ??= globalThis.Temporal?.Instant;
452+
TemporalInstant ??= primordialsStaging.Temporal?.Instant;
453453
if (TemporalInstant === undefined) {
454454
throw new ERR_NO_TEMPORAL();
455455
}

lib/internal/modules/esm/translators.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const {
1313
StringPrototypeReplaceAll,
1414
StringPrototypeSlice,
1515
StringPrototypeStartsWith,
16-
globalThis,
1716
} = primordials;
17+
const primordialsStaging = require('internal/primordials_staging');
1818

1919
const {
2020
compileFunctionForCJSLoader,
@@ -554,7 +554,7 @@ const wasmInstances = new SafeWeakMap();
554554
translators.set('wasm', function(url, translateContext) {
555555
const { source } = translateContext;
556556
// WebAssembly global is not available during snapshot building, so we need to get it lazily.
557-
const { WebAssembly } = globalThis;
557+
const { WebAssembly } = primordialsStaging;
558558
assertBufferSource(source, false, 'load');
559559

560560
debug(`Translating WASMModule ${url}`, translateContext);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// This file stores JS builting that can not become primordials yet because
4+
// they can be disabled by runtime flags, or are not enabled by default yet.
5+
6+
// Without this module, the only choice we would have is getting them from
7+
// the global proxy which is mutable from userland. This is especially important
8+
// for lazy-loaded builting required in modules participating in early bootstrap.
9+
// In such modules, we are usually unable to get these builting synchronously,
10+
// this applies to synchronous destructuring of this module.
11+
// Importing the module itself is fine at any point, including top level of file.
12+
13+
let _AsyncDisposableStack;
14+
let _DisposableStack;
15+
let _Float16Array;
16+
let _Intl;
17+
let _Iterator;
18+
let _ShadowRealm;
19+
let _SharedArrayBuffer;
20+
let _Temporal;
21+
let _Uint8ArrayFromHex;
22+
let _Uint8ArrayFromBase64;
23+
let _WebAssembly;
24+
25+
module.exports = {
26+
get AsyncDisposableStack() {
27+
return _AsyncDisposableStack;
28+
},
29+
get DisposableStack() {
30+
return _DisposableStack;
31+
},
32+
get Float16Array() {
33+
return _Float16Array;
34+
},
35+
get Intl() {
36+
return _Intl;
37+
},
38+
get Iterator() {
39+
return _Iterator;
40+
},
41+
get ShadowRealm() {
42+
return _ShadowRealm;
43+
},
44+
get SharedArrayBuffer() {
45+
return _SharedArrayBuffer;
46+
},
47+
get Temporal() {
48+
return _Temporal;
49+
},
50+
get Uint8ArrayFromHex() {
51+
return _Uint8ArrayFromHex;
52+
},
53+
get Uint8ArrayFromBase64() {
54+
return _Uint8ArrayFromBase64;
55+
},
56+
get WebAssembly() {
57+
return _WebAssembly;
58+
},
59+
_init({
60+
AsyncDisposableStack,
61+
DisposableStack,
62+
Float16Array,
63+
Intl,
64+
Iterator,
65+
ShadowRealm,
66+
SharedArrayBuffer,
67+
Temporal,
68+
Uint8Array: {
69+
fromBase64,
70+
fromHex,
71+
},
72+
WebAssembly,
73+
}) {
74+
_AsyncDisposableStack = AsyncDisposableStack;
75+
_DisposableStack = DisposableStack;
76+
_Float16Array = Float16Array;
77+
_Intl = Intl;
78+
_Iterator = Iterator;
79+
_ShadowRealm = ShadowRealm;
80+
_SharedArrayBuffer = SharedArrayBuffer;
81+
_Temporal = Temporal;
82+
_Uint8ArrayFromBase64 = fromBase64;
83+
_Uint8ArrayFromHex = fromHex;
84+
_WebAssembly = WebAssembly;
85+
86+
delete this._init;
87+
},
88+
};

lib/internal/process/pre_execution.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ function prepareExecution(options) {
107107

108108
refreshRuntimeOptions();
109109

110+
require('internal/primordials_staging')._init?.(globalThis);
111+
110112
// Patch the process object and get the resolved main entry point.
111113
const mainEntry = patchProcessObject(expandArgv1);
112114
setupTraceCategoryState();

lib/internal/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const {
4646
SymbolPrototypeGetDescription,
4747
SymbolReplace,
4848
SymbolSplit,
49-
globalThis,
5049
} = primordials;
50+
const stagingPrimordials = require('internal/primordials_staging');
5151

5252
const {
5353
codes: {
@@ -246,7 +246,7 @@ function assertCrypto() {
246246
function assertTypeScript() {
247247
if (noTypeScript)
248248
throw new ERR_NO_TYPESCRIPT();
249-
if (globalThis.WebAssembly === undefined)
249+
if (stagingPrimordials.WebAssembly === undefined)
250250
throw new ERR_WEBASSEMBLY_NOT_SUPPORTED('TypeScript');
251251
}
252252

lib/internal/util/comparisons.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ const {
5050
Uint8ClampedArray,
5151
WeakMap,
5252
WeakSet,
53-
globalThis: { Float16Array },
5453
} = primordials;
54+
const {
55+
Float16Array,
56+
} = require('internal/primordials_staging');
5557

5658
const { compare } = internalBinding('buffer');
5759
const assert = require('internal/assert');

lib/v8.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ const {
3232
Uint32Array,
3333
Uint8Array,
3434
Uint8ClampedArray,
35-
globalThis: {
36-
Float16Array,
37-
},
3835
} = primordials;
36+
const {
37+
Float16Array,
38+
} = require('internal/primordials_staging');
3939

4040
const { Buffer } = require('buffer');
4141
const {

test/parallel/test-bootstrap-modules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ expected.beforePreExec = new Set([
117117
'NativeModule internal/net',
118118
'NativeModule internal/dns/utils',
119119
'NativeModule internal/modules/esm/get_format',
120+
'NativeModule internal/primordials_staging',
120121
]);
121122

122123
expected.atRunTime = new Set([

0 commit comments

Comments
 (0)