Demonstrates how to call Wasmer through Rust from a Swift Multiplatform app (iOS, MacOS).
If a wasm program is ran on iOS and uses some memory in some capacity, an error will happen:
Failed to create memory: Error when allocating memory: Cannot allocate memory (os error 12)
Example of issues opened related to this: wasmerio/wasmer#4343
To circumvent this issue, you need to tune the memory (style or size) of the Wasmer VM. Three examples are available in this repository:
- A default Store with a default Memory -> Default Memory (it will crash)
- A Static Memory Style (implementing BaseTunables directly) -> Static Limited Memory
- A Dynamic Memory Style (composing Tunables to override and force the MemoryStyle) -> Default Memory
In my tests, using a limited static memory style was more performant than a dynamic style. It might depend on your program and needs.
- Preferrably an ARM-based Mac (M1/2/3) - Rust can also be cross-compiled on x86 with cargo/cross
- XCode and XCode tools (run the command
xcode-select --install
) - RustUp and Cross
- Execute
compileRustToXCFramework.sh
. It will compile theRustWasmer
package to 3 targets (iOS, iOS Simulator, MacOS) and create an XCFramework which contains each of these static libraries, as well as the headers necessary to bridge to Swift directly - Clean the project every time you execute
compileRustToXCFramework.sh
(in XCode: Product > Clean Build Folder) - Run the target you want to test on (one from
MacOS
,iOS
,iOS App Clip
) - If selecting
iOS
andiOS App Clip
, you can choose between running on an emulator, or on a real iPhone (the later needs a developer provisioning profile) - Note: A real iPhone is needed to reproduce the issues we've seen with memory usage/reference (working fine on simulators or MacOS).
Note: Code-signing might fail if you don't have an Apple Developer's provisioning profile setup on your machine. You can change each of the target's signature config by clicking on each target and selecting Signing & Capabilities
> Signing Certificate
> Sign to run locally
.