How does br_if
work? I have read the docs but I didn't find anything about result value of br_if
. I use WebAssembly Studio. I have this code and I don`t understand why it works so.
(func $f (param $a i32) (result i32)
(block (result i32)
(br_if 0 (i32.const 5) (get_local $a))
))
I supposed that br_if
execute only when conditional is not 0, but this function always return 5, even if param a is 0. I thought that the br_if
optionally returns a value and tries to set value after block, but it does not compile. Please explain how it works.
(func $f (param $a i32) (result i32)
(block (result i32)
(br_if 0 (i32.const 5) (get_local $a))
)
(i32.const 10))
Also I want to ask about returning value from blocks or loops, because in the docs I didn't see any mention of it. Can I optionally return a value from a loop or a block when a function returns a value in all branches?