typestar

drop and select in WebAssembly

Throw a value away, or pick between two without branching.

(module
  ;; drop throws the top of the stack away; select picks between two
  ;; values by a condition without any branching.
  (func $noisy (result i32)
    i32.const 99)

  (func (export "ignore_result") (result i32)
    (drop (call $noisy))
    (i32.const 7))

  (func (export "larger") (param $a i32) (param $b i32) (result i32)
    (select
      (local.get $a)
      (local.get $b)
      (i32.gt_s (local.get $a) (local.get $b)))))

How it works

  1. drop discards the called function's unwanted result.
  2. select takes two values and a condition and keeps one.
  3. Neither branches: select evaluates both sides, then chooses.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
393
Tokens
84
Three-star pace
65 tpm

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

Type this snippet

Step 3 of 3 in Locals & the stack, step 9 of 28 in Language basics.

← Previous Next →