By compiling with the following options,
emcc -s WASM=1 main.cpp -o index.js
emscripten generates an index.wasm
, and a js wrapper index.js
. The js wrapper loads the .wasm
file located in the same directory, using XMLHttpRequest
when it's running in a browser.
So what should I do if I need to load of the .wasm
file manually? The loading part seems hard-coded in the wrapper.
Here are some circumstances where I need this:
.wasm
file from another url (for example from CDN)..wasm
file is dynamically generated.XMLHttpRequest
is not allowed, there is only an API like getMyData('some_file')
. I know I could generate the .wasm
file only, by
emcc -s WASM=1 main.cpp -o index.wasm
But then I loses the wrapper, which is not trivial when using some emscripten APIs like SDL and GLES.
AFIK, the JS wrapper calls fetch() and not XHR. If available, you could use a Service Worker to intercept that request and return whatever you'd like.
You could also modify the Emscripten source (I have v1.39.13) to rewrite the default loading behavior. In file $EMSDK/upstream/emscripten/src/preamble.js
, modify functions getBinaryPromise()
and getBinary()
to your liking, then build with emcc
to have your custom loading behavior in the WASM JS wrapper.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.