◐ Shell
clean mode source ↗

lib: replace global SharedArrayBuffer constructor with bound method · nodejs/node@219d2e9

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-

let cachedCwd = '';

126-

let lastCounter = -1;

127-

const originalCwd = process.cwd;

128-
129-

process.cwd = function() {

130-

const currentCounter = AtomicsLoad(cwdCounter, 0);

131-

if (currentCounter === lastCounter)

132-

return cachedCwd;

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+

let cachedCwd = '';

122+

let lastCounter = -1;

123+

const originalCwd = process.cwd;

124+
125+

process.cwd = function() {

126+

const currentCounter = AtomicsLoad(cwdCounter, 0);

127+

if (currentCounter === lastCounter)

135128

return cachedCwd;

136-

};

137-

workerIo.sharedCwdCounter = cwdCounter;

138-

}

129+

lastCounter = currentCounter;

130+

cachedCwd = originalCwd();

131+

return cachedCwd;

132+

};

133+

workerIo.sharedCwdCounter = cwdCounter;

139134
140135

const isLoaderHookWorker = (filename === 'internal/modules/esm/worker' && doEval === 'internal');

141136

if (!isLoaderHookWorker) {

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-

const lock = new SharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);

534+

const lock = constructSharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);

539535

this.#lock = new Int32Array(lock);

540536
541537

this.#worker = new InternalWorker('internal/modules/esm/worker', {

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

const BUSY_WRITE_TIMEOUT = 100;

5051

const kEmptyBuffer = Buffer.allocUnsafe(0);

5152
52-

const kNil = new Int32Array(new SharedArrayBuffer(4));

53+

const kNil = new Int32Array(constructSharedArrayBuffer(4));

5354
5455

function sleep(ms) {

5556

// Also filters out NaN, non-number types, including empty strings, but allows bigints

Original file line numberDiff line numberDiff line change

@@ -24,7 +24,6 @@ const {

2424

SymbolFor,

2525

TypedArrayPrototypeFill,

2626

Uint32Array,

27-

globalThis: { SharedArrayBuffer },

2827

} = primordials;

2928
3029

const EventEmitter = require('events');

@@ -62,7 +61,10 @@ const {

6261

const { createMainThreadPort, destroyMainThreadPort } = require('internal/worker/messaging');

6362

const { deserializeError } = require('internal/error_serdes');

6463

const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');

65-

const { kEmptyObject } = require('internal/util');

64+

const {

65+

constructSharedArrayBuffer,

66+

kEmptyObject,

67+

} = require('internal/util');

6668

const { validateArray, validateString, validateObject, validateNumber } = require('internal/validators');

6769

const {

6870

throwIfBuildingSnapshot,

@@ -106,9 +108,8 @@ let cwdCounter;

106108
107109

const environmentData = new SafeMap();

108110
109-

// SharedArrayBuffers can be disabled with --enable-sharedarraybuffer-per-context.

110-

if (isMainThread && SharedArrayBuffer !== undefined) {

111-

cwdCounter = new Uint32Array(new SharedArrayBuffer(4));

111+

if (isMainThread) {

112+

cwdCounter = new Uint32Array(constructSharedArrayBuffer(4));

112113

const originalChdir = process.chdir;

113114

process.chdir = function(path) {

114115

AtomicsAdd(cwdCounter, 0, 1);

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

throw new ERR_WORKER_MESSAGING_SAME_THREAD();

204203

}

205204
206-

const memory = new SharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);

205+

const memory = constructSharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);

207206

const status = new Int32Array(memory);

208207

const promise = AtomicsWaitAsync(status, WORKER_MESSAGING_STATUS_INDEX, 0, timeout).value;

209208