Slide 23
Slide 23 text
How to embed Wasm runtime to your App
// Define a host function that prints an i32 value.
let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in
// This function is called from "print_add" in the WebAssembly module.
print(args[0])
return []
}
// Create a runtime importing the host function.
let runtime = Runtime(hostModules: [
"printer": HostModule(functions: ["print_i32": hostPrint])
])
let instance = try runtime.instantiate(module: module)
// Invoke the exported function "print_add"
_ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])