请问 webassembly 返回数组在 JS 中应如何获取 ?

例如:

float* test () {
    static float data[3];
    data[0] = 0.0;
    data[1] = 1.0;
    data[2] = 2.0;
    return data;
}
// 获取 wasm
function loadWebAssembly(filename, imports) {
  // Fetch the file and compile it
  return fetch(filename)
    .then(response => response.arrayBuffer())
    .then(buffer => WebAssembly.compile(buffer))
    .then(module => {
      // Create the imports for the module, including the
      // standard dynamic library imports
      imports = imports || {}
      imports.env = imports.env || {}
      imports.env.__memory_base = imports.env.__memory_base || 0
      imports.env.__table_base = imports.env.__table_base || 0
      if (!imports.env.memory) {
        imports.env.memory = new WebAssembly.Memory({ initial: 256 })
      }
      if (!imports.env.table) {
        imports.env.table = new WebAssembly.Table({ initial: 0, element: 'anyfunc' })
      }
      // Create the instance.
      return new WebAssembly.Instance(module, imports)
    });
}

const instance = await loadWebAssembly('test.wasm')
console.log(instance.exports._test())

转为 wasm 之后,请问在 js 交互中,应如何获取返回的数组?

阅读 4.1k
1 个回答
新手上路,请多包涵

同问,这个返回的一个数字好像是结果在内存上的一个偏移量,但是怎么取我也很惆怅

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进