typestar

block and br_if in WebAssembly

Labels are exits: br on a block jumps past its end.

(module
  ;; br jumps to a label; on a block that means "exit here". br_if
  ;; makes the jump conditional -- this is the early return of WAT.
  (func (export "first_fault") (param $a i32) (param $b i32) (result i32)
    (block $out (result i32)
      (i32.const 1)
      (br_if $out (i32.eqz (local.get $a)))
      drop
      (i32.const 2)
      (br_if $out (i32.eqz (local.get $b)))
      drop
      (i32.const 0))))

How it works

  1. (block $out (result i32) ...) opens a labeled region.
  2. br_if $out leaves early, carrying the value already on the stack.
  3. The pattern chains: push a candidate answer, test, fall through.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
366
Tokens
72
Three-star pace
60 tpm

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

Type this snippet

Step 2 of 3 in Control flow, step 11 of 28 in Language basics.

← Previous Next →