dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

`dart compile js` is optimising away a variable store to `globalThis` #56977

Closed domesticmouse closed 3 weeks ago

domesticmouse commented 3 weeks ago

I'm attempting to install a Dart function into the global JS namespace using the new dart:js_interop tooling, but I'm hitting an error condition.

The following code fails to work as intended.

import 'dart:js_interop';

int calculateImpl(int a, int b) {
  print('Multiplied $a by $b');
  return a * b;
}

@JS()
external JSFunction get calculate;

@JS()
external set calculate(JSFunction f);

void main(List<String> arguments) {
  calculate = calculateImpl.toJS;
}

However, the following code works as intended.

import 'dart:js_interop';

int calculateImpl(int a, int b) {
  print('Multiplied $a by $b');
  return a * b;
}

@JS()
external JSFunction get calculate;

@JS()
external set calculate(JSFunction f);

void main(List<String> arguments) {
  calculate = calculateImpl.toJS;
  print(calculate);
}

Note: the only change between the two code samples is the call to print(calculate);.

I'm guessing there is an over eager optimizer pass killing the variable store to calculate because said variable is never read from for the rest of the Dart main function.

The compilation command I am using is:

$ dart compile js --no-source-maps bin/main.dart -o web/main.js

And, the obligatory Flutter Doctor output:

$ flutter doctor -v
[✓] Flutter (Channel stable, 3.24.4, on macOS 14.7 23H124 darwin-arm64, locale en)
    • Flutter version 3.24.4 on channel stable at /Users/brettmorgan/flutter
    • Upstream repository https://github.com/flutter/flutter
    • Framework revision 603104015d (4 days ago), 2024-10-24 08:01:25 -0700
    • Engine revision db49896cf2
    • Dart version 3.5.4
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/brettmorgan/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] IntelliJ IDEA Community Edition (version 2024.2.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 82.0.3
    • Dart plugin version 242.22855.32

[✓] VS Code (version 1.94.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.98.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.7 23H124 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.7 23H124 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 130.0.6723.70

[✓] Network resources
    • All expected network resources are available.

• No issues found!
dart-github-bot commented 3 weeks ago

Summary: The user is attempting to install a Dart function into the global JS namespace using dart:js_interop, but the function is being optimized away by the Dart compiler. The issue occurs when the function is not used after being assigned to the global variable, leading to the compiler removing the assignment.

srujzs commented 3 weeks ago

Duplicate of https://github.com/dart-lang/sdk/issues/56533

This should be fixed in 3.6.

domesticmouse commented 3 weeks ago

Duplicate of #56533

This should be fixed in 3.6.

Confirmed as working on main