node/deps/v8/test/mjsunit/harmony/uint8-array-to-base64-on-shared-array-buffer.js
Michaël Zasso fff0d1554d deps: update V8 to 13.7.152.9
PR-URL: https://github.com/nodejs/node/pull/58064
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2025-05-18 07:42:43 +00:00

56 lines
1.5 KiB
JavaScript

// Copyright 2025 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --js-base-64 --allow-natives-syntax
const workerScript = `
onmessage = function(event) {
const sab = event.data.buffer;
const uint8Array = new Uint8Array(sab);
const dataToWrite = [102, 111, 111, 98, 97, 114];
for (let i = 0; i < dataToWrite.length; ++i) {
uint8Array[i] = dataToWrite[i];
}
postMessage("started");
while (true) {
for (let i = 0; i < dataToWrite.length; ++i) {
uint8Array[i] = dataToWrite[i];
}
}
};
`;
function testConcurrentSharedArrayBufferUint8ArrayToBase64() {
const sab = new SharedArrayBuffer(6);
const uint8ArrayMain = new Uint8Array(sab);
// Create a worker
const worker = new Worker(workerScript, {type: 'string'});
// Send the SharedArrayBuffer
worker.postMessage({buffer: sab});
assertEquals('started', worker.getMessage());
// Give the worker a little time to write
for (let i = 0; i < 10000; ++i) {
}
// Call toBase64 on the main thread's view of the SAB
for (let i=0; i < 100; i++) {
const base64String = uint8ArrayMain.toBase64();
assertEquals(
'Zm9vYmFy', base64String,
'toBase64 result mismatch with concurrent writes');
}
// Terminate the worker (now it should exit its loop)
worker.terminate();
}
// Run the test function
testConcurrentSharedArrayBufferUint8ArrayToBase64();