Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting JSBoolean to bool fails in dart2js #55024

Closed
simolus3 opened this issue Feb 26, 2024 · 3 comments
Closed

Converting JSBoolean to bool fails in dart2js #55024

simolus3 opened this issue Feb 26, 2024 · 3 comments
Assignees
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-js-interop Issues that impact all js interop

Comments

@simolus3
Copy link
Contributor

Consider this simple snippet:

import 'dart:js_interop';

void main() {
  print(globalContext.equals(globalContext).toDart);
}

As .equals is declared to return a JSBoolean, it needs to be converted to Dart with toDart. dart2js seems to disagree on that though, and pretends equals returns a bool directly:

> ~/bin/dart-sdk/dev/bin/dart compile js tool/repro.dart 
tool/repro.dart:4:45:
Error: The getter 'toDart' isn't defined for the class 'bool'.
  print(globalContext.equals(globalContext).toDart);
                                            ^^^^^^
Error: Compilation failed.

Happens on 3.3.0 stable and I've also tested this on 3.4.0-160.0.dev. Casting to bool works, but all the documentation very strongly discourages that.

@sigmundch sigmundch added web-js-interop Issues that impact all js interop area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. labels Feb 27, 2024
@sigmundch
Copy link
Member

cc @srujzs

I can repro locally, but oddly it doesn't cause an issue in dartpad, not sure why.

What's strange is that the public API is declared to return JSBoolean in dart:js_interop, but to return a bool on the patch-files (both js_shared and wasm).

@sigmundch
Copy link
Member

(thanks @simolus3 for the nice short repro, BTW)

@srujzs srujzs self-assigned this Feb 27, 2024
@srujzs
Copy link
Contributor

srujzs commented Feb 27, 2024

Thanks for filing!

Yeah, it looks like the patch files use bool but the publicly-visible API uses JSBoolean. The tests in https://dart-review.googlesource.com/c/sdk/+/341140 must have sidestepped this issue on all the web compilers. I'm guessing the usage of identical in package:expect might have made this unintentionally work on dart2wasm, but not sure. This affects several other relational operators too like lessThan.

I'll upload a change. Not sure if this would be considered a breaking change, but we can discuss in the CL.

I can repro locally, but oddly it doesn't cause an issue in dartpad, not sure why.

I'm seeing a separate error in dartpad in the console:

Uncaught TypeError: Cannot read properties of null (reading 'postMessage')

copybara-service bot pushed a commit that referenced this issue Mar 25, 2024
Related issues:
#55024
#55267

These operators were initially broken in 3.3 and were exposed
as returning JSBoolean but implemented as returning bool. They
were fixed to return bool in the public API, but we should
prefer to have them return JS types as they're likely to be used
in cases where implicit conversions are not useful.

CoreLibraryReviewExempt: Fixing type mismatch in backend-specific library.
Change-Id: I3b0e60550dcac78918f8399d11238dcfa34982cd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/359180
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
simolus3 added a commit to simolus3/sqlite3.dart that referenced this issue Jun 15, 2024
Dart 3.3 had the problem of some JS-interop methods declared to return
a `JSBoolean` actually returning a Dart `bool`, causing compilation
errors.
Now that dart-lang/sdk#55024 has been fixed
in Dart 3.4, the workarounds introduced to fix the issue can be
reverted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-js-interop Issues that impact all js interop
Projects
Status: Done
Development

No branches or pull requests

3 participants