简体   繁体   English

如何检测 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?

I'm using Emscripten to compile some C code to WebAssembly.我正在使用 Emscripten 将一些 C 代码编译为 WebAssembly。 This is the final emcc call in my Makefile:这是我的 Makefile 中的最后一个 emcc 调用:

emcc $(CFLAGS) iva.a -o iva.js

Which works as intended and generates a.js file and a.wasm file.它按预期工作并生成 a.js 文件和 a.wasm 文件。 The JS is loaded into my HTML page as follows: JS 被加载到我的 HTML 页面中,如下所示:

<script src="../dist/iva.js">

And it loads and instantiates the WebAssembly code iva.wasm properly.它会正确加载和实例化 WebAssembly 代码iva.wasm This message appears in the console soon after I load the page:加载页面后,此消息很快出现在控制台中:

Fetch finished loading: GET "http://localhost:6931/dist/iva.wasm".

Which I take to mean that my WebAssembly is loaded through a fetch() and, perhaps pending some processing, I can access my functions through the console:我的意思是我的 WebAssembly 是通过 fetch() 加载的,并且可能正在等待一些处理,我可以通过控制台访问我的函数:

Module._init_display_system()

And get the return values.并获取返回值。 This holds true and everything works.这是真的,一切正常。

Clearly, I should be able to do this through a script as well.显然,我也应该能够通过脚本来做到这一点。 However, I can't see a way to only run a function after my WebAssembly has been instantiated.但是,在实例化 WebAssembly之后,我看不到只运行 function 的方法。 I get the feeling that I'm missing something rather obvious.我觉得我错过了一些相当明显的东西。

Anyway, how do I do this?无论如何,我该怎么做?

Use Module['onRuntimeInitialized'] .使用Module['onRuntimeInitialized']

Module['onRuntimeInitialized'] = function() {
       console.log("wasm loaded ");
       var x=Module.ccall("doubleIt","number",["number"],[20]);
       alert(x);
    }

You have used emsdk, there are online WASM compilers like Wasmfiddle.你用过 emsdk,有在线的 WASM 编译器,比如 Wasmfiddle。 Find my github repo useful for both the methods.找到我的 github repo对这两种方法都有用。

While the two solutions of Sudhakar RS and Anil8753 are totally fine, I want to add, that you have to execute this approach before you load your WebAssembly.虽然Sudhakar RSAnil8753的两种解决方案都很好,但我想补充一点,您必须在加载 WebAssembly之前执行此方法。 Otherwise it might be too late to catch the event.否则,赶上事件可能为时已晚。

Besides that, there is another approach, which uses promises.除此之外,还有另一种使用 Promise 的方法。 If you add如果你添加

-s MODULARIZE=1 -s 'EXPORT_NAME="createMyModule"'

as options when compiling with emcc , your resulting .js will contain the function createMyModule .作为使用emcc编译时的选项,生成的.js将包含 function createMyModule This function will return a Promise that resolves as soon as your WebAssembly code is ready to use.这个 function 将返回一个Promise ,它会在您的 WebAssembly 代码准备好使用时立即解析。 Example usage:示例用法:

In your HTML, add your WebAssembly as you already did:在你的 HTML 中,像你已经做的那样添加你的 WebAssembly:

<script src="../dist/iva.js">

In your js, call the function and after the promise resolved, you are good to go:在您的 js 中,调用 function 并在 promise 解决后,您对 go 很好:

createMyModule().then(MyModule => {
  console.log('WebAssembly loaded!');
  // Access your functions (if bound by Embind):
  console.log(MyModule.getSomething());
});
  <script>
    var Module = {
      onRuntimeInitialized: function() {
         console.log('module loaded');
      }
    };
  </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Emscripten:自定义生成的js,使其以自定义方式加载.wasm文件 - Emscripten: Customize the generated js so it loads the .wasm file in a custom way 如何检测iframe何时开始加载以及何时完成加载 - How do I detect when an iframe starts loading and when it finishes loading 如何在后端 js 代码中使用 emscripten 生成的 cpp class? - How can I use emscripten-generated cpp class in backend js code? 如果我推迟JS文件加载以提高Pagespeed,如何调用加载函数? - If I defer JS file loading to improve pagespeed how do i call functions on load? 如何检测 js 脚本中的特定页面以便我可以从函数返回? - How do I detect a specific page in a js script so that I can return from the function? 我需要更改什么才能在 JS 中调用我的函数? - What do I need to change so I can call my function in JS? 如何在emscripten中使用preamble.js文件? - How do I use the preamble.js file in emscripten? 使用asm.js / emscripten / SDL时如何获取所有关键状态? - How do I get all key states when using asm.js/emscripten/SDL? 当我的API用户在JS中调用时,如何识别他们? - How can I identify users of my API when they call it in JS? TweenMax预加载动画完成后无法清除,因此我无法访问我的网站吗? 我该如何解决? - Can’t clear my TweenMax preload animation after it finishes so I can’t access my website? How do I resolve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM