blob: 92e3c9de778f3975b4f2becb566bee847433963c [file] [log] [blame] [edit]
// 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.
/// @assertion ByteBuffer get toDart
/// Converts this [JSArrayBuffer] to a [ByteBuffer] by either casting or
/// wrapping it.
///
/// > [!NOTE]
/// > Depending on whether code is compiled to JavaScript or Wasm, this
/// > conversion will have different semantics.
///
/// When compiling to JavaScript, [ByteBuffer]s are [JSArrayBuffer]s and this
/// operation will be a cast.
///
/// When compiling to Wasm, the [JSArrayBuffer] is wrapped with a [ByteBuffer]
/// implementation and the wrapper is returned.
///
/// Modifications to this [JSArrayBuffer] will affect the returned
/// [ByteBuffer] and vice versa.
///
/// @description Check that this getter converts this [JSArrayBuffer] to a
/// [ByteBuffer]. Test [JSArrayBuffer] created in JavaScript.
/// @author sgrekhov22@gmail.com
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'dart:typed_data';
import '../../../Utils/expect.dart';
import '../js_utils.dart';
main() {
eval("globalThis.ab = new ArrayBuffer(2);");
JSArrayBuffer ab = globalContext["ab"] as JSArrayBuffer;
JSInt16Array ar = JSInt16Array(ab);
ar["0"] = 42.toJS;
ByteBuffer bb = ab.toDart;
Expect.equals(42, bb.asInt16List()[0]);
bb.asInt16List()[0] = 1;
Expect.equals(1, (ar["0"] as JSNumber).toDartInt);
}