v8/test/mjsunit/shared-memory/shared-array-atomics-workers.js
Luis Fernando Pardo Sixtos 6151ae0127 [shared-struct] Support shared arrays in Atomics.{load,store,exchange}
Atomics.load, Atomics.store, and Atomics.exchange now accept shared
array objects as their 1st argument.

Currently these are implemented in C++ and not yet in CSA.

Bug: v8:12547
Change-Id: I54ed8816a696a4f45dda964739b1cfd917d39dc0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3723974
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Commit-Queue: Luis Fernando Pardo Sixtos <lpardosixtos@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#81537}
2022-07-05 15:49:01 +00:00

43 lines
1.4 KiB
JavaScript

// Copyright 2022 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: --shared-string-table --harmony-struct --allow-natives-syntax
'use strict';
if (this.Worker) {
(function TestSharedArrayPostMessage() {
let workerScript = `onmessage = function(arr) {
// Non-atomic write that will be made visible once main thread
// observes the atomic write below.
arr[0][0] = 42;
arr[1].payload = 84;
Atomics.store(arr, 2, "worker");
};
postMessage("started");`;
let worker = new Worker(workerScript, {type: 'string'});
let started = worker.getMessage();
assertEquals('started', started);
let OuterArray = new SharedArray(3);
let Struct = new SharedStructType(['payload']);
OuterArray[0] = new SharedArray(1);
OuterArray[1] = new Struct();
OuterArray[2] = 'main';
assertEquals(undefined, OuterArray[0][0]);
assertEquals(undefined, OuterArray[1].payload);
assertEquals('main', OuterArray[2]);
worker.postMessage(OuterArray);
// Spin until we observe the worker's write on index 2.
while (Atomics.load(OuterArray, 2) !== 'worker') {
}
// The non-atomic store write must also be visible.
assertEquals(42, OuterArray[0][0]);
assertEquals(84, OuterArray[1].payload);
worker.terminate();
})();
}