wasm icon indicating copy to clipboard operation
wasm copied to clipboard

WIP: iOS support

Open liamappelbe opened this issue 3 years ago • 2 comments

(This doesn't work at all yet)

Most of the files are boilerplate created by flutter. The only relevant files are:

  • flutter_wasm/ios/flutter_wasm.podspec
  • wasm/bin/setup.dart
  • flutter_wasm/bin/ios_setup.dart

liamappelbe avatar Jan 25 '22 23:01 liamappelbe

I looked at this briefly. iOS is a bit different than Android because they don't have JIT. Wasmer has support for it though, you just have to do AOT compilation.

Here's what needs to change:

  1. The wasm module has to be put into its own file so it can be compiled
  2. There needs to be a command-line step that compiles the wasm module to a dylib for the appropriate target. Since this requires cross-compilation I have to believe using the rust toolchain is easiest. This step would be added in the plugin podspec like Liam was doing. There are 2 ways to compile the dylib:
    1. Use wasmer command-line tool (ex https://crates.io/crates/wasmer-cli). Once that is installed the wasm module can be compiled with: cargo wasmer compile ./tests/integration/ios/DylibExample/DylibExample/sum.wasm --dylib --target aarch64-apple-ios -o foo.dylib.
    2. Here is an example of how to compile it programmatically: https://github.com/wasmerio/wasmer/blob/master/examples/platform_ios_headless.rs
  3. The code for initializing the module has to be tweaked a bit for loading from a dylib. Where the current example uses wasm_module_new, the function wasm_module_deserialize must be used (which isn't currently surfaced to Dart). An example of that can be found here: https://github.com/wasmerio/wasmer/blob/61b72164a46cc2a6ac86ca7264a3207daaa84200/tests/integration/ios/DylibExample/DylibExample/calc.cpp#L64 (beware that example/test is difficult to get running and requires a bit of setup with Rust)

gaaclarke avatar Oct 13 '22 23:10 gaaclarke

@gaaclarke Sorry for the slow reply, I missed your message. Thanks for taking a look.

Why would I need to compile the wasm code to a dylib to use the AOT backend? If I'm loading it via wasm_module_deserialize, doesn't that mean I just need to produce the file via wasm_module_serialize? If I compile the wasm to a dylib, wouldn't I need to load it using FFI or something?

The issue I'm having at the moment is just that I can't figure out how to get the package's native code to link into the app. I think there's something missing in flutter_wasm/ios/flutter_wasm.podspec, but I don't know enough about the iOS build system to figure it out. The way this problem manifests is that when I try to run the app, FFI simply fails to find any of the native wasmer functions. If someone can help me with the build magic, and get this PR to the point where FFI loads the wasmer functions correctly, I can figure out the rest.

liamappelbe avatar Nov 02 '22 01:11 liamappelbe