fill: Wasm memory instruction
The memory.fill memory instruction sets all bytes in a memory region to a given byte.
This is analogous to memset in C.
Try it
(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.
Syntax
memory.fill dest_memory
memory.fill-
The
memory.fillinstruction type. Must always be included first. dest_memoryOptional-
The identifier for the
memoryyou want to fill. This can be one of the following:name-
An identifying name set for the
memorywhen it was first defined. This must begin with a$symbol, for example$my_mem. index-
The
memory's index number, for example0for the firstmemoryin the wasm module,1for the second, etc.
If
dest_memoryis omitted, it defaults to0.
Type
[dest_offset value length] -> []
dest_offset-
An integer representing the offset at which to start filling the memory. This will be an
i32or ani64, to match theaddress_typethememorywas defined with. value-
An
i32representing the value to fill the selected bytes with. Thevalueis truncated to 8 bits so it can be applied to each byte. length-
An
i32or ani64representing the number of bytes to fill with thevalue. This will match theaddress_typethememorywas defined with.
Traps
If the indicated memory region is out of bounds, the instruction traps.
Binary encoding
| Instruction | Binary format | Example text => binary |
|---|---|---|
memory.fill |
0xfc 11:u32 x:memidx |
memory.fill 0 => 0xfc 0x0b 0x00 |
Examples
>Fill within the default (0-index) memory
;; 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 within a specified memory
;; 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))
Specifications
| Specification |
|---|
| WebAssembly Core Specification> # -hrefsyntax-instr-memorymathsfmemoryfillx> |
Browser compatibility
See also
memorydefinition