blob: c461437c37bc8b16b58d1a46784884a15fd9bc8c [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 Float64List get toDart
/// Converts this [JSFloat64Array] to a [Float64List] 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, [Float64List]s are [JSFloat64Array]s and
/// this getter will be a cast.
///
/// When compiling to Wasm, the [JSFloat64Array] is wrapped with a
/// [Float64List] implementation and the wrapper is returned.
///
/// Modifications to this [JSFloat64Array] will affect the returned
/// [Float64List] and vice versa.
///
/// @description Check that this getter converts this [JSFloat64Array] to a
/// [Float64List] and modifications to [JSFloat64Array] affect the [Float64List]
/// and vice versa. Test [JSFloat64Array] created in Dart.
/// @author sgrekhov22@gmail.com
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'dart:typed_data';
import '../../../Utils/expect.dart';
main() {
ByteBuffer buffer = Float64List.fromList([3.14]).buffer;
JSFloat64Array a = JSFloat64Array(buffer.toJS);
Float64List l = a.toDart;
Expect.listApproxEquals([3.14], l.toList());
a["0"] = (-3.14).toJS;
Expect.listApproxEquals([-3.14], l.toList());
l[0] = 1.1;
Expect.listApproxEquals([1.1], a.toDart.toList());
}