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
(block $out (result i32) ...)opens a labeled region.br_if $outleaves early, carrying the value already on the stack.- The pattern chains: push a candidate answer, test, fall through.
Keywords and builtins used here
blockbr_ifdropexportfunci32moduleparamresult
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.
Step 2 of 3 in Control flow, step 11 of 28 in Language basics.