The memory.fill memory instruction sets all bytes in a memory region to a given byte.
This is analogous to memset in C.
(module
(memory $my_mem (export "memory") 1)
(func (export "fill")
i32.const 0 ;; memory offset
i32.const 42 ;; value to fill
i32.const 6 ;; number of bytes to fill
memory.fill $my_mem
)
)WebAssembly.instantiateStreaming(fetch("{%wasm-url%}")).then((result) => {
result.instance.exports.fill();
const memBuffer = result.instance.exports.memory.buffer;
const memArray = new Uint8Array(memBuffer, 0, 6);
console.log(memArray[5]);
});In the above example, we define an exported memory called $my_mem with a maximum size of one page. We then include an exported fill() function that fills the first six bytes of $my_mem with the value 42.
In the JavaScript, we call the fill() function to populate the exported memory with data, put the exported memory's contents into an array, and log the sixth array element to the console.
memory.fill dest_memory
memory.fillThe memory.fill instruction type. Must always be included first.
dest_memory OptionalThe identifier for the memory you want to fill. This can be one of the following:
nameAn identifying name set for the memory when it was first defined. This must begin with a $ symbol, for example $my_mem.
indexThe memory's index number, for example 0 for the first memory in the wasm module, 1 for the second, etc.
If dest_memory is omitted, it defaults to 0.
[dest_offset value length] -> []
dest_offsetAn integer representing the offset at which to start filling the memory. This will be an i32 or an i64, to match the address_type the memory was defined with.
valueAn i32 representing the value to fill the selected bytes with. The value is truncated to 8 bits so it can be applied to each byte.
lengthAn i32 or an i64 representing the number of bytes to fill with the value. This will match the address_type the memory was defined with.
If the indicated memory region is out of bounds, the instruction traps.
| Instruction | Binary format | Example text => binary |
|---|---|---|
memory.fill | 0xfc 11:u32 x:memidx | memory.fill 0 => 0xfc 0x0b 0x00 |
;; Fill region at offset/range in default memory with 255
i32.const 200 ;; The pointer to the region to update
i32.const 255 ;; The value to set each byte to (must be < 256)
i32.const 100 ;; The number of bytes to update
memory.fill ;; Fill default memory
;; Fill default memory using an S-expression
(memory.fill (i32.const 200) (i32.const 255) (i32.const 100));; Fill specific memory referenced by its index
i32.const 200 ;; The pointer to the region to update
i32.const 255 ;; The value to set each byte to (must be < 256)
i32.const 100 ;; The number of bytes to update
memory.fill (memory 1) ;; Fill memory with index 1
;; Fill memory referenced by its name
i32.const 200 ;; The pointer to the region to update
i32.const 255 ;; The value to set each byte to (must be < 256)
i32.const 100 ;; The number of bytes to update
memory.fill (memory $memoryName) ;; Fill memory with name "$memoryName"
;; Fill same memory using an S-expression
(memory.fill (memory $memoryName) (i32.const 200) (i32.const 255) (i32.const 100))memory definition