Skip to content

[dart2wasm/dart:js_interop] Invalid conversions between primitive JS types do not throw #54179

Open
@srujzs

Description

@srujzs
import 'dart:js_interop';

void main() {
  print((0.toJS as JSBoolean).toDart);
  print((0.toJS as JSString).toDart);
  print((true.toJS as JSNumber).toDartDouble);
  print((true.toJS as JSNumber).toDartInt);
  print((true.toJS as JSString).toDart);
  print((''.toJS as JSNumber).toDartDouble);
  print((''.toJS as JSNumber).toDartInt);
  print((''.toJS as JSBoolean).toDart);
}

outputs:

false
0
1.0
1
true
0.0
0
false

on dart2wasm. It throws on the JS backends wherever the casts are due to the reified types of JS types. The conversions are implemented in dart2wasm to use the inline JS<> helper, which simply tries to coerce the underlying extern ref into the Dart type (bool, String, etc.).

Note that if we used external getters with Dart primitive types instead e.g. external bool get y; where y was not a boolean JS value, an error is thrown.

For consistency and less confusion, we should probably throw a TypeError if the value is not the JS type it claims to be when converting. This way, users only pay the cost of type-checking when they try to internalize the JS value.

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.web-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions