Skip to content

Local & Global

Get the value of a local variable by name or index.

Example:

(func $example (param $x i32) (result i32)
(local $temp i32)
(local.get $x) ;; Get parameter
(local.get $temp) ;; Get local
(local.get 0) ;; Get by index
)

Set the value of a local variable by name or index.

Example:

(func $example (param $x i32)
(local $result i32)
(local.set $result (i32.const 42))
(local.set 1 (i32.const 100)) ;; Set by index
)

Set the value of a local variable and return it (combination of set and get).

Example:

(func $example (result i32)
(local $x i32)
;; Set $x to 42 and also return it
(local.tee $x (i32.const 42))
)

Get the value of a global variable by name or index.

Example:

(global $counter (mut i32) (i32.const 0))
(func $read_counter (result i32)
(global.get $counter)
)

Set the value of a global variable by name or index. Only works on mutable globals.

Example:

(global $counter (mut i32) (i32.const 0))
(func $increment
(global.set $counter
(i32.add (global.get $counter) (i32.const 1)))
)