is_null: Wasm reference instruction

The ref.is_null reference instruction checks whether a reference is null.

Try it

(module
  (import "console" "log" (func $log (param i32)))
  (func $f)
  (elem declare func $f)
  (func $main
    ref.null func
    ref.is_null
    call $log ;; log 1: the reference is null

    ref.func $f
    ref.is_null
    call $log ;; log 0: the reference isn't null
  )
  (start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });

WAT syntax

ref.is_null
ref.is_null

The ref.is_null instruction.

Immediates

None.

Operand stack

[input:(ref null heap_type)] -> [output:i32]
input

The reference to check. This can be a reference of any nullable reference type, for example a funcref or an externref.

output

An i32 that is 1 if input is null, or 0 otherwise.

Binary encoding

Instruction Binary format Example text => binary
ref.is_null 0xd1 ref.is_null => 0xd1

Description

The ref.is_null instruction pops a reference off the stack and pushes 1 if it is null, or 0 if it isn't. It is typically used to check whether a nullable local, global or table element has been given a value yet, before using it with an instruction that would trap on a null reference.

To create a null reference, use ref.null. To convert a nullable reference into a non-nullable one, trapping if it is null, use ref.as_non_null.

Specifications

Specification
WebAssembly Core Specification
# -hrefsyntax-instr-refmathsfrefis_null

Browser compatibility

See also