Search code examples
webassembly

How do I make a conditional return in WASM?


I am writing a wasm module.

So the code is:

(module
  (func (export "someCheck")
    (param $n f64)
    (result i32)

    local.get $n
    f64.const 2
    f64.eq
  )
)

This is a simple module that returns the result of $n being equal to 2. If $n is 2 then return 1, otherwise return 0. Here everything is clear to me.

Here is a similar function written in JavaScript:

const someCheck = (n) => Number(n === 2)

But how do I make a conditional return?

Like this, but in wasm:

const someCheck = (n) => {
  if (Number(n === 2))
    return 1;

  // other logic
  return someRez;
}

Thanks!


NOTE I think someone might need this table. She helps me a lot. Especially stack hints


Solution

  • WebAssembly has the concept of if/else control structures.

    https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow/if...else