Skip to content

Commit

Permalink
repl: fix crash when SharedArrayBuffer disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Sep 2, 2021
1 parent 13b569c commit 7c3ed50
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions benchmark/worker/atomics-wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const bench = common.createBenchmark(main, {
});

function main({ n }) {
if (typeof SharedArrayBuffer === 'undefined') {
throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
}

const i32arr = new Int32Array(new SharedArrayBuffer(4));
bench.start();
for (let i = 0; i < n; i++)
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
ArrayPrototypeSplice,
ObjectDefineProperty,
PromisePrototypeCatch,
globalThis: { Atomics },
globalThis: { Atomics, SharedArrayBuffer },
} = primordials;

const {
Expand Down Expand Up @@ -142,6 +142,9 @@ port.on('message', (message) => {
const originalCwd = process.cwd;

process.cwd = function() {
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
if (typeof SharedArrayBuffer === 'undefined') return originalCwd();

const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
return cachedCwd;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ let cwdCounter;

const environmentData = new SafeMap();

if (isMainThread) {
// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
if (isMainThread && typeof SharedArrayBuffer !== 'undefined') {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
const originalChdir = process.chdir;
process.chdir = function(path) {
Expand Down

0 comments on commit 7c3ed50

Please # to comment.