memory.size and memory.grow in WebAssembly
Memory is counted in 64 KiB pages, growable at run time.
(module
;; Memory is measured in 64 KiB pages. size reports the current
;; count; grow adds pages and returns the old count, or -1.
(memory 1 4)
(func (export "pages_now") (result i32)
memory.size)
(func (export "add_pages") (param $n i32) (result i32)
(memory.grow (local.get $n)))
(func (export "bytes_available") (result i32)
(i32.mul memory.size (i32.const 65536))))
How it works
(memory 1 4)starts at one page and caps at four.memory.growadds pages, returning the old count or -1.- Multiplying
memory.sizeby 65536 turns pages into bytes.
Keywords and builtins used here
exportfunci32memorymoduleparamresult
The run, in numbers
- Lines
- 13
- Characters to type
- 373
- Tokens
- 69
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 64 seconds.
Step 3 of 4 in Memory, step 18 of 28 in Language basics.