lib: replace global SharedArrayBuffer constructor with bound method

PR-URL: https://github.com/nodejs/node/pull/60497
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Renegade334
2025-10-30 15:19:30 +00:00
committed by Node.js GitHub Bot
parent 48d0cd7fdc
commit 21b0fe4b28
5 changed files with 32 additions and 40 deletions

View File

@@ -11,9 +11,6 @@ const {
ObjectDefineProperty,
PromisePrototypeThen,
RegExpPrototypeExec,
globalThis: {
SharedArrayBuffer,
},
} = primordials;
const {
@@ -119,23 +116,21 @@ port.on('message', (message) => {
require('internal/worker').assignEnvironmentData(environmentData);
setupMainThreadPort(mainThreadPort);
if (SharedArrayBuffer !== undefined) {
// The counter is only passed to the workers created by the main thread,
// not to workers created by other workers.
let cachedCwd = '';
let lastCounter = -1;
const originalCwd = process.cwd;
// The counter is only passed to the workers created by the main thread,
// not to workers created by other workers.
let cachedCwd = '';
let lastCounter = -1;
const originalCwd = process.cwd;
process.cwd = function() {
const currentCounter = AtomicsLoad(cwdCounter, 0);
if (currentCounter === lastCounter)
return cachedCwd;
lastCounter = currentCounter;
cachedCwd = originalCwd();
process.cwd = function() {
const currentCounter = AtomicsLoad(cwdCounter, 0);
if (currentCounter === lastCounter)
return cachedCwd;
};
workerIo.sharedCwdCounter = cwdCounter;
}
lastCounter = currentCounter;
cachedCwd = originalCwd();
return cachedCwd;
};
workerIo.sharedCwdCounter = cwdCounter;
const isLoaderHookWorker = (filename === 'internal/modules/esm/worker' && doEval === 'internal');
if (!isLoaderHookWorker) {

View File

@@ -15,13 +15,8 @@ const {
SafeSet,
StringPrototypeSlice,
StringPrototypeToUpperCase,
globalThis,
} = primordials;
const {
SharedArrayBuffer,
} = globalThis;
const {
ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED,
ERR_INTERNAL_ASSERTION,
@@ -44,6 +39,7 @@ const {
validateString,
} = require('internal/validators');
const {
constructSharedArrayBuffer,
kEmptyObject,
} = require('internal/util');
@@ -535,7 +531,7 @@ class AsyncLoaderHookWorker {
const { InternalWorker } = require('internal/worker');
MessageChannel ??= require('internal/worker/io').MessageChannel;
const lock = new SharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
const lock = constructSharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
this.#lock = new Int32Array(lock);
this.#worker = new InternalWorker('internal/modules/esm/worker', {

View File

@@ -9,13 +9,14 @@ const {
AtomicsWait,
Int32Array,
MathMax,
Number,
SymbolDispose,
globalThis: {
Number,
SharedArrayBuffer,
},
} = primordials;
const {
constructSharedArrayBuffer,
} = require('internal/util');
const {
Buffer,
} = require('buffer');
@@ -49,7 +50,7 @@ const {
const BUSY_WRITE_TIMEOUT = 100;
const kEmptyBuffer = Buffer.allocUnsafe(0);
const kNil = new Int32Array(new SharedArrayBuffer(4));
const kNil = new Int32Array(constructSharedArrayBuffer(4));
function sleep(ms) {
// Also filters out NaN, non-number types, including empty strings, but allows bigints

View File

@@ -24,7 +24,6 @@ const {
SymbolFor,
TypedArrayPrototypeFill,
Uint32Array,
globalThis: { SharedArrayBuffer },
} = primordials;
const EventEmitter = require('events');
@@ -62,7 +61,10 @@ const {
const { createMainThreadPort, destroyMainThreadPort } = require('internal/worker/messaging');
const { deserializeError } = require('internal/error_serdes');
const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');
const { kEmptyObject } = require('internal/util');
const {
constructSharedArrayBuffer,
kEmptyObject,
} = require('internal/util');
const { validateArray, validateString, validateObject, validateNumber } = require('internal/validators');
const {
throwIfBuildingSnapshot,
@@ -106,9 +108,8 @@ let cwdCounter;
const environmentData = new SafeMap();
// SharedArrayBuffers can be disabled with --enable-sharedarraybuffer-per-context.
if (isMainThread && SharedArrayBuffer !== undefined) {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
if (isMainThread) {
cwdCounter = new Uint32Array(constructSharedArrayBuffer(4));
const originalChdir = process.chdir;
process.chdir = function(path) {
AtomicsAdd(cwdCounter, 0, 1);

View File

@@ -6,18 +6,17 @@ const {
AtomicsWaitAsync,
Int32Array,
SafeMap,
globalThis,
} = primordials;
const {
SharedArrayBuffer,
} = globalThis;
const {
isMainThread,
threadId: currentThreadId,
} = internalBinding('worker');
const {
constructSharedArrayBuffer,
} = require('internal/util');
const {
codes: {
ERR_WORKER_MESSAGING_ERRORED,
@@ -203,7 +202,7 @@ async function postMessageToThread(threadId, value, transferList, timeout) {
throw new ERR_WORKER_MESSAGING_SAME_THREAD();
}
const memory = new SharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);
const memory = constructSharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);
const status = new Int32Array(memory);
const promise = AtomicsWaitAsync(status, WORKER_MESSAGING_STATUS_INDEX, 0, timeout).value;