typestar

Bulk memory in WebAssembly

Copy or fill whole byte runs in a single instruction.

(module
  ;; memory.fill and memory.copy move whole runs of bytes in one
  ;; instruction: destination, source or value, then length.
  (memory (export "mem") 1)
  (data (i32.const 0) "waypoint")

  (func (export "duplicate") (param $to i32)
    (memory.copy (local.get $to) (i32.const 0) (i32.const 8)))

  (func (export "blank") (param $at i32) (param $len i32)
    (memory.fill (local.get $at) (i32.const 32) (local.get $len)))

  (func (export "read") (param $i i32) (result i32)
    (i32.load8_u (local.get $i))))

How it works

  1. memory.copy moves length bytes from source to destination.
  2. memory.fill writes one byte value across a run — 32 is a space.
  3. i32.load8_u verifies what landed where.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
492
Tokens
133
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 123 seconds.

Type this snippet

Step 4 of 4 in Memory, step 19 of 28 in Language basics.

← Previous Next →