Data segments in WebAssembly
Bytes baked into memory before anything runs.
(module
;; A data segment bakes bytes into memory at instantiation; load8_u
;; reads one byte back as an unsigned i32.
(memory 1)
(data (i32.const 0) "TRAILHEAD")
(func (export "byte_at") (param $i i32) (result i32)
(i32.load8_u (local.get $i)))
;; The first byte, 'T', is ASCII 84.
(func (export "first_byte") (result i32)
(i32.load8_u (i32.const 0))))
How it works
(data (i32.const 0) "TRAILHEAD")writes at instantiation.i32.load8_ureads one byte back as an unsigned i32.- The first byte is ASCII: 'T' reads as 84.
Keywords and builtins used here
dataexportfunci32memorymoduleparamresult
The run, in numbers
- Lines
- 12
- Characters to type
- 355
- Tokens
- 65
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 60 seconds.
Step 2 of 4 in Memory, step 17 of 28 in Language basics.