↑ arvau

MDN / const: Wasm numeric instruction

The const numeric instruction is used to declare numbers.

(module
  (import "console" "log" (func $log (param i32)))
  (func $main

    i32.const 10 ;; load a number onto the stack
    call $log ;; log the number

  )
  (start $main)
)
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console });

Syntax

value_type.const
  • value_type

    • : The type of value the instruction is being run on. The following types support const:

    const

    • : The const instruction. Must always be included after the value_type and a period (.).

Type

[] -> [output]
  • output

    • : The value being pushed onto the stack:

      • For a non-SIMD value_type, this will be a basic numeric value such as 3 or 3.5.

      • For a SIMD value_type, this will be a v128 type followed by a SIMD value interpretation, for example i32x4 0x9 0xa 0xb 0xc.

Binary encoding

InstructionBinary formatExample text => binaryi32.const0x41 i:leb128i32.const 2 => 0x41 0x02i64.const0x42 i:leb128i64.const 1 => 0x42 0x01f32.const0x43 f:float32f32.const 2.5 => 0x43 0x00 0x00 0x20 0x40f64.const0x44 f:float64f64.const 2.5 => 0x44 0x00 0x00 0x00 0x00 0x00 0x00 0x04 0x40v128.const0xfd 0x0c (b:byte)¹⁶v128.const i32x4 0x9 0xa 0xb 0xc => 0xfd 0x0c 0x09 0x00 0x00 0x00 0x0a 0x00 0x00 0x00 0x0b 0x00 0x00 0x00 0x0c 0x00 0x00 0x00

Specifications

Browser compatibility