【发布时间】:2021-05-16 22:03:09
【问题描述】:
我在 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
【问题讨论】:
-
您是否为
asc使用了--importMemory标志? -
是的,我确实这样做了。
标签: webassembly assemblyscript