WebAssembly memory is a simple, resizable ArrayBuffer containing raw bytes. A module and its host VM share this memory, while each module receives its own allocation and pointer context. Because WebAssembly has no string or character primitives, values crossing the boundary must be represented as bytes. section 10.6
A host application and module must agree on a low-level exchange sequence:
- Allocate space in module memory. section 10.6
- Retrieve the allocated location and write input bytes into it. section 10.6
- Invoke the module function using the agreed memory location. section 10.6
- Read the result bytes from the returned memory location. section 10.6
Passing a search term through memory
An exported Rust allocator creates a Vec with capacity, returns its pointer, and prevents the buffer from being dropped. A memory_search function then reads a C string from that pointer, converts the bytes into a String, performs the search, converts the first result back into bytes, pads it to 1024 bytes, and returns a C-compatible pointer. The host VM obtains the module's memory and allocate function before calling the search function. section 10.6 section 10.6
Figure 10.6 Wasm memory
Use the memory diagram to trace how the VM and module share an allocation while retaining separate module execution contexts. section 10.6
Figure 10.8 Wasm reading and writing to the memory buffer
Use the buffer diagram to follow input writing and result reading across the host-module boundary. section 10.6
