TExcelTAD
Home › WebAssembly › Globals

Globals

A global is a single typed value that belongs to the module instance rather than to a function call. Every function in the module can read it, and, if it is mutable, change it. Globals live outside linear memory, so they cannot be reached by a stray pointer.

text
(global $name type init)          ;; immutable: a constant
(global $name (mut type) init)    ;; mutable

The initializer must be a constant expression: a const instruction, or global.get of an imported global (newer engines also allow simple add/sub/mul in constant expressions).

Immutable and mutable globals

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (global $max_users i32 (i32.const 100))          ;; constant
  (global $logged_in (mut i32) (i32.const 0))      ;; changes at runtime

  (func $login
    (global.set $logged_in (i32.add (global.get $logged_in) (i32.const 1))))

  (func (export "main")
    (call $login)
    (call $login)
    (call $login)
    (call $log (global.get $logged_in))
    (call $log (i32.sub (global.get $max_users) (global.get $logged_in)))))
3
97

Trying to global.set an immutable global is a validation error:

wat · try it yourself
(module
  (global $pi f64 (f64.const 3.14159))
  (func (export "main")
    (global.set $pi (f64.const 3))))
main.wat:4:6: error: can't global.set on immutable global at index 0.
    (global.set $pi (f64.const 3))))
     ^^^^^^^^^^

Globals keep their value between calls

Locals are reset on every call; globals persist for the lifetime of the instance. That makes them useful for counters, configuration, and random number generator state. Here is a tiny deterministic pseudo-random generator (a 32-bit xorshift) whose state is a global:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (global $state (mut i32) (i32.const 2463534242))

  (func $next_random (result i32)
    (local $x i32)
    (local.set $x (global.get $state))
    (local.set $x (i32.xor (local.get $x) (i32.shl (local.get $x) (i32.const 13))))
    (local.set $x (i32.xor (local.get $x) (i32.shr_u (local.get $x) (i32.const 17))))
    (local.set $x (i32.xor (local.get $x) (i32.shl (local.get $x) (i32.const 5))))
    (global.set $state (local.get $x))
    (local.get $x))

  ;; a die roll 1..6 from the random bits
  (func $roll (result i32)
    (i32.add (i32.rem_u (call $next_random) (i32.const 6)) (i32.const 1)))

  (func (export "main")
    (call $log (call $roll))
    (call $log (call $roll))
    (call $log (call $roll))
    (call $log (call $roll))
    (call $log (call $roll))))
2
5
3
1
6

Because the seed is fixed, the sequence is the same every run, which is exactly what you want for tests.

Globals of every type

Any value type can be a global, including i64 and floats:

wat · try it yourself
(module
  (import "console" "log" (func $log64 (param i64)))
  (import "console" "log" (func $logf (param f64)))
  (global $big (mut i64) (i64.const 1))
  (global $rate f64 (f64.const 0.075))

  (func (export "main")
    (local $i i32)
    (loop $again
      (global.set $big (i64.mul (global.get $big) (i64.const 1000)))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br_if $again (i32.lt_u (local.get $i) (i32.const 6))))
    (call $log64 (global.get $big))
    (call $logf (f64.mul (f64.const 200) (global.get $rate)))))
1000000000000000000
15

Importing and exporting globals

Globals can be shared with the host. An imported global's value is set by the host; a mutable exported global can be read and changed from JavaScript through a WebAssembly.Global object (see Memory, Table & Global from JS):

js
const counter = new WebAssembly.Global({ value: 'i32', mutable: true }, 0);
const { instance } = await WebAssembly.instantiate(bytes, { env: { counter } });
instance.exports.tick();
console.log(counter.value);   // the Wasm code's changes are visible here

The stack pointer convention

Compilers from C, C++ and Rust use a mutable global named __stack_pointer to manage the shadow stack in linear memory: a function that needs memory for a local array subtracts from the stack pointer on entry and adds it back on exit. You'll meet it in the toolchain lessons.

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (memory 1)
  (global $sp (mut i32) (i32.const 65536))         ;; the stack grows down from the top of memory

  (func $with_scratch_space (result i32)
    (local $frame i32)
    ;; reserve 16 bytes
    (global.set $sp (local.tee $frame (i32.sub (global.get $sp) (i32.const 16))))
    (i32.store (local.get $frame) (i32.const 7))   ;; use the space
    ;; release them
    (global.set $sp (i32.add (local.get $frame) (i32.const 16)))
    (local.get $frame))

  (func (export "main")
    (call $log (call $with_scratch_space))
    (call $log (global.get $sp))))
65520
65536

Test yourself

How do you declare a global that can be changed?

  1. (global $g i32 …)
  2. (global $g (mut i32) …)
  3. (global mutable $g i32 …)
  4. (local $g i32)

What is the difference between a local and a global?

  1. Globals are faster
  2. A local exists for one call; a global persists for the whole instance
  3. Locals can be exported
  4. Globals live in linear memory

Which of these can initialise a global?

  1. (call $f)
  2. (i32.const 10)
  3. (local.get 0)
  4. (i32.load (i32.const 0))