首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自Javascript的Webassembly

来自Javascript的Webassembly
EN

Stack Overflow用户
提问于 2019-10-27 23:16:57
回答 1查看 278关注 0票数 0

我有一个包含向量代码行(使用1000+和stdlib.h)的C ++程序。该程序由一个函数组成,该函数接受5个无符号整型(或1个无符号字符和4个无符号整型)作为输入,返回一个字符串或4个无符号整型(我不知道如何返回数字数组,所以我使用字符串)。我使用WasmExplorer将程序编译成.wasm文件。

如何从javascript中调用.wasm文件中的函数,得到结果?我试过了:

代码语言:javascript
复制
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文件)

代码语言:javascript
复制
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中的特定函数

EN

回答 1

Stack Overflow用户

发布于 2019-10-30 08:54:00

错误消息中清楚地说明了问题和解决方案:您应该使用WebAssembly.instantiate()函数而不是WebAssembly.Instance()构造函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58580672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档