0

我正在使用“导入”内存在 js 和 wasm 之间共享一块内存。在我的汇编脚本代码中,我什至没有访问预分配的内存并且仍然得到一个RuntimeError: memory access out of bounds. 我想了解为什么。

js:

const [width, height] = [100, 100];
const arraySize = width * height;
const pageSize = 64 * 1024;
const nPages = Math.ceil(arraySize / pageSize);
const memory = new WebAssembly.Memory({ 
  initial: nPages 
});
WebAssembly
  .instantiateStreaming(fetch('/build/optimized.wasm'), {
    env: {
      memory,
      abort: (_msg, _file, line, column) => {
        console.error(`Abort at ${line}:${column}`)
      },
    }
  })
  .then(({instance}) => {
    const bytes = new Uint8ClampedArray(memory.buffer);
    
    // A
    for (let i = 0; i < arraySize; i++) {
      bytes[i] = 1;
    }

    instance.exports.asdf(width, height);
  });

汇编脚本:

export function asdf(width: i32, height: i32): void {
  // B
  const arr = new Uint8Array(width * height);
}

当我删除任何一个AB它工作。


编辑:

奇怪:设置const [width, height] = [39, 39];也不会产生错误。


编辑2:

我正在使用导入的内存,因为我找到的所有示例都是这样做的。我是否应该按照它们在此处显示的方式创建数组等?https://www.assemblyscript.org/loader.html#creating-arrays

4

1 回答 1

0

我没有看到任何错误。看游乐场

另外请记住,当您在 AssemblyScript 中分配内存时,您可能会根据您的示例在主机端覆盖初始化内存。

于 2021-02-14T12:46:01.443 回答