afb26623df
Initial implementation for concurrent shared arrays. Current implementation exposes a `SharedArray` constructor, but its syntax might change in the future. Shared arrays can be shared across Isolates, have a fixed size, have no prototype, have no constructor, and can only store primitives, shared structs and other shared arrays. With this CL shared structs are also allowed to store shared arrays. The Backing storage for the SharedArrays is a `FixedArrayBase`. This CL introdces a new ElementKind: `SHARED_ARRAY_ELEMENTS`. The new kind should match the overall functionality of the `PACKED_SEALED_ELEMENTS` kind, but having it as standalone kind allows for easier branching in CSA and turbofan code. Bug: v8:12547 Change-Id: I054a04624d4cf1f37bc26ae4b92b6fe33408538a Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3585353 Reviewed-by: Tobias Tebbi <tebbi@chromium.org> Reviewed-by: Igor Sheludko <ishell@chromium.org> Commit-Queue: Luis Fernando Pardo Sixtos <lpardosixtos@microsoft.com> Reviewed-by: Shu-yu Guo <syg@chromium.org> Cr-Commit-Position: refs/heads/main@{#81285}
79 lines
2.2 KiB
JavaScript
79 lines
2.2 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 TestNoPrototype() {
|
|
// For now the experimental shared structs don't have a prototype, unlike the
|
|
// proposal explainer which says accessing the prototype throws.
|
|
assertNull(S.prototype);
|
|
assertNull(Object.getPrototypeOf(new S()));
|
|
})();
|
|
|
|
(function TestPrimitives() {
|
|
// All primitives can be stored in fields.
|
|
let s = new S();
|
|
for (let prim of [42, -0, Math.random(),
|
|
undefined, null, true, false,
|
|
"foo"]) {
|
|
s.field = prim;
|
|
assertEquals(s.field, prim);
|
|
}
|
|
})();
|
|
|
|
(function TestObjects() {
|
|
let s = new S();
|
|
// Shared objects cannot point to non-shared objects.
|
|
assertThrows(() => { s.field = []; });
|
|
assertThrows(() => { s.field = {}; });
|
|
// Shared objects can point to other shared objects.
|
|
let shared_rhs = new S();
|
|
s.field = shared_rhs;
|
|
assertEquals(s.field, shared_rhs);
|
|
shared_rhs = new SharedArray(10);
|
|
s.field = shared_rhs;
|
|
assertEquals(s.field, shared_rhs);
|
|
})();
|
|
|
|
(function TestNotExtensible() {
|
|
let s = new S();
|
|
// Shared structs are non-extensible.
|
|
assertThrows(() => { s.nonExistent = 42; });
|
|
assertThrows(() => { Object.setPrototypeOf(s, {}); });
|
|
assertThrows(() => { Object.defineProperty(s, 'nonExistent', { value: 42 }); });
|
|
})();
|
|
|
|
(function TestTooManyFields() {
|
|
let field_names = [];
|
|
for (let i = 0; i < 1000; i++) {
|
|
field_names.push('field' + i);
|
|
}
|
|
assertThrows(() => { new SharedStructType(field_names); });
|
|
})();
|
|
|
|
(function TestOwnPropertyEnumeration() {
|
|
let s = new S();
|
|
s.field = 42;
|
|
|
|
assertArrayEquals(['field'], Reflect.ownKeys(s));
|
|
|
|
let propDescs = Object.getOwnPropertyDescriptors(s);
|
|
let desc = propDescs['field'];
|
|
assertEquals(true, desc.writable);
|
|
assertEquals(false, desc.configurable);
|
|
assertEquals(true, desc.enumerable);
|
|
assertEquals(42, desc.value);
|
|
|
|
let vals = Object.values(s);
|
|
assertArrayEquals([42], vals);
|
|
|
|
let entries = Object.entries(s);
|
|
assertEquals(1, entries.length);
|
|
assertArrayEquals(['field', 42], entries[0]);
|
|
})();
|