WebAssembly Control Flow Instructions
WebAssembly control flow is expressed in structured instructions such as if, else, br, loop, return and block. Below is a list of the instructions.
- if - The beginning of an if instruction.
- then - The then block of an if instruction.
- else - The else block of an if instruction.
- loop - A labeled block used to create loops.
- block - A sequence of instructions, often used within expressions.
- br - Branch to the given label in a containing instruction or block.
- br_if - Identical to a branch, but with a prerequisite condition.
- br_table - Branches, but instead of to a label it jumps to a function index in a table.
- return - Returns a value from the instruction.
- end - Marks the end of a block, loop, if, or a function.
- nop - An operation that does nothing.
Usage of if then else
If the value of $x is less than 10, then return the value of 10, else return the value of $x.
(module
(func $min10 (param $x i32) (result i32)
(if (result i32)
(i32.lt_s
(get_local $x)
(i32.const 10)
)
(then
(i32.const 10)
)
(else
(get_local $x)
)
)
)
(export "min10" (func $min10))
)
Complete Sample Source code