0

尝试创建一个大小为从 JS 导入的变量的数组似乎不起作用。例如:

汇编脚本:

export declare arrSize: u32

const arr = new Uint32Array(arrSize)

export function init (): void {
  unchecked(testArr[0]) = 1
  store<u32>(0, unchecked(testArr[0]))
}

JS:

const memory = new WebAssembly.Memory({ initial: 1 })
WebAssembly.instantiateStreaming(fetch('module.wasm'), {
  env: { memory },
  index: { arrSize: 2 }
}).then(module => {
  const { init } = module.instance.exports
  init()
  const arr = new Uint32Array(memory.buffer, 0, 2)
  console.log(arr)
})

而如果我const arr = new Uint32Array(2)在 AssemblyScript 文件中进行更改,它就可以工作。

有没有办法在 WebAssembly 模块中动态调整大小的本地数组?

4

1 回答 1

1

这有效:

export declare const count: i32;

const arr = new Array<i32>(count);
export function getCount(): i32 {
    return arr.length;
}

在您的导入对象中WebAssembly.instantiate应该有:

{
    index: {
        count: 22
    }
}

前提是您的汇编脚本文件名为index.ts

于 2021-01-13T19:02:26.713 回答