Skip to content

Commit 33baaf4

Browse files
Renegade334aduh95
authored andcommitted
lib: replace global SharedArrayBuffer constructor with bound method
PR-URL: #60497 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 9b79634 commit 33baaf4

File tree

5 files changed

+33
-41
lines changed

5 files changed

+33
-41
lines changed

‎lib/internal/main/worker_thread.js‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const{
1111
ObjectDefineProperty,
1212
PromisePrototypeThen,
1313
RegExpPrototypeExec,
14-
globalThis: {
15-
SharedArrayBuffer,
16-
},
1714
}=primordials;
1815

1916
const{
@@ -119,23 +116,21 @@ port.on('message', (message) =>{
119116
require('internal/worker').assignEnvironmentData(environmentData);
120117
setupMainThreadPort(mainThreadPort);
121118

122-
if(SharedArrayBuffer!==undefined){
123-
// The counter is only passed to the workers created by the main thread,
124-
// not to workers created by other workers.
125-
letcachedCwd='';
126-
letlastCounter=-1;
127-
constoriginalCwd=process.cwd;
128-
129-
process.cwd=function(){
130-
constcurrentCounter=AtomicsLoad(cwdCounter,0);
131-
if(currentCounter===lastCounter)
132-
returncachedCwd;
133-
lastCounter=currentCounter;
134-
cachedCwd=originalCwd();
119+
// The counter is only passed to the workers created by the main thread,
120+
// not to workers created by other workers.
121+
letcachedCwd='';
122+
letlastCounter=-1;
123+
constoriginalCwd=process.cwd;
124+
125+
process.cwd=function(){
126+
constcurrentCounter=AtomicsLoad(cwdCounter,0);
127+
if(currentCounter===lastCounter)
135128
returncachedCwd;
136-
};
137-
workerIo.sharedCwdCounter=cwdCounter;
138-
}
129+
lastCounter=currentCounter;
130+
cachedCwd=originalCwd();
131+
returncachedCwd;
132+
};
133+
workerIo.sharedCwdCounter=cwdCounter;
139134

140135
constisLoaderHookWorker=(filename==='internal/modules/esm/worker'&&doEval==='internal');
141136
if(!isLoaderHookWorker){

‎lib/internal/modules/esm/hooks.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ const{
1515
SafeSet,
1616
StringPrototypeSlice,
1717
StringPrototypeToUpperCase,
18-
globalThis,
1918
}=primordials;
2019

21-
const{
22-
SharedArrayBuffer,
23-
}=globalThis;
24-
2520
const{
2621
ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED,
2722
ERR_INTERNAL_ASSERTION,
@@ -44,6 +39,7 @@ const{
4439
validateString,
4540
}=require('internal/validators');
4641
const{
42+
constructSharedArrayBuffer,
4743
kEmptyObject,
4844
}=require('internal/util');
4945

@@ -535,7 +531,7 @@ class AsyncLoaderHookWorker{
535531
const{ InternalWorker }=require('internal/worker');
536532
MessageChannel??=require('internal/worker/io').MessageChannel;
537533

538-
constlock=newSharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
534+
constlock=constructSharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
539535
this.#lock =newInt32Array(lock);
540536

541537
this.#worker =newInternalWorker('internal/modules/esm/worker',{

‎lib/internal/streams/fast-utf8-stream.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ const{
99
AtomicsWait,
1010
Int32Array,
1111
MathMax,
12+
Number,
1213
SymbolDispose,
13-
globalThis: {
14-
Number,
15-
SharedArrayBuffer,
16-
},
1714
}=primordials;
1815

16+
const{
17+
constructSharedArrayBuffer,
18+
}=require('internal/util');
19+
1920
const{
2021
Buffer,
2122
}=require('buffer');
@@ -49,7 +50,7 @@ const{
4950
constBUSY_WRITE_TIMEOUT=100;
5051
constkEmptyBuffer=Buffer.allocUnsafe(0);
5152

52-
constkNil=newInt32Array(newSharedArrayBuffer(4));
53+
constkNil=newInt32Array(constructSharedArrayBuffer(4));
5354

5455
functionsleep(ms){
5556
// Also filters out NaN, non-number types, including empty strings, but allows bigints

‎lib/internal/worker.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const{
2323
SymbolFor,
2424
TypedArrayPrototypeFill,
2525
Uint32Array,
26-
globalThis: { SharedArrayBuffer },
2726
}=primordials;
2827

2928
constEventEmitter=require('events');
@@ -61,7 +60,10 @@ const{
6160
const{ createMainThreadPort, destroyMainThreadPort }=require('internal/worker/messaging');
6261
const{ deserializeError }=require('internal/error_serdes');
6362
const{ fileURLToPath, isURL, pathToFileURL }=require('internal/url');
64-
const{ kEmptyObject }=require('internal/util');
63+
const{
64+
constructSharedArrayBuffer,
65+
kEmptyObject,
66+
}=require('internal/util');
6567
const{ validateArray, validateString, validateObject, validateNumber }=require('internal/validators');
6668
const{
6769
throwIfBuildingSnapshot,
@@ -105,9 +107,8 @@ let cwdCounter;
105107

106108
constenvironmentData=newSafeMap();
107109

108-
// SharedArrayBuffers can be disabled with --enable-sharedarraybuffer-per-context.
109-
if(isMainThread&&SharedArrayBuffer!==undefined){
110-
cwdCounter=newUint32Array(newSharedArrayBuffer(4));
110+
if(isMainThread){
111+
cwdCounter=newUint32Array(constructSharedArrayBuffer(4));
111112
constoriginalChdir=process.chdir;
112113
process.chdir=function(path){
113114
AtomicsAdd(cwdCounter,0,1);

‎lib/internal/worker/messaging.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ const{
66
AtomicsWaitAsync,
77
Int32Array,
88
SafeMap,
9-
globalThis,
109
}=primordials;
1110

12-
const{
13-
SharedArrayBuffer,
14-
}=globalThis;
15-
1611
const{
1712
isMainThread,
1813
threadId: currentThreadId,
1914
}=internalBinding('worker');
2015

16+
const{
17+
constructSharedArrayBuffer,
18+
}=require('internal/util');
19+
2120
const{
2221
codes: {
2322
ERR_WORKER_MESSAGING_ERRORED,
@@ -203,7 +202,7 @@ async function postMessageToThread(threadId, value, transferList, timeout){
203202
thrownewERR_WORKER_MESSAGING_SAME_THREAD();
204203
}
205204

206-
constmemory=newSharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);
205+
constmemory=constructSharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);
207206
conststatus=newInt32Array(memory);
208207
constpromise=AtomicsWaitAsync(status,WORKER_MESSAGING_STATUS_INDEX,0,timeout).value;
209208

0 commit comments

Comments
(0)