11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { MessageChannel, Worker } = require('worker_threads');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test that SharedArrayBuffer instances created from WASM are transferrable
71cb0ef41Sopenharmony_ci// through MessageChannels (without crashing).
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
101cb0ef41Sopenharmony_ciconst wasmSource = fixtures.readSync('shared-memory.wasm');
111cb0ef41Sopenharmony_ciconst wasmModule = new WebAssembly.Module(wasmSource);
121cb0ef41Sopenharmony_ciconst instance = new WebAssembly.Instance(wasmModule);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst { buffer } = instance.exports.memory;
151cb0ef41Sopenharmony_ciassert(buffer instanceof SharedArrayBuffer);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci{
181cb0ef41Sopenharmony_ci  const { port1, port2 } = new MessageChannel();
191cb0ef41Sopenharmony_ci  port1.postMessage(buffer);
201cb0ef41Sopenharmony_ci  port2.once('message', common.mustCall((buffer2) => {
211cb0ef41Sopenharmony_ci    // Make sure serialized + deserialized buffer refer to the same memory.
221cb0ef41Sopenharmony_ci    const expected = 'Hello, world!';
231cb0ef41Sopenharmony_ci    const bytes = Buffer.from(buffer).write(expected);
241cb0ef41Sopenharmony_ci    const deserialized = Buffer.from(buffer2).toString('utf8', 0, bytes);
251cb0ef41Sopenharmony_ci    assert.deepStrictEqual(deserialized, expected);
261cb0ef41Sopenharmony_ci  }));
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci{
301cb0ef41Sopenharmony_ci  // Make sure we can free WASM memory originating from a thread that already
311cb0ef41Sopenharmony_ci  // stopped when we exit.
321cb0ef41Sopenharmony_ci  const worker = new Worker(`
331cb0ef41Sopenharmony_ci  const { parentPort } = require('worker_threads');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  // Compile the same WASM module from its source bytes.
361cb0ef41Sopenharmony_ci  const wasmSource = new Uint8Array([${wasmSource.join(',')}]);
371cb0ef41Sopenharmony_ci  const wasmModule = new WebAssembly.Module(wasmSource);
381cb0ef41Sopenharmony_ci  const instance = new WebAssembly.Instance(wasmModule);
391cb0ef41Sopenharmony_ci  parentPort.postMessage(instance.exports.memory);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  // Do the same thing, except we receive the WASM module via transfer.
421cb0ef41Sopenharmony_ci  parentPort.once('message', ({ wasmModule }) => {
431cb0ef41Sopenharmony_ci    const instance = new WebAssembly.Instance(wasmModule);
441cb0ef41Sopenharmony_ci    parentPort.postMessage(instance.exports.memory);
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci  `, { eval: true });
471cb0ef41Sopenharmony_ci  worker.on('message', common.mustCall(({ buffer }) => {
481cb0ef41Sopenharmony_ci    assert(buffer instanceof SharedArrayBuffer);
491cb0ef41Sopenharmony_ci    worker.buf = buffer; // Basically just keep the reference to buffer alive.
501cb0ef41Sopenharmony_ci  }, 2));
511cb0ef41Sopenharmony_ci  worker.once('exit', common.mustCall());
521cb0ef41Sopenharmony_ci  worker.postMessage({ wasmModule });
531cb0ef41Sopenharmony_ci}
54