如何检测Emscripten生成的.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?

查看:89
本文介绍了如何检测Emscripten生成的.js何时完成wasm的加载,以便我可以运行调用它的JS函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Emscripten将一些C代码编译为WebAssembly.这是我的Makefile中的最后一个emcc调用:

I'm using Emscripten to compile some C code to WebAssembly. This is the final emcc call in my Makefile:

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

将按预期工作,并生成.js文件和.wasm文件.JS如下加载到我的HTML页面中:

Which works as intended and generates a .js file and a .wasm file. The JS is loaded into my HTML page as follows:

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

它正确地加载并实例化了WebAssembly代码 iva.wasm .加载页面后不久,该消息就会出现在控制台中:

And it loads and instantiates the WebAssembly code iva.wasm properly. This message appears in the console soon after I load the page:

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

我的意思是说我的WebAssembly是通过fetch()加载的,并且可能要等待一些处理,我才能通过控制台访问我的函数:

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:

Module._init_display_system()

并获取返回值.这是事实,一切正常.

And get the return values. This holds true and everything works.

很显然,我也应该可以通过脚本来完成此操作.但是,我看不到仅在实例化WebAssembly之后运行 函数的方法.我感觉到我缺少明显的东西.

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. I get the feeling that I'm missing something rather obvious.

无论如何,我该怎么做?

Anyway, how do I do this?

推荐答案

使用 Module ['onRuntimeInitialized'] .

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

您曾经使用过emsdk,有像Wasmfiddle这样的在线WASM编译器.找到我的github存储库对这两种方法都有用.

You have used emsdk, there are online WASM compilers like Wasmfiddle. Find my github repo useful for both the methods.

这篇关于如何检测Emscripten生成的.js何时完成wasm的加载,以便我可以运行调用它的JS函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆