v8/test/mjsunit/shared-memory/shared-array-atomics.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

64 lines
1.7 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
'use strict';
(function TestPrimitivesUsingAtomics() {
// All primitives can be stored in fields.
const prims = [42, -0, undefined, null, true, false, 'foo'];
let arr = new SharedArray(1);
for (let prim of prims) {
Atomics.store(arr, 0, prim);
assertEquals(Atomics.load(arr, 0), prim);
}
for (let prim1 of prims) {
for (let prim2 of prims) {
arr[0] = prim1;
assertEquals(Atomics.exchange(arr, 0, prim2), prim1);
assertEquals(arr[0], prim2);
}
}
})();
(function TestObjectsUsingAtomics() {
let arr = new SharedArray(1);
// Shared objects cannot point to non-shared objects.
assertThrows(() => {
Atomics.store(arr, 0, []);
});
assertThrows(() => {
Atomics.store(arr, 0, {});
});
// Shared objects can point to other shared objects.
let shared_rhs = new SharedArray(10);
Atomics.store(arr, 0, shared_rhs);
assertEquals(Atomics.load(arr, 0), shared_rhs);
let Struct = new SharedStructType(['field']);
shared_rhs = new Struct();
Atomics.store(arr, 0, shared_rhs);
assertEquals(Atomics.load(arr, 0), shared_rhs);
let shared_rhs2 = new SharedArray(10);
assertEquals(Atomics.exchange(arr, 0, shared_rhs2), shared_rhs);
assertEquals(arr[0], shared_rhs2);
})();
(function TestOutOfBounds() {
let arr = new SharedArray(1);
// Shared structs are non-extensible.
assertThrows(() => {
Atomics.store(arr, 2, 42);
});
assertThrows(() => {
Atomics.store(arr, -1, 42);
});
assertThrows(() => {
Atomics.store(arr, 'field', 42);
});
})();