Skip to content

Difference in side effects between dart2js and dart2wasm #59651

@koji-1009

Description

@koji-1009

When the following tests are run on dart2js and dart2wasm, the results are different.
It passes in dart2js, but does not pass in dart2wasm.

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:web/web.dart' as web;
import 'package:test/test.dart';

void main() {
  test('calculate', () {
    final array = Uint8List(16);
    expect(
      array.any((e) => e == 0),
      isTrue,
    );

    web.window.crypto.getRandomValues(array.toJS);

    expect(
      array.any((e) => e != 0),
      isTrue,
    );
  });
}

Result

# koji @ MacBookPro in ~/workspace/wasm_array on git:main x [23:57:55] C:1
$ dart test -p chrome -c dart2js
Building package executable...
Built test:test.
00:04 +0: [Chrome, Dart2Js] loading test/wasm_array_test.dart
Compiled 14,098,335 input bytes (9,096,737 characters source) to 1,128,319 characters JavaScript in 4.18 seconds using 0.000 MB of memory

00:05 +1: All tests passed!

# koji @ MacBookPro in ~/workspace/wasm_array on git:main x [23:58:20]
$ dart test -p chrome -c dart2wasm
Building package executable...
Built test:test.
00:02 +0: [Chrome, Dart2Wasm] loading test/wasm_array_test.dart
Generated wasm module '/private/var/folders/hr/y37hfwp553b_1bc41k7s8nqm0000gn/T/dart_test_8t0zPW/test_PD9QTD/wasm_array_test.dart.browser_test.dart.wasm', and JS init file '/private/var/folders/hr/y37hfwp553b_1bc41k7s8nqm0000gn/T/dart_test_8t0zPW/test_PD9QTD/wasm_array_test.dart.browser_test.dart.mjs'.

00:02 +0 -1: [Chrome, Dart2Wasm] test/wasm_array_test.dart: calculate [E]
  Expected: true
    Actual: <false>

  wasm_array_test.dart.browser_test.dart.wasm 1:273327  fail
  wasm_array_test.dart.browser_test.dart.wasm 1:272587  _expect
  wasm_array_test.dart.browser_test.dart.wasm 1:272122  expect
  wasm_array_test.dart.browser_test.dart.wasm 1:271795  main closure at file:///Users/koji/workspace/wasm_array/test/wasm_array_test.dart:8:21
  wasm_array_test.dart.browser_test.dart.wasm 1:271842  closure wrapper at file:///Users/koji/workspace/wasm_array/test/wasm_array_test.dart:8:21 trampoline
  wasm_array_test.dart.browser_test.dart.wasm 1:342564  Declarer.test closure at file:///Users/koji/.pub-cache/hosted/pub.dev/test_api-0.7.4/lib/src/backend/declarer.dart:227:22 inner
  wasm_array_test.dart.browser_test.dart.wasm 1:346584  _awaitHelper closure at org-dartlang-sdk:///lib/_internal/wasm/lib/async_patch.dart:83:16


To run this test again: /Users/koji/flutter/bin/cache/dart-sdk/bin/dart test test/wasm_array_test.dart -p chrome --plain-name 'calculate'
00:02 +0 -1: Some tests failed.

Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'dart test --chain-stack-traces'.

https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues

Return value

The same array passed as typedArray but with its contents replaced with the newly generated random numbers. Note that typedArray is modified in-place, and no copy is made.


If Uint8List and JSUint8List are processed separately, the behavior is consistent between dart2js and dart2wasm.

import 'dart:js_interop';
import 'dart:typed_data';

import 'package:web/web.dart' as web;
import 'package:test/test.dart';

void main() {
  test('calculate', () {
    final array = Uint8List(16);
    expect(
      array.any((e) => e == 0),
      isTrue,
    );

    final values = array.toJS;
    web.window.crypto.getRandomValues(values);
    final result = values.toDart;

    expect(
      result.any((e) => e != 0),
      isTrue,
    );
  });
}

Version

$ dart --version
Dart SDK version: 3.5.4 (stable) (Wed Oct 16 16:18:51 2024 +0000) on "macos_arm64"

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart2wasmIssues for the dart2wasm compiler.area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.closed-as-intendedClosed as the reported issue is expected behaviorweb-dart2jsweb-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions