Types

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

Value Types

Value types classify the individual values that WebAssembly code can compute with and the values that a variable accepts.

\[\begin{split}\begin{array}{llll} \def\mathdef2070#1{{}}\mathdef2070{value type} & \href{../syntax/types.html#syntax-valtype}{\mathit{valtype}} &::=& \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 single and double precision floating-point types as defined by the IEEE 754 standard

Conventions

  • The meta variable \(t\) ranges over value types where clear from context.
  • The notation \(|t|\) denotes the bit width of a value type. 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\).

Result Types

Result types classify the result of executing instructions or blocks, which is a sequence of values.

\[\begin{split}\begin{array}{llll} \def\mathdef2070#1{{}}\mathdef2070{result type} & \href{../syntax/types.html#syntax-resulttype}{\mathit{resulttype}} &::=& [\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}^?] \\ \end{array}\end{split}\]

Note

In the current version of WebAssembly, at most one value is allowed as a result. However, this may be generalized to sequences of values in future versions.

Function Types

Function types classify the signature of functions, mapping a vector of parameters to a vector of results.

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

Note

In the current version of WebAssembly, the length of the result type vector of a valid function type may be at most \(1\). This restriction may be removed in future versions.

Limits

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

\[\begin{split}\begin{array}{llll} \def\mathdef2070#1{{}}\mathdef2070{limits} & \href{../syntax/types.html#syntax-limits}{\mathit{limits}} &::=& \{ \href{../syntax/types.html#syntax-limits}{\mathsf{min}}~\href{../syntax/values.html#syntax-int}{\def\mathdef2076#1{{\mathit{u}#1}}\mathdef2076{\mathit{32}}}, \href{../syntax/types.html#syntax-limits}{\mathsf{max}}~\href{../syntax/values.html#syntax-int}{\def\mathdef2076#1{{\mathit{u}#1}}\mathdef2076{\mathit{32}}}^? \} \\ \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\mathdef2070#1{{}}\mathdef2070{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 element types within a size range.

\[\begin{split}\begin{array}{llll} \def\mathdef2070#1{{}}\mathdef2070{table type} & \href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}} &::=& \href{../syntax/types.html#syntax-limits}{\mathit{limits}}~\href{../syntax/types.html#syntax-elemtype}{\mathit{elemtype}} \\ \def\mathdef2070#1{{}}\mathdef2070{element type} & \href{../syntax/types.html#syntax-elemtype}{\mathit{elemtype}} &::=& \href{../syntax/types.html#syntax-elemtype}{\mathsf{anyfunc}} \\ \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.

The element type \(\href{../syntax/types.html#syntax-elemtype}{\mathsf{anyfunc}}\) is the infinite union of all function types. A table of that type thus contains references to functions of heterogeneous type.

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\mathdef2070#1{{}}\mathdef2070{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\mathdef2070#1{{}}\mathdef2070{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\mathdef2070#1{{}}\mathdef2070{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]\)