v8/test/mjsunit/shared-memory/shared-struct-atomics.js
Shu-yu Guo 67044edf08 [shared-struct] Support shared structs in Atomics.exchange
Bug: v8:12547
Change-Id: Ie27831b793f214368a003adac24b7c92f1a5fc11
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3518426
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79504}
2022-03-17 05:40:36 +00:00

50 lines
1.5 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";
let S = new SharedStructType(['field']);
(function TestPrimitivesUsingAtomics() {
// All primitives can be stored in fields.
let s = new S();
const prims = [42, -0, undefined, null, true, false, "foo"];
for (let prim of prims) {
Atomics.store(s, 'field', prim);
assertEquals(Atomics.load(s, 'field'), prim);
}
for (let prim1 of prims) {
for (let prim2 of prims) {
s.field = prim1;
assertEquals(Atomics.exchange(s, 'field', prim2), prim1);
assertEquals(s.field, prim2);
}
}
})();
(function TestObjectsUsingAtomics() {
let s = new S();
// Shared objects cannot point to non-shared objects.
assertThrows(() => { Atomics.store(s, 'field', []); });
assertThrows(() => { Atomics.store(s, 'field', {}); });
// Shared objects can point to other shared objects.
let shared_rhs = new S();
Atomics.store(s, 'field', shared_rhs);
assertEquals(Atomics.load(s, 'field'), shared_rhs);
let shared_rhs2 = new S();
assertEquals(Atomics.exchange(s, 'field', shared_rhs2), shared_rhs);
assertEquals(s.field, shared_rhs2);
})();
(function TestNotExtensibleUsingAtomics() {
let s = new S();
// Shared structs are non-extensible.
assertThrows(() => { Atomics.store(s, 'nonExistent', 42); });
})();