I am creating a WebAssembly instance and trying to provide my own WebAssembly.Memory, but it always ignores the value I provide and creates its own memory instance instead.
According to MDN, the syntax should be:
WebAssembly.instantiateStreaming(fetch('memory.wasm'), { js: { mem: memory } })
However, when I do the same:
const memory = new WebAssembly.Memory({ initial: 1, maximum: 1 });
const { instance } = await WebAssembly.instantiateStreaming(
fetch('main.wasm'),
{ js: { mem: memory } },
);
console.log('did use same memory?', instance.exports.memory.buffer === memory.buffer);
I see the answer is false (and the size of the memory it uses is 16777216 bytes rather than 65536, i.e. 256 pages instead of 1). This happens in both Chrome and FireFox, so does not seem to be an implementation issue.
This does not appear to be connected to anything in the main.wasm file, but as a test I have reduced it to the following minimal source (compiled with emscripten):
#include <emscripten/emscripten.h>
static int counter = 1;
EMSCRIPTEN_KEEPALIVE int inc() {
counter++;
return counter;
}
emcc -O3 main.c -o main.wasm --no-entry
(you can see the full MCVE here: GitHub / live site)
I have tried various variations (such as {js: {memory}}, {env: {memory}}, {memory}, etc.) but it always seems to behave as if I did not set any memory configuration. What do I need to do to make it use my memory config?