[英]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
. emscripten 生成一个
index.wasm
和一个 js 包装器index.js
。 The js wrapper loads the .wasm
file located in the same directory, using XMLHttpRequest
when it's running in a browser. js 包装器在浏览器中运行时使用
XMLHttpRequest
加载位于同一目录中的.wasm
文件。
So what should I do if I need to load of the .wasm
file manually?那么如果我需要手动加载
.wasm
文件该怎么办呢? 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
文件。.wasm
file is dynamically generated..wasm
文件的url是动态生成的。XMLHttpRequest
is not allowed, there is only an API like getMyData('some_file')
.XMLHttpRequest
的沙箱中运行,只有 API 像getMyData('some_file')
。 I know I could generate the .wasm
file only, by我知道我只能生成
.wasm
文件,通过
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.但是随后我丢失了包装器,这在使用 SDL 和 GLES 等一些 emscripten API 时并非易事。
AFIK, the JS wrapper calls fetch() and not XHR. AFIK,JS 包装器调用 fetch() 而不是 XHR。 If available, you could use a Service Worker to intercept that request and return whatever you'd like.
如果可用,您可以使用 Service Worker 拦截该请求并返回您想要的任何内容。
You could also modify the Emscripten source (I have v1.39.13) to rewrite the default loading behavior.您还可以修改 Emscripten 源代码(我有 v1.39.13)以重写默认加载行为。 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.在文件
$EMSDK/upstream/emscripten/src/preamble.js
中,根据自己的喜好修改函数getBinaryPromise()
和getBinary()
,然后使用emcc
构建以在 WASM JS 包装器中自定义加载行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.