Calling Functions & Recursion
call $f pops the function's arguments from the stack (first parameter deepest), runs $f, and pushes its
results. Functions are identified by index: imports come first, then the module's own functions in order.
Calling and composing
(module
(import "console" "log" (func $log (param i32))) ;; function 0
(func $double (param i32) (result i32) ;; function 1
(i32.mul (local.get 0) (i32.const 2)))
(func $increment (param i32) (result i32) ;; function 2
(i32.add (local.get 0) (i32.const 1)))
(func (export "main")
;; double(increment(double(5))) = 22
(call $log (call $double (call $increment (call $double (i32.const 5)))))
;; the same with numeric indices, flat form
i32.const 5
call 1
call 2
call 1
call 0))22
22
A call's arguments are evaluated before the call, left to right, and the callee gets its own fresh locals.
Recursion
A function may call itself. Here is the classic factorial, using i64 so the result doesn't overflow too soon:
(module
(import "console" "log" (func $log (param i64)))
(func $fact (param $n i64) (result i64)
(if (result i64) (i64.le_u (local.get $n) (i64.const 1))
(then (i64.const 1))
(else
(i64.mul
(local.get $n)
(call $fact (i64.sub (local.get $n) (i64.const 1)))))))
(func (export "main")
(call $log (call $fact (i64.const 5)))
(call $log (call $fact (i64.const 20)))
(call $log (call $fact (i64.const 21))))) ;; overflows 64 bits and wraps120
2432902008176640000
-4249290049419214848
21! is 51090942171709440000, which doesn't fit in 64 bits, so the result silently wraps to a negative number.
Mutual recursion
Functions can call each other in any order: there is no need to declare a function before using it.
(module
(import "console" "log" (func $log (param i32)))
(func $is_even (param $n i32) (result i32)
(if (result i32) (i32.eqz (local.get $n))
(then (i32.const 1))
(else (call $is_odd (i32.sub (local.get $n) (i32.const 1))))))
(func $is_odd (param $n i32) (result i32)
(if (result i32) (i32.eqz (local.get $n))
(then (i32.const 0))
(else (call $is_even (i32.sub (local.get $n) (i32.const 1))))))
(func (export "main")
(call $log (call $is_even (i32.const 10)))
(call $log (call $is_odd (i32.const 7)))
(call $log (call $is_even (i32.const 3)))))1
1
0
The call stack is not the value stack
Each call gets a new frame for its locals, managed by the engine. Wasm code can't see return addresses or other frames, which is part of what makes it safe: a buffer overflow in linear memory can never overwrite a return address. Compilers that need addressable stack data (C arrays, Rust structs whose address is taken) keep a separate shadow stack in linear memory, with a global as stack pointer.
The engine's call stack is finite. Very deep recursion exhausts it, and the engine aborts the call (in V8 the error
is RangeError: Maximum call stack size exceeded; other engines word it differently).
Tail calls
A tail call is a call that is the last thing a function does. With the tail-call feature (standardised in
WebAssembly 3.0 and supported by current browsers) you write return_call instead of call: the current frame is
replaced instead of growing the stack, so tail-recursive loops can run any number of times.
(module
(import "console" "log" (func $log (param i64)))
;; sum 1..n with an accumulator, tail-recursively
(func $sum (param $n i64) (param $acc i64) (result i64)
(if (result i64) (i64.eqz (local.get $n))
(then (local.get $acc))
(else
(return_call $sum
(i64.sub (local.get $n) (i64.const 1))
(i64.add (local.get $acc) (local.get $n))))))
(func (export "main")
(call $log (call $sum (i64.const 100) (i64.const 0)))
(call $log (call $sum (i64.const 1000000) (i64.const 0)))))5050
500000500000
A million nested ordinary calls would overflow the stack; with return_call the depth stays at one. Functional
languages (Scheme, OCaml, Haskell backends) rely on this.
Iteration vs recursion
Recursive Fibonacci is elegant but slow (it recomputes the same values). The loop version runs in linear time:
(module
(import "console" "log" (func $log2 (param i32 i32)))
(func $fib_rec (param $n i32) (result i32)
(if (result i32) (i32.lt_u (local.get $n) (i32.const 2))
(then (local.get $n))
(else (i32.add
(call $fib_rec (i32.sub (local.get $n) (i32.const 1)))
(call $fib_rec (i32.sub (local.get $n) (i32.const 2)))))))
(func $fib_loop (param $n i32) (result i32)
(local $a i32) (local $b i32) (local $t i32)
(local.set $b (i32.const 1))
(block $done
(loop $step
(br_if $done (i32.eqz (local.get $n)))
(local.set $t (i32.add (local.get $a) (local.get $b)))
(local.set $a (local.get $b))
(local.set $b (local.get $t))
(local.set $n (i32.sub (local.get $n) (i32.const 1)))
(br $step)))
(local.get $a))
(func (export "main")
(call $log2 (call $fib_rec (i32.const 20)) (call $fib_loop (i32.const 20)))
(call $log2 (call $fib_rec (i32.const 25)) (call $fib_loop (i32.const 25)))))6765 6765
75025 75025
Both agree; the loop version just does far less work.
Test yourself
In a module with 2 imported functions followed by 3 defined functions, what index does the first defined function have?
- 0
- 1
- 2
- 3
Why can't a buffer overflow in linear memory overwrite a Wasm return address?
- Because Wasm has no functions
- Return addresses live in the engine's protected call stack, not in linear memory
- Because memory is read-only
- Because overflows trap immediately
What does return_call $f do?
- Calls
$fand ignores its result - Calls
$freplacing the current frame (a tail call) - Returns a reference to
$f - Calls
$fasynchronously