Value Types
Core WebAssembly has only four number types. Everything else, from booleans and characters to pointers and structs, is built from them.
| Type | Size | What it holds | Constant |
|---|---|---|---|
i32 |
32 bits | Integer (also booleans and memory addresses) | i32.const 42 |
i64 |
64 bits | Integer | i64.const 9000000000 |
f32 |
32 bits | IEEE 754 single-precision float | f32.const 1.5 |
f64 |
64 bits | IEEE 754 double-precision float | f64.const 3.14159 |
Two more kinds of value type exist: v128 (a 128-bit SIMD vector, see the SIMD lesson) and reference types
(funcref, externref, see Reference Types).
Integers have no sign
An i32 is just 32 bits. It is neither signed nor unsigned: the instruction decides how to interpret it.
Instructions that care have _s (signed) and _u (unsigned) variants, such as i32.div_s / i32.div_u or
i32.lt_s / i32.lt_u. Instructions where it makes no difference (add, sub, mul, and, …) have just one form.
(module
(import "console" "log" (func $log (param i32)))
(func (export "main")
;; the bit pattern 0xFFFFFFFF is -1 as signed, 4294967295 as unsigned
(call $log (i32.const -1))
(call $log (i32.const 0xFFFFFFFF)) ;; same bits, same value printed
(call $log (i32.div_s (i32.const -8) (i32.const 2))) ;; signed: -8 / 2
(call $log (i32.div_u (i32.const -8) (i32.const 2))))) ;; unsigned: 4294967288 / 2-1
-1
-4
2147483644
The host's JavaScript log receives every i32 as a signed number, which is why 0xFFFFFFFF prints as -1.
The unsigned division treated the bits of -8 as 4294967288 and produced 2147483644.
Integer overflow wraps
Integer arithmetic is modular: it wraps around silently instead of trapping.
(module
(import "console" "log" (func $log (param i32)))
(import "console" "log" (func $log64 (param i64)))
(func (export "main")
(call $log (i32.add (i32.const 2147483647) (i32.const 1))) ;; max i32 + 1
(call $log64 (i64.add (i64.const 2147483647) (i64.const 1)))
(call $log64 (i64.const 9223372036854775807)))) ;; max i64-2147483648
2147483648
9223372036854775807
The i64 values arrive in JavaScript as BigInt, so even the largest 64-bit integer prints exactly.
Floats follow IEEE 754
f32 and f64 behave like float and double in C, or Math.fround(x) and ordinary numbers in JavaScript.
They include infinities, NaN and negative zero, and division by zero does not trap.
(module
(import "console" "log" (func $logf (param f64)))
(import "console" "log" (func $logs (param f32)))
(func (export "main")
(call $logf (f64.add (f64.const 0.1) (f64.const 0.2)))
(call $logs (f32.const 0.1)) ;; nearest f32 to 0.1, widened to a JS number
(call $logf (f64.div (f64.const 1) (f64.const 0)))
(call $logf (f64.div (f64.const -1) (f64.const 0)))
(call $logf (f64.const -0))
(call $logf (f64.const nan))))0.30000000000000004
0.10000000149011612
Infinity
-Infinity
-0
NaN
f32 has only about 7 significant decimal digits, so 0.1 stored as an f32 is visibly different from 0.1
once converted to a 64-bit JavaScript number.
Literal syntax
The text format accepts several ways to write constants:
(module
(import "console" "log" (func $log (param i32)))
(import "console" "log" (func $logf (param f64)))
(func (export "main")
(call $log (i32.const 1_000_000)) ;; underscores for readability
(call $log (i32.const 0xff)) ;; hexadecimal
(call $log (i32.const -0x10))
(call $logf (f64.const 1e3)) ;; exponent
(call $logf (f64.const 0x1p-2)) ;; hex float: 1 * 2^-2
(call $logf (f64.const inf))))1000000
255
-16
1000
0.25
Infinity
How types map to JavaScript
| Wasm | Arrives in JavaScript as | Sent from JavaScript as |
|---|---|---|
i32 |
Number (signed) | any value converted with ToInt32 |
i64 |
BigInt (signed) | a BigInt (a Number throws a TypeError) |
f32 |
Number | a Number, rounded to single precision |
f64 |
Number | a Number |
v128 |
not allowed: throws TypeError |
not allowed |
Tip: Booleans are just
i32: comparisons produce1for true and0for false, andif/br_iftreat any non-zero value as true.
Test yourself
Is an i32 signed or unsigned?
- Always signed
- Always unsigned
- Neither: instructions such as
div_s/div_udecide the interpretation - It depends on the module header
What does (i32.add (i32.const 2147483647) (i32.const 1)) produce?
- A trap
- 2147483648
- -2147483648
- 0
How does an exported function's i64 result appear in JavaScript?
- As a Number, possibly rounded
- As a BigInt
- As a string
- It cannot be returned