RnMss
RnMss

Reputation: 3863

Emscripten: Customize the generated js so it loads the .wasm file in a custom way

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:

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.

Upvotes: 5

Views: 1750

Answers (1)

anthumchris
anthumchris

Reputation: 9090

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.

Upvotes: 2

Related Questions