Skip to content

Expose JSUnsafeDartObject in dart:js_interop_unsafe #55187

Closed
@srujzs

Description

@srujzs

Externalizing some internal discussion on this topic already:

Currently, the way to pass Dart objects to JavaScript involves JSBoxedDartObject. Its conversion method, toJSBox does:

  • On the JS backends, we create a JS object and then set a Dart runtime-specific symbol whose value is the Dart object.
  • On dart2wasm, we do something very similar except the Dart object is converted to an externref first.

While this offers a consistent interface on the JavaScript side and avoids mixing Dart objects between runtimes, it does come with downsides:

  1. It's slow, especially if passing Dart objects to JS is a common operation.
  2. It creates a new box everytime the Dart object is converted, requiring users to be careful about identity.
  3. Users aren't meant to actually access the Dart value in the box.

Instead, we can provide a better type here which doesn't provide any consistency or runtime safety, but addresses the above downsides. As it's not a JS value, it shouldn't be a subtype of JSAny and our error checker should allowlist this value to flow into JS.

Related issues:

#55183
#55114
#54925

Metadata

Metadata

Assignees

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

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions