typestar

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

  1. (data (i32.const 0) "TRAILHEAD") writes at instantiation.
  2. i32.load8_u reads one byte back as an unsigned i32.
  3. The first byte is ASCII: 'T' reads as 84.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 4 in Memory, step 17 of 28 in Language basics.

← Previous Next →