简体   繁体   English

Emscripten:自定义生成的js,使其以自定义方式加载.wasm文件

[英]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:以下是我需要这个的一些情况:

  • I need to serve the .wasm file from another url (for example from CDN).我需要从另一个 url(例如来自 CDN)提供.wasm文件。
  • The url to download the .wasm file is dynamically generated.下载.wasm文件的url是动态生成的。
  • The code is running in a sandbox where XMLHttpRequest is not allowed, there is only an API like getMyData('some_file') .该代码在不允许XMLHttpRequest的沙箱中运行,只有 API 像getMyData('some_file')
  • I need to embed all the files as base64 string.我需要将所有文件嵌入为 base64 字符串。

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.

相关问题 如何检测 Emscripten 的 generated.js 何时完成加载 wasm,以便我可以运行调用它的 JS 函数? - How do I detect when Emscripten's generated .js finishes loading the wasm so I can run my JS functions which call it? 将文件名作为参数传递给 Emscripten 生成的 js - Passing in a file name to Emscripten generated js as an argument 拆分emscripten生成的文件 - Split emscripten generated file 如何使用 Emscripten 和 Embind 发出的 .js 和 .wasm 工件? - How to use the .js and .wasm artifacts emitted by Emscripten and Embind? 将JS函数传递给Emscripten生成的代码 - Passing JS function to Emscripten-generated code glfw的基本设置会导致在emscripten生成的js文件中与eventlistener有关的运行时错误 - Basic setup for glfw causes a runtime error with regards to eventlistener in emscripten generated js file 我需要在wordpress网站的页脚中更改脚本的顺序,以便我的自定义main.js文件加载所有脚本的最后一个 - I need to change order of scripts in footer of my wordpress site so my custom main.js file loads last of all scripts js导入找不到wasm文件 - js import not finding wasm file jQuery加载到资产管道之前,自定义js文件加载 - custom js file loading before jquery loads in asset pipeline emscripten 编译的 wasm 的未知错误编号“5508328” - Unknown error num `5508328` of emscripten compiled wasm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM