我正在使用“导入”内存在 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);
}
当我删除任何一个A
或B
它工作。
编辑:
奇怪:设置const [width, height] = [39, 39];
也不会产生错误。
编辑2:
我正在使用导入的内存,因为我找到的所有示例都是这样做的。我是否应该按照它们在此处显示的方式创建数组等?https://www.assemblyscript.org/loader.html#creating-arrays