I'm sharing a piece of memory between js and wasm, using 'imported' memory. in my assemblyscript code I'm not even accessing the preallocated memory and still get a RuntimeError: memory access out of bounds
. I would like to understand why.
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);
});
assemblyscript:
export function asdf(width: i32, height: i32): void {
// B
const arr = new Uint8Array(width * height);
}
when I remove either A
or B
it works.
edit:
weird: setting const [width, height] = [39, 39];
also produces no error.
edit 2:
I'm using imported memory because all the examples I found do that. should I perhaps be creating arrays, etc. the way they show here? https://www.assemblyscript.org/loader.html#creating-arrays
I don't see any errors. See playground
Also this keep in mind when you allocate memory in AssemblyScript you may overwrite initialized memory before on the host side according your example.