-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
dart2wasm fails with typed FFI classes due to tree-shaking #53910
Comments
It may be fair to say (@osa1 / @askeksa correct me if wrong) that dart2wasm doesn't officially support FFI yet (only a subset of the API is working - as it's used by flutter web internals). There's also the issue of multi-memory wasm proposal hasn't been finalized & implemented yet. That being said, some of the errors may not be intentional and could be fixed. |
EDIT: This most likely boils down to a lack of WASM support in the package or an incomplete migration to WASM (see https://dart.dev/interop/js-interop/package-web#conditional-imports) When trying to compile an existing Flutter application, I've run into this issue 🤔 Packages in our project that depended on ffi that break when using WASM:
So having support for FFI when running WASM would be important it seems 🤔 |
Hmm okay from the package ecosystem perspective there might be work needed: e.g. for export 'src/device_info_plus_linux.dart'
if (dart.library.html) 'src/device_info_plus_web.dart';
export 'src/device_info_plus_windows.dart'
if (dart.library.html) 'src/device_info_plus_web.dart'; Though the export does not work on WASM as I understand as the So I guess the code should be changed to something like: export 'src/device_info_plus_linux.dart'
if (dart.library.js_interop) 'src/device_info_plus_web.dart';
export 'src/device_info_plus_windows.dart'
if (dart.library.js_interop) 'src/device_info_plus_web.dart'; Ahh yes, the documentation has been updated to include this change 🙌 |
We don't (officially) support |
…agmas in user code - Disallow importing `dart:ffi` in user code. - Disallow `wasm:import` and `wasm:export` pragmas in user code. Bug: #53910 Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/369040 Cherry-pick-request: #55890 Change-Id: Ifaaeb4f2c4b13bd5f564ffbf5281d6439ac6dd56 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369040 Reviewed-by: Alexander Thomas <athom@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Martin Kustermann <kustermann@google.com>
dart2wasm throws various errors when compiling
dart:ffi
types, I'm guessing due to aggressive tree-shaking removing generic type data when those types are not explicitly instantiated.All of the below are run with
dart --enable-asserts pkg/dart2wasm/bin/dart2wasm.dart <file> /tmp/out.wasm
Files can be found @ https://github.com/nmfisher/dart2wasm_ffi_examples if that's easier.
fails with
Constant InstanceConstant(const _FfiAbiSpecificMapping{_FfiAbiSpecificMapping.nativeTypes: const <Type?>[Uint8, Uint8, Int8, Int8, Uint8, Uint8, Int8, Uint8, Int8, Int8, Int8, Uint8, Uint8, Int8, Int8, Uint8, Uint8, Int8, Int8, Int8, Int8, Int8]}) references field _FfiAbiSpecificMapping.nativeTypes which is not retained
.Workaround is to explicitly declare/instantiate an equivalent type:
This compiles successfully.
fails with
Constant InstanceConstant(const _FfiStructLayout{_FfiStructLayout.fieldTypes: const <Type>[Int32, Float], _FfiStructLayout.packing: null}) references field _FfiStructLayout.fieldTypes which is not retained
.This specific issue can be solved by explicitly accessing the
ref
, e.g.However, that then fails with a different exception
Exception in ConstructorInvocation at file:///Users/nickfisher/Documents/dart2wasm_ffi_examples/struct_fix1.dart:4:8 Unhandled exception: Bad state: No element
(presumably it's looking for a single-argument constructor on the Dart class
MyStruct
to pass the address, but that doesn't exist, hence why it fails).fails with:
If we change
ptr
tofinal
, it compiles successfully:dart info
)General info
Process info
macOS
N/A
The text was updated successfully, but these errors were encountered: