Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test workerData with SharedArrayBuffer #21180

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ if (isMainThread) {
* `options` {Object}
* `eval` {boolean} If true, interpret the first argument to the constructor
as a script that is executed once the worker is online.
* `data` {any} Any JavaScript value that will be cloned and made
* `workerData` {any} Any JavaScript value that will be cloned and made
available as [`require('worker_threads').workerData`][]. The cloning will
occur as described in the [HTML structured clone algorithm][], and an error
will be thrown if the object cannot be cloned (e.g. because it contains
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-worker-workerdata-sharedarraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Flags: --expose-gc --experimental-worker
'use strict';

const common = require('../common');
const assert = require('assert');
const { Worker } = require('worker_threads');

{
const sharedArrayBuffer = new SharedArrayBuffer(12);
const local = Buffer.from(sharedArrayBuffer);

const w = new Worker(`
const { parentPort, workerData } = require('worker_threads');
const local = Buffer.from(workerData.sharedArrayBuffer);

parentPort.on('message', () => {
local.write('world!', 6);
parentPort.postMessage('written!');
});
`, {
eval: true,
workerData: { sharedArrayBuffer }
});
w.on('message', common.mustCall(() => {
assert.strictEqual(local.toString(), 'Hello world!');
global.gc();
w.terminate();
}));
w.postMessage({});
// This would be a race condition if the memory regions were overlapping
local.write('Hello ');
}