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.11k stars 1.57k forks source link

interop & wasm: reconcile import and export mechanisms #55733

Open sigmundch opened 4 months ago

sigmundch commented 4 months ago

In wasm you can use JS-interop or wasm:* pragmas to import and export symbols. This can be a source of inconsistencies and confusion (e.g. see https://github.com/dart-lang/sdk/issues/55715).

Some questions:

cc @srujzs @mkustermann @osa1

srujzs commented 4 months ago

I think wasm:import and wasm:export should both be locked down to the SDK via a check. IIRC, the former is to call out to JS functions defined in the instance from Wasm and the latter is to call dart2wasm functions from JS. Both mechanisms rely on unique names so having users be able to use this will likely lead to collisions somewhere. It's also a bit wonky for users to even be able to expose the JS function properly in the case of wasm:import and the Wasm function in the case of wasm:export. JS interop does what users need here anyways.

It looks like skwasm uses these mechanisms already, so we can allowlist that code. Should we lock down any of the other wasm: pragmas?

I'm also not sure what the plan is for dart:ffi for Wasm to Wasm interop and if this is something that's needed for that.

mkustermann commented 4 months ago

Yes, I think for now this should be internal.

From a conceptual view, there should be two/three different mechanisms

osa1 commented 4 months ago

I agree, Wasm interop pragmas should be internal.

mkustermann commented 4 months ago

@osa1 Could you change dart2wasm to only recognize @pragma('wasm:import') & @pragma('wasm:export') for dart:* libraries? (I would assume any usage in flutter engine will be inside dart:* libraries as well)

osa1 commented 4 months ago

We discussed this a bit today, we also want to limit @Native usage to Flutter engine and SDK, as it was never intended as a general way to interact with any Wasm module. We will have a different mechanism for that.

With this we can also reopen #37355 until we have this new mechanism to interact with other Wasm modules.