我有一个包含向量代码行(使用1000+和stdlib.h)的C ++程序。该程序由一个函数组成,该函数接受5个无符号整型(或1个无符号字符和4个无符号整型)作为输入,返回一个字符串或4个无符号整型(我不知道如何返回数字数组,所以我使用字符串)。我使用WasmExplorer将程序编译成.wasm文件。
如何从javascript中调用.wasm文件中的函数,得到结果?我试过了:
let squarer;
function loadWebAssembly (fileName) {
return fetch (fileName)
.then (response => response.arrayBuffer ())
.then (bits => WebAssembly.compile (bits))
.then (module => {return new WebAssembly.Instance (module)});
};
loadWebAssembly ('http://test.ru/squarer.wasm')
.then (instance => {
squarer = instance.exports._Z7squareri;
console.log ('Finished compiling! Ready when you are ...');
});Chrome出错(我有29Kb的.wasm文件)
Uncaught (in promise) RangeError: WebAssembly.Instance is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.instantiate.如何从JS调用函数(举个例子)?
Chrome中wasm/wasm-000197c6/wasm-000197c6-22中的特定函数
发布于 2019-10-30 08:54:00
错误消息中清楚地说明了问题和解决方案:您应该使用WebAssembly.instantiate()函数而不是WebAssembly.Instance()构造函数。
https://stackoverflow.com/questions/58580672
复制相似问题