TExcelTAD
Home › WebAssembly › Multi-Value Returns

Multi-Value Returns

In the original WebAssembly (the 2017 MVP), a function could return at most one value. The multi-value feature, standard since WebAssembly 2.0, allows functions, blocks, loops and ifs to produce several results and blocks to take parameters.

Functions with several results

List all result types in (result …). The function leaves them on the stack in order:

wat · try it yourself
(module
  (import "console" "log" (func $log2 (param i32 i32)))

  ;; integer division returning quotient and remainder
  (func $divmod (param $a i32) (param $b i32) (result i32 i32)
    (i32.div_u (local.get $a) (local.get $b))
    (i32.rem_u (local.get $a) (local.get $b)))

  (func (export "main")
    (call $log2 (call $divmod (i32.const 17) (i32.const 5)))
    (call $log2 (call $divmod (i32.const 100) (i32.const 7)))))
3 2
14 2

Notice (call $log2 (call $divmod …)): the two results of $divmod become the two arguments of $log2 directly, without any locals.

Results can mix types, and the playground prints all of main's results:

wat · try it yourself
(module
  (func (export "main") (result i32 i64 f64)
    (i32.const 7)
    (i64.const 9000000000)
    (f64.const 0.125)))
7 9000000000 0.125

From JavaScript, a function with several results returns an array: instance.exports.divmod(17, 5) gives [3, 2].

Taking results apart

To use the results individually, pop them into locals. The last result is on top of the stack, so it's popped first:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func $min_max (param $a i32) (param $b i32) (result i32 i32)
    (if (result i32 i32) (i32.lt_s (local.get $a) (local.get $b))
      (then (local.get $a) (local.get $b))
      (else (local.get $b) (local.get $a))))
  (func (export "main")
    (local $lo i32) (local $hi i32)
    (call $min_max (i32.const 42) (i32.const 8))
    (local.set $hi)           ;; top of stack: the second result
    (local.set $lo)
    (call $log (local.get $lo))
    (call $log (local.get $hi))
    (call $log (i32.sub (local.get $hi) (local.get $lo)))))
8
42
34

Swapping without a temporary

With several results, a function can return its arguments in a different order. A swap needs no temporary local:

wat · try it yourself
(module
  (import "console" "log" (func $log2 (param i32 i32)))
  (func $swap (param i32 i32) (result i32 i32)
    (local.get 1) (local.get 0))
  (func (export "main")
    (call $log2 (i32.const 1) (i32.const 2))
    (call $log2 (call $swap (i32.const 1) (i32.const 2)))))
1 2
2 1

Loops with parameters

Blocks, loops and ifs can also have parameters: values they take from the stack when they start, declared like a function's, e.g. (loop $l (param i32 i32) (result i32 i32) …). A loop with parameters is powerful: branching back to the loop passes new values to the next iteration, like arguments to a recursive call. The loop state lives on the stack instead of in locals. Here's Fibonacci where each iteration receives (a, b):

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func $fib (param $n i32) (result i32)
    (local $a i32) (local $b i32)
    (i32.const 0) (i32.const 1)                       ;; initial (a, b)
    (loop $step (param i32 i32) (result i32 i32)
      (local.set $b) (local.set $a)                   ;; take the parameters
      (if (result i32 i32) (local.get $n)
        (then
          (local.set $n (i32.sub (local.get $n) (i32.const 1)))
          (local.get $b)                              ;; new a
          (i32.add (local.get $a) (local.get $b))     ;; new b
          (br $step))                                 ;; pass both to the next iteration
        (else (local.get $a) (local.get $b))))
    (drop))                                            ;; keep a, drop b
  (func (export "main")
    (call $log (call $fib (i32.const 10)))
    (call $log (call $fib (i32.const 30)))))
55
832040

This style is what some compilers produce, and it avoids locals for loop-carried values. In hand-written code, locals are often clearer; use whichever reads better.

Where multi-value helps

  • Returning pairs such as quotient/remainder, (value, ok) flags or 128-bit numbers as two i64s.
  • Rust and Swift can return small structs and tuples in registers instead of through memory.
  • Fewer memory round-trips: before multi-value, compilers had to write extra results to the shadow stack.
wat · try it yourself
(module
  ;; parse a decimal digit: returns (value, ok)
  (import "console" "log" (func $log2 (param i32 i32)))
  (func $digit (param $c i32) (result i32 i32)
    (local $v i32)
    (local.set $v (i32.sub (local.get $c) (i32.const 48)))
    (local.get $v)
    (i32.le_u (local.get $v) (i32.const 9)))
  (func (export "main")
    (call $log2 (call $digit (i32.const 55)))     ;; '7' -> 7, ok
    (call $log2 (call $digit (i32.const 65)))))   ;; 'A' -> 17, not ok
7 1
17 0

Test yourself

How does JavaScript receive the results of an exported function with (result i32 i32)?

  1. Only the first value
  2. As an array of two numbers
  3. As an object {0: …, 1: …}
  4. It can't be exported

After call $f where $f returns (result i32 f64), which value is on top of the stack?

  1. The i32
  2. The f64
  3. Neither, they are in locals
  4. It depends on the engine

What does br to a loop with (param i32 i32) require?

  1. Nothing
  2. Two i32 values on the stack, which become the loop's next parameters
  3. A table index
  4. That the loop has no result