(global...)
Create a global variable that be used throughout the module and even be used outside WASM in JavaScript. If you are importing or exporting the global variable then you will need to use the WebAssembly.Global object on the JavaScript side.

Syntax

(global $label type expression )
(global $label (mut type) expression )
(global $label (import...) type )
(global $label (export...) type expression)

Parameters

$label Optional
The label name of the global variable. Each global variable can be used either with the label or using an index to it. The first global variable is given the index of 0, with the next index being 1, and so on.
type
The data type the variable uses.
expression Optional
Used to set the starting value of the global variable. Use something like (i32.const 0) depending on the data type of the global variable. When exporting the expression is required.
mut Optional
Used to set if the global variable mutable (can be changed) or immutable (cannot be changes). By default all global variables act like constants which cannot be changed.
(import...) Optional
Used to import the global variable from JavaScript.
(export...) Optional
Used to export the global variable to JavaScript.

Examples

Internal Constant

;; Global constant (immutable, cannot be changed)
(global $piDiv180 f64 (f64.const 0.0174533))

;; Degree to radian function
(func $degreeToRadian (param $degree f64) (result f64)
    ;; Multiple pi/180 by $degree
    global.get $piDiv180
    local.get $degree
    f64.mul

    ;; The stack contains a f64 value with radians value    
)

Internal Variable

;; Global variable (mutable, can be changed)
(global $vectorX (mut f64) (f64.const 0))

;; Scale vector X
(func $scaleVectorX (param $scaler f64)
    ;; Multiple vectorX by the scaler
    global.get $vectorX
    local.get $scaler
    f64.mul

    ;; Update the $vectorX value
    global.set $vectorX
)

Imported From Literal

;; Import global from a literal
(global $maxCount (import "import" "maxCount") f64)
// Set options
const options = {
    import: {
        maxCount: 101
    }
}

// Instantiate the WASM data
const promise = WebAssembly.instantiate(wasmData, options);

Imported From Literal

;; Import global from a WebAssembly.Global object (with mutable = true)
(global $upperLimit (import "import" "upperLimit") (mut i32))
// Create some WASM global object
const upperLimit = new WebAssembly.Global({ value: 'i32', mutable: true }, 404);

// Set options
const options = {
    import: {
        upperLimit: upperLimit
    }
}

// Instantiate the WASM data
const promise = WebAssembly.instantiate(wasmData, options);  

Exported

;; Export global to a WebAssembly.Global
(global $lowerLimit (export "lowerLimit") (mut i32) (f64.const 10))
// Create instance of WASM
const wasm = await WebAssembly.instantiateStreaming(fetch('app.wasm'));

// Get exported global object
const global = wasm.instance.exports.lowerLimit;