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 memory
(import "import" "memory" (memory 1 shared))
// Create shared memory
const memory = new WebAssembly.Memory({ initial: 1, shared: true });

// Create options object
const options = {
  import: {
    memory: memory
  }
};

// Create instances of WASM with options
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);

// Create arrays of memory
let uint8ArrayMemory = new Uint8Array(memory.buffer);

// Set memory
uint8ArrayMemory[0] = 0x01;
uint8ArrayMemory[1] = 0x02;
uint8ArrayMemory[2] = 0x03;

// Call WASM function to read data from memory
const m1 = wasm1.instance.exports.readMemory(0); // m1 = 0x01
const m2 = wasm2.instance.exports.readMemory(1); // m2 = 0x02
const m3 = wasm3.instance.exports.readMemory(2); // m3 = 0x03