When I tried to use wasm from Swift, I ran into a strange phenomenon.
I get an error when fetching wasm of localfile in WKWebView.
Unhandled Promise Rejection: TypeError: Unexpected response MIME type. Expected 'application/wasm'
It's puzzling because you can read wasm with XMLHttpRequest.
Overwrite fetch with XMLHttpRequest.
async function fetch(filename) {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', filename);
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load', (e) => resolve(xhr.response));
xhr.send();
});
const response = new Response(await promise.then(), {headers: new Headers([['Content-Type', 'application/wasm']])});
return response;
}
https://wasmbyexample.dev/examples/hello-world/hello-world.assemblyscript.en-us.html
// install
$ brew install wasmer
$ npm install -g assemblyscript
// edit
$ vi hello-world.ts
export function add(a: i32, b: i32): i32 {
return a + b;
}
// compile
$ asc hello-world.ts -b hello-world.wasm
// execution
$ wasmer hello-world.wasm -i add 1 2
3
- Mac 12.2.1(21D62)
- Xcode Version 13.2.1 (13C100)
There is good news. This way Pyodide worked on iOS.
Thank.