javascript - 来自 Javascript 的 WebAssembly
问题描述
我有一个包含 1000 多行的 C++ 程序(使用向量和 stdlib.h)。该程序由一个函数组成,该函数将五个无符号整数(或 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中的具体功能
解决方案
错误信息中清楚地说明了问题和解决方案:您应该使用WebAssembly.instantiate()
函数而不是WebAssembly.Instance()
构造函数。
推荐阅读
- spring-boot - Auth0 - 无法检索远程 JWK 集:读取超时
- database - 如何更改马赛克图的颜色?
- c# - C# 通过不同的列表(“或”)过滤列表
- firebase - Firebase 快照在颤动中映射
- php - 如何删除包含 php 内容的特定 html 标签?
- python - 从字典列表中删除带有空字符串键的字典
- laravel - 所有响应的 Laravel Auth 用户对象
- html - 如何填充包含左侧标题的分区
- python - 在条件中使用 any() 在列表中的元组中查找值
- ubuntu-16.04 - 使用 Tesseract 时出现符号查找错误:未定义符号:_ZN10SIMDDetect14avx_available_E