11cb0ef41Sopenharmony_ci// Flags: --debug-arraybuffer-allocations
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/28777
81cb0ef41Sopenharmony_ci// Make sure that SharedArrayBuffers and transferred ArrayBuffers created in
91cb0ef41Sopenharmony_ci// Worker threads are accessible after the creating thread ended.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifor (const ctor of ['ArrayBuffer', 'SharedArrayBuffer']) {
121cb0ef41Sopenharmony_ci  const w = new Worker(`
131cb0ef41Sopenharmony_ci  const { parentPort } = require('worker_threads');
141cb0ef41Sopenharmony_ci  const arrayBuffer = new ${ctor}(4);
151cb0ef41Sopenharmony_ci  parentPort.postMessage(
161cb0ef41Sopenharmony_ci    arrayBuffer,
171cb0ef41Sopenharmony_ci    '${ctor}' === 'SharedArrayBuffer' ? [] : [arrayBuffer]);
181cb0ef41Sopenharmony_ci  `, { eval: true });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  let arrayBuffer;
211cb0ef41Sopenharmony_ci  w.once('message', common.mustCall((message) => arrayBuffer = message));
221cb0ef41Sopenharmony_ci  w.once('exit', common.mustCall(() => {
231cb0ef41Sopenharmony_ci    assert.strictEqual(arrayBuffer.constructor.name, ctor);
241cb0ef41Sopenharmony_ci    const uint8array = new Uint8Array(arrayBuffer);
251cb0ef41Sopenharmony_ci    uint8array[0] = 42;
261cb0ef41Sopenharmony_ci    assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0]));
271cb0ef41Sopenharmony_ci  }));
281cb0ef41Sopenharmony_ci}
29