Skip to content

[ffi/js-interop/wasm] Unified API for using dart:ffi and JS Interop with WASM #46690

Open
@dcharkes

Description

@dcharkes

When developers have native code that lends itself to compilation to WASM, this native code could be used in Dart Web through JS interop.

However, we do not have a unified API for interacting with the same native library on Dart Native through dart:ffi, and on Dart Web through JS interop.

We envision such native libraries to be used in the following way:

Compile to WASM only for Dart Web, and keep using C dynamic libraries for Dart Native:

  1. Dart Web: Dart <-JS interop-> JS <-> WASM
  2. Dart Native: Dart <-dart:ffi-> C (package:ffigen can auto-generate bindings)

First, it would convenient if we would have a code generator that would generate the JS interop bindings for a package compiled to WASM.

Second, it would be convenient if we had some kind of unified API that could both target dart:ffi's types and the generated WASM bindings, if it would be at all possible to have a unified API. The language runtimes of C and Assembly are different, so it might not fully be possible. So this requires exploration.

(An alternative approach would be to also combine the C libraries to WASM for Dart Native:

  1. Dart Web: Dart <-JS interop-> JS <-> WASM
  2. Dart Native: Dart <-package:wasm-> WASM (package:wasm internally does: Dart <-dart:ffi-> C <-Wasmer runtime-> WASM.)

The downsides of this approach is having to ship a WASM runtime such as Wasmer with the Dart Native apps, and it might be slower than just using the native library. The potential upside is that the native library does not have to be cross-compiled.)

Community contributions are welcome.

Filing an issue because we don't have one tracking this yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.library-ffiweb-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