来自Javascript的Webassembly
我有一个包含1000多行代码的C++程序(使用vector和stdlib.h)。该程序由一个函数组成,该函数以5个无符号整数(或1个无符号字符和4个无符号整数)作为输入,返回一个字符串或4个无符号整数(我不知道如何返回数字数组,所以我使用字符串)。 我使用WasmExplorer将程序编译成一个.wasm文件 如何从javascript调用.wasm文件中的函数,获得结果? 我试过:来自Javascript的Webassembly,javascript,c++,webassembly,Javascript,C++,Webassembly,我有一个包含1000多行代码的C++程序(使用vector和stdlib.h)。该程序由一个函数组成,该函数以5个无符号整数(或1个无符号字符和4个无符号整数)作为输入,返回一个字符串或4个无符号整数(我不知道如何返回数字数组,所以我使用字符串)。 我使用WasmExplorer将程序编译成一个.wasm文件 如何从javascript调用.wasm文件中的函数,获得结果? 我试过: let平方器; 函数loadWebAssembly(文件名){ 返回获取(文件名) .then(response
let平方器;
函数loadWebAssembly(文件名){
返回获取(文件名)
.then(response=>response.arrayBuffer())
.然后(bits=>WebAssembly.compile(bits))
.then(module=>{returnnewwebassembly.Instance(module)});
};
loadWebAssembly('http://test.ru/squarer.wasm')
。然后(实例=>{
squarer=instance.exports.Z7squareri;
console.log('已完成编译!准备就绪时…');
});
Chrome错误(我有29Kb.wasm文件)
如何从JS调用函数(附示例)
>在WASM/WASM—00197C6/WASM—00197-C22中,在错误信息中明确地指出了问题和解决方案:使用<代码> WebSalpps.实例()/Case>函数代替Uncaught (in promise) RangeError: WebAssembly.Instance is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.instantiate.