webassembly.instantiate 与模块

webassembly.instantiate versus Module

我有一个 imagePV.c 文件:

unsigned char *imageGrayscale(...) {...} . 

然后我完成了:$emcc -o glue.js imagePV.c ....

在 index.html 中我输入:

    <script src="glue.js"></script> . 
    <script> 
        var imageGrayscale= Module.cwrap('imageGrayscale',...);
        Module.HEAPU8.set(...);
        imageGrayscale(...);
    </script>

一切正常。所以我的问题是:实例化需要什么?我的意思是,需要什么:

WebAssembly.instantiateStreaming(fetch('glue.wasm'), importObject).then(obj => obj.instance.exports.exported_func());

我是初学者

简短回答:您的 Module 已经在幕后使用 WebAssembly.instantiateStreaming()

长答案:Emscripten (emcc) 不仅是一个编译器,还是一个完整的工具链基础设施。当您使用 emcc 命令时,emcc 调用 clang 编译器将您的 C 代码编译为 WASM,使用 imports, and generates a boilerplate JS code (it's called preamble.js) 链接必要的 JS 代码以在 WASM 中使用以实例化 WASM Module 的代码,以便开发人员无需编写自己的代码来编写引导代码

查看您的 glue.js 代码。你可以自己找a line that uses WebAssembly.instantiateStreaming(). Unless you use emcc without -s SIDE_MODULE=1 option, it always generates preamble.js code for you. So normally you don't need to use WebAssembly

一些高级用户不想要这个大样板并以更难的方式使用 WASM,比如在没有 emcc 的情况下使用 clang。 Then they need to use WebAssembly including instantiateStreaming() themselves.