WebAssembly.Memory
Create a memory block that can be imported into a WASM module.
Any changes made to the memory in JavaScript is also seen in the WASM module.
Also, any changes made to the memory inside the WASM module are seen in JavaScript.
The same memory is used both by JavaScript and the WASM module.
It is possible to shared the same memory block with multiple WASM modules.
Constructor
const memory = new WebAssembly.Memory(options);
Parameters
options
Declares information about the memory being created.
options.initial
The starting size of the memory. The size is in pages, with each page being 64Kbs in size (64 * 1024 bytes).
options.maximum Optional
The maximum number of pages the memory can grow up to. This is the total number of pages the memory can have.
options.shared Optional
Can the memory object be shared with more than one WASM module?
Example
(import "import" "memory" (memory 1 shared))
const memory = new WebAssembly.Memory({ initial: 1, shared: true });
const options = {
import: {
memory: memory
}
};
const wasm1 = await WebAssembly.instantiateStreaming(fetch('app.wasm'), options);
const wasm2 = await WebAssembly.instantiateStreaming(fetch('app.wasm'), options);
const wasm3 = await WebAssembly.instantiateStreaming(fetch('app.wasm'), options);
let uint8ArrayMemory = new Uint8Array(memory.buffer);
uint8ArrayMemory[0] = 0x01;
uint8ArrayMemory[1] = 0x02;
uint8ArrayMemory[2] = 0x03;
const m1 = wasm1.instance.exports.readMemory(0);
const m2 = wasm2.instance.exports.readMemory(1);
const m3 = wasm3.instance.exports.readMemory(2);