Files
sdk/tests/lib/html/shared_array_buffer_test.dart
Srujan Gaddam 67bda7c22b [dart2js/ddc/dart2wasm/dart:js_interop] Support SharedArrayBuffers in JS typed data wrappers
https://github.com/dart-lang/sdk/issues/56455

The existing native typed data implementation in dart2js/ddc
and the JS typed data wrappers in dart2wasm do not support
SharedArrayBuffers.

In dart2js/ddc, this is because the native type for ByteBuffer
is simply ArrayBuffer, leading to type failures when using
SharedArrayBuffers. To handle this, this change makes NativeByteBuffer
an abstract parent class to NativeArrayBuffer and NativeSharedArrayBuffer.
This allows ByteBuffer to support both types. There is a preexisting
SharedArrayBuffer type in dart:html that we should avoid breaking, so
we add an interface that NativeSharedArrayBuffer implements and expose
that interface.

In dart2wasm, JSArrayBufferImpl only allows ArrayBuffers as its
extern ref. This change makes that wrapper support SharedArrayBuffers
as well.

In dart:js_interop, the existing toJS conversion on ByteBuffer
now throws if the underlying buffer was actually a SharedArrayBuffer.
This is to support the return type of JSArrayBuffer. This behavior
technically already existed due to type differences in the JS
compilers, but was never possible with dart2wasm.

CoreLibraryReviewExempt: Backend-specific libraries with no real functional changes to public APIs.
Change-Id: I4dac9fb808590bf0c274da815c152cd4637316b1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437526
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Srujan Gaddam <srujzs@google.com>
2025-07-07 12:50:25 -07:00

51 lines
1.5 KiB
Dart

// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Test the `SharedArrayBuffer` interface exposed through `dart:html` and make
// sure it's well-typed.
import 'dart:html';
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:expect/expect.dart';
@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(f) => f;
@JS('SharedArrayBuffer')
external JSAny? get _sharedArrayBufferConstructor;
bool supportsSharedArrayBuffer = _sharedArrayBufferConstructor != null;
void main() {
// TODO(https://github.com/dart-lang/sdk/issues/61043): Support this in the
// test runner.
if (!supportsSharedArrayBuffer) return;
final buf = SharedArrayBuffer(3);
Expect.equals(3, buf.byteLength);
final bufNoArgs = SharedArrayBuffer();
Expect.equals(0, bufNoArgs.byteLength);
final slice1 = buf.slice();
Expect.equals(3, slice1.byteLength);
final slice2 = buf.slice(1);
Expect.equals(2, slice2.byteLength);
final slice3 = buf.slice(1, 2);
Expect.equals(1, slice3.byteLength);
Expect.isTrue(buf is SharedArrayBuffer);
buf as SharedArrayBuffer;
Expect.isTrue(confuse(buf) is SharedArrayBuffer);
confuse(buf) as SharedArrayBuffer;
// This should be true in order to allow typed lists to contain
// `SharedArrayBuffer`s.
Expect.isTrue(buf is ByteBuffer);
buf as ByteBuffer;
Expect.isTrue(confuse(buf) is ByteBuffer);
confuse(buf) as ByteBuffer;
}