Types

Various entities in WebAssembly are classified by types. Types are checked during validation, instantiation, and possibly execution.

Number Types

Number types classify numeric values.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{number type} & \href{../syntax/types.html#syntax-numtype}{\mathit{numtype}} &::=& \href{../syntax/types.html#syntax-valtype}{\mathsf{i32}} ~|~ \href{../syntax/types.html#syntax-valtype}{\mathsf{i64}} ~|~ \href{../syntax/types.html#syntax-valtype}{\mathsf{f32}} ~|~ \href{../syntax/types.html#syntax-valtype}{\mathsf{f64}} \\ \end{array}\end{split}\]

The types \(\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}\) and \(\href{../syntax/types.html#syntax-valtype}{\mathsf{i64}}\) classify 32 and 64 bit integers, respectively. Integers are not inherently signed or unsigned, their interpretation is determined by individual operations.

The types \(\href{../syntax/types.html#syntax-valtype}{\mathsf{f32}}\) and \(\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}\) classify 32 and 64 bit floating-point data, respectively. They correspond to the respective binary floating-point representations, also known as single and double precision, as defined by the IEEE 754 standard (Section 3.3).

Number types are transparent, meaning that their bit patterns can be observed. Values of number type can be stored in memories.

Conventions

  • The notation \(|t|\) denotes the bit width of a number type \(t\). That is, \(|\href{../syntax/types.html#syntax-valtype}{\mathsf{i32}}| = |\href{../syntax/types.html#syntax-valtype}{\mathsf{f32}}| = 32\) and \(|\href{../syntax/types.html#syntax-valtype}{\mathsf{i64}}| = |\href{../syntax/types.html#syntax-valtype}{\mathsf{f64}}| = 64\).

Vector Types

Vector types classify vectors of numeric values processed by vector instructions (also known as SIMD instructions, single instruction multiple data).

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{vector type} & \href{../syntax/types.html#syntax-vectype}{\mathit{vectype}} &::=& \href{../syntax/types.html#syntax-valtype}{\mathsf{v128}} \\ \end{array}\end{split}\]

The type \(\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}\) corresponds to a 128 bit vector of packed integer or floating-point data. The packed data can be interpreted as signed or unsigned integers, single or double precision floating-point values, or a single 128 bit type. The interpretation is determined by individual operations.

Vector types, like number types are transparent, meaning that their bit patterns can be observed. Values of vector type can be stored in memories.

Conventions

  • The notation \(|t|\) for bit width extends to vector types as well, that is, \(|\href{../syntax/types.html#syntax-valtype}{\mathsf{v128}}| = 128\).

Heap Types

Heap types classify objects in the runtime store.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{heap type} & \href{../syntax/types.html#syntax-heaptype}{\mathit{heaptype}} &::=& \href{../syntax/types.html#syntax-heaptype}{\mathsf{func}} ~|~ \href{../syntax/types.html#syntax-heaptype}{\mathsf{extern}} ~|~ \href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}} ~|~ \href{../syntax/types.html#syntax-functype}{\mathit{functype}} ~|~ \href{../syntax/types.html#syntax-valtype}{\mathsf{bot}} \\ \end{array}\end{split}\]

The type \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{func}}\) denotes the infinite union of all types of functions, regardless of their concrete function types.

The type \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{extern}}\) denotes the infinite union of all objects owned by the embedder and that can be passed into WebAssembly under this type.

A concrete heap type consists of a type index and classifies an object of the respective type defined in some module.

A concrete heap type can also consist of a function type directly. However, this form is representable in neither the binary format nor the text format, such that it cannot be used in a program; it only occurs during validation or execution, as the result of substituting a type index with its definition.

The type \(\href{../syntax/types.html#syntax-valtype}{\mathsf{bot}}\) is a subtype of all other heap types. By virtue of being representable in neither the binary format nor the text format, it cannot be used in a program; it only occurs during validation, as a part of a possible operand type for instructions.

A type of any form is closed when it does not contain a heap type that is a type index, i.e., all type indices have been substituted with their defined type.

Convention

  • \(t[x^\ast \href{../syntax/types.html#notation-subst}{\mathrel{\mathbf{:=}}} \mathit{ft}^\ast]\) denotes the parallel substitution of type indices \(x^\ast\) with function types \(\mathit{ft}^\ast\), provided \(|x^\ast| = |\mathit{ft}^\ast|\) in type \(t\).

  • \(t[\href{../syntax/types.html#notation-subst}{\mathrel{\mathbf{:=}}} \mathit{ft}^\ast]\) is shorthand for the substitution \(t[x^\ast \href{../syntax/types.html#notation-subst}{\mathrel{\mathbf{:=}}} \mathit{ft}^\ast]\) where \(x^\ast = 0 \cdots (|\mathit{ft}^\ast| - 1)\) in type \(t\).

Reference Types

Reference types classify values that are first-class references to objects in the runtime store.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{reference type} & \href{../syntax/types.html#syntax-reftype}{\mathit{reftype}} &::=& \href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}^?~\href{../syntax/types.html#syntax-heaptype}{\mathit{heaptype}} \\ \end{array}\end{split}\]

A reference type is characterised by the heap type it points to.

In addition, a reference type of the form \(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\mathit{ht}\) is nullable, meaning that it can either be a proper reference to \(\mathit{ht}\) or null. Other references are non-null.

Reference types are opaque, meaning that neither their size nor their bit pattern can be observed. Values of reference type can be stored in tables.

Conventions

  • The reference type \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{funcref}}\) is an abbreviation for \(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\href{../syntax/types.html#syntax-heaptype}{\mathsf{func}}\).

  • The reference type \(\href{../syntax/types.html#syntax-heaptype}{\mathsf{externref}}\) is an abbreviation for \(\href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~\href{../syntax/types.html#syntax-heaptype}{\mathsf{extern}}\).

Value Types

Value types classify the individual values that WebAssembly code can compute with and the values that a variable accepts. They are either number types, vector types, reference types, or the unique bottom type, written \(\href{../syntax/types.html#syntax-valtype}{\mathsf{bot}}\).

The type \(\href{../syntax/types.html#syntax-valtype}{\mathsf{bot}}\) is a subtype of all other value types. By virtue of being representable in neither the binary format nor the text format, it cannot be used in a program; it only occurs during validation, as a possible operand type for instructions.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{value type} & \href{../syntax/types.html#syntax-valtype}{\mathit{valtype}} &::=& \href{../syntax/types.html#syntax-numtype}{\mathit{numtype}} ~|~ \href{../syntax/types.html#syntax-vectype}{\mathit{vectype}} ~|~ \href{../syntax/types.html#syntax-reftype}{\mathit{reftype}} ~|~ \href{../syntax/types.html#syntax-valtype}{\mathsf{bot}} \\ \end{array}\end{split}\]

Conventions

  • The meta variable \(t\) ranges over value types or subclasses thereof where clear from context.

Result Types

Result types classify the result of executing instructions or functions, which is a sequence of values, written with brackets.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{result type} & \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} &::=& [\href{../syntax/conventions.html#syntax-vec}{\mathit{vec}}(\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}})] \\ \end{array}\end{split}\]

Instruction Types

Instruction types classify the behaviour of instructions or instruction sequences, by describing how they manipulate the operand stack and the initialization status of locals:

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{instruction type} & \href{../syntax/types.html#syntax-instrtype}{\mathit{instrtype}} &::=& \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} \def\mathdef2755#1{\rightarrow_{#1}}\mathdef2755{\href{../syntax/modules.html#syntax-localidx}{\mathit{localidx}}^\ast} \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} \\ \end{array}\end{split}\]

An instruction type \([t_1^\ast] \def\mathdef2756#1{\rightarrow_{#1}}\mathdef2756{x^\ast} [t_2^\ast]\) describes the required input stack with argument values of types \(t_1^\ast\) that an instruction pops off and the provided output stack with result values of types \(t_2^\ast\) that it pushes back. Moreover, it enumerates the indices \(x^\ast\) of locals that have been set by the instruction or sequence.

Note

Instruction types are only used for validation, they do not occur in programs.

Local Types

Local types classify locals, by describing their value type as well as their initialization status:

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{initialization status} & \href{../syntax/types.html#syntax-init}{\mathit{init}} &::=& \href{../syntax/types.html#syntax-init}{\mathsf{set}} ~|~ \href{../syntax/types.html#syntax-init}{\mathsf{unset}} \\ \def\mathdef2716#1{{}}\mathdef2716{local type} & \href{../syntax/types.html#syntax-localtype}{\mathit{localtype}} &::=& \href{../syntax/types.html#syntax-init}{\mathit{init}}~\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}} \\ \end{array}\end{split}\]

Note

Local types are only used for validation, they do not occur in programs.

Function Types

Function types classify the signature of functions, mapping a vector of parameters to a vector of results. They are also used to classify the inputs and outputs of instructions.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{function type} & \href{../syntax/types.html#syntax-functype}{\mathit{functype}} &::=& \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} \href{../syntax/types.html#syntax-instrtype}{\rightarrow} \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} \\ \end{array}\end{split}\]

Defined Types

Defined types are the ones that can be defined in a module, assigning them a type index.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{defined type} & \href{../syntax/types.html#syntax-deftype}{\mathit{deftype}} &::=& \href{../syntax/types.html#syntax-functype}{\mathit{functype}} \\ \end{array}\end{split}\]

Note

Future versions of WebAssembly may introduce additional forms of defined types.

Limits

Limits classify the size range of resizeable storage associated with memory types and table types.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{limits} & \href{../syntax/types.html#syntax-limits}{\mathit{limits}} &::=& \{ \href{../syntax/types.html#syntax-limits}{\mathsf{min}}~\href{../syntax/values.html#syntax-int}{\mathit{u32}}, \href{../syntax/types.html#syntax-limits}{\mathsf{max}}~\href{../syntax/values.html#syntax-int}{\mathit{u32}}^? \} \\ \end{array}\end{split}\]

If no maximum is given, the respective storage can grow to any size.

Memory Types

Memory types classify linear memories and their size range.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{memory type} & \href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} &::=& \href{../syntax/types.html#syntax-limits}{\mathit{limits}} \\ \end{array}\end{split}\]

The limits constrain the minimum and optionally the maximum size of a memory. The limits are given in units of page size.

Table Types

Table types classify tables over elements of reference type within a size range.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{table type} & \href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}} &::=& \href{../syntax/types.html#syntax-limits}{\mathit{limits}}~\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}} \\ \end{array}\end{split}\]

Like memories, tables are constrained by limits for their minimum and optionally maximum size. The limits are given in numbers of entries.

Note

In future versions of WebAssembly, additional element types may be introduced.

Global Types

Global types classify global variables, which hold a value and can either be mutable or immutable.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{global type} & \href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}} &::=& \href{../syntax/types.html#syntax-mut}{\mathit{mut}}~\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}} \\ \def\mathdef2716#1{{}}\mathdef2716{mutability} & \href{../syntax/types.html#syntax-mut}{\mathit{mut}} &::=& \href{../syntax/types.html#syntax-mut}{\mathsf{const}} ~|~ \href{../syntax/types.html#syntax-mut}{\mathsf{var}} \\ \end{array}\end{split}\]

External Types

External types classify imports and external values with their respective types.

\[\begin{split}\begin{array}{llll} \def\mathdef2716#1{{}}\mathdef2716{external types} & \href{../syntax/types.html#syntax-externtype}{\mathit{externtype}} &::=& \href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}} ~|~ \href{../syntax/types.html#syntax-externtype}{\mathsf{table}}~\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}} ~|~ \href{../syntax/types.html#syntax-externtype}{\mathsf{mem}}~\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} ~|~ \href{../syntax/types.html#syntax-externtype}{\mathsf{global}}~\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}} \\ \end{array}\end{split}\]

Conventions

The following auxiliary notation is defined for sequences of external types. It filters out entries of a specific kind in an order-preserving fashion:

  • \(\href{../syntax/types.html#syntax-externtype}{\mathrm{funcs}}(\href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast) = [\href{../syntax/types.html#syntax-functype}{\mathit{functype}} ~|~ (\href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~\href{../syntax/types.html#syntax-functype}{\mathit{functype}}) \in \href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast]\)

  • \(\href{../syntax/types.html#syntax-externtype}{\mathrm{tables}}(\href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast) = [\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}} ~|~ (\href{../syntax/types.html#syntax-externtype}{\mathsf{table}}~\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}) \in \href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast]\)

  • \(\href{../syntax/types.html#syntax-externtype}{\mathrm{mems}}(\href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast) = [\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}} ~|~ (\href{../syntax/types.html#syntax-externtype}{\mathsf{mem}}~\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}) \in \href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast]\)

  • \(\href{../syntax/types.html#syntax-externtype}{\mathrm{globals}}(\href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast) = [\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}} ~|~ (\href{../syntax/types.html#syntax-externtype}{\mathsf{global}}~\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}) \in \href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}^\ast]\)