Memories
In WebAssembly, a memory is a resizable ArrayBuffer
or SharedArrayBuffer
. In other terms, an ArrayBuffer
represents a binary data buffer, or byte array. A SharedArrayBuffer
is the same thing as an ArrayBuffer
, but not transferable between contexts, and set up in a way where it can be used for views in shared memory, unlike an ArrayBuffer
.
Memories can be both imported into, or exported from, a module (see the Imports section of our documentation for an example of passing imports, like a table, into a module).
Examples
Creating a memory can look like this:
const aMemory = new WebAssembly.Memory({
initial: 10, // starting, and minimum number of pages
maximum: 100, // maximum number of pages
});
const initial = 10; // starting, and minimum number of pages
const maximum = Some(100); // maximum number of pages, as Option<u32>
let memory_type = MemoryType::new(initial, maximum); // setting memory type
let memory = Memory::new(&mut store, memory_type)?; // using memory constructor
Accessing an exported memory looks something like this:
WebAssembly.instantiateStreaming(fetch("module.wasm")).then((obj) => {
const memoryContents = new Uint32Array(obj.instance.exports.memory.buffer); // setting the memory contents to a variable
const firstValue = values[0]; // setting the contents from the first position in the memory to a variable
});
let engine = Engine::default();
let mut store = Store::new(&engine, ());
let module = Module::from_file(&engine, "module.wasm")?;
let memory = instance
.get_memory(&mut store, "memory")
.ok_or(anyhow::format_err!("failed to find `memory` export"))?;
const first_value = memory.data(&store)[0];
Additional reading
You can read more about memory
in Mozilla’s Wasm documentation here, read more about ArrayBuffers
here and SharedArrayBuffers
here.