Types
Note
In some places, possible types include both type constructors or types denoted by type indices.
Thus, the binary format for type constructors corresponds to the encodings of small negative \({{\href{../syntax/values.html#syntax-int}{\mathit{s}\kern-0.1em}}}{N}\) values, such that they can unambiguously occur in the same place as (positive) type indices.
Number Types
Number types are encoded by a single byte.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-numtype}{\mathtt{numtype}}} & ::= & \mathtt{0x7C} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-numtype}{\mathsf{f\scriptstyle64}} \\
& & | & \mathtt{0x7D} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-numtype}{\mathsf{f\scriptstyle32}} \\
& & | & \mathtt{0x7E} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-numtype}{\mathsf{i\scriptstyle64}} \\
& & | & \mathtt{0x7F} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-numtype}{\mathsf{i\scriptstyle32}} \\
\end{array}\end{split}\]
Vector Types
Vector types are also encoded by a single byte.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-vectype}{\mathtt{vectype}}} & ::= & \mathtt{0x7B} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-vectype}{\mathsf{v\scriptstyle128}} \\
\end{array}\end{split}\]
Heap Types
Heap types are encoded as either a single byte, or as a type index encoded as a positive signed integer.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-absheaptype}{\mathtt{absheaptype}}} & ::= & \mathtt{0x69} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{exn}} \\
& & | & \mathtt{0x6A} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{array}} \\
& & | & \mathtt{0x6B} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{struct}} \\
& & | & \mathtt{0x6C} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{i\scriptstyle31}} \\
& & | & \mathtt{0x6D} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{eq}} \\
& & | & \mathtt{0x6E} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{any}} \\
& & | & \mathtt{0x6F} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{extern}} \\
& & | & \mathtt{0x70} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{func}} \\
& & | & \mathtt{0x71} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{none}} \\
& & | & \mathtt{0x72} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{noextern}} \\
& & | & \mathtt{0x73} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{nofunc}} \\
& & | & \mathtt{0x74} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-heaptype}{\mathsf{noexn}} \\[0.8ex]
& {\href{../binary/types.html#binary-heaptype}{\mathtt{heaptype}}} & ::= & {\mathit{ht}}{:}{\href{../binary/types.html#binary-absheaptype}{\mathtt{absheaptype}}} & \quad\Rightarrow\quad{} & {\mathit{ht}} \\
& & | & x{:}{\href{../binary/values.html#binary-int}{\def\mathdef1840#1{{\mathtt{s}#1}}\mathdef1840{\mathtt{33}}}} & \quad\Rightarrow\quad{} & x & \quad \mbox{if}~ x \geq 0 \\
\end{array}\end{split}\]
Note
The heap type \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\) cannot occur in a module.
Reference Types
Reference types are either encoded by a single byte followed by a heap type, or, as a short form, directly as an abstract heap type.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-reftype}{\mathtt{reftype}}} & ::= & \mathtt{0x63}~~{\mathit{ht}}{:}{\href{../binary/types.html#binary-heaptype}{\mathtt{heaptype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~{\mathit{ht}} \\
& & | & \mathtt{0x64}~~{\mathit{ht}}{:}{\href{../binary/types.html#binary-heaptype}{\mathtt{heaptype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~{\mathit{ht}} \\
& & | & {\mathit{ht}}{:}{\href{../binary/types.html#binary-absheaptype}{\mathtt{absheaptype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-reftype}{\mathsf{ref}}~\href{../syntax/types.html#syntax-reftype}{\mathsf{null}}~{\mathit{ht}} \\
\end{array}\end{split}\]
Value Types
Value types are encoded with their respective encoding as a number type, vector type, or reference type.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-valtype}{\mathtt{valtype}}} & ::= & {\mathit{nt}}{:}{\href{../binary/types.html#binary-numtype}{\mathtt{numtype}}} & \quad\Rightarrow\quad{} & {\mathit{nt}} \\
& & | & {\mathit{vt}}{:}{\href{../binary/types.html#binary-vectype}{\mathtt{vectype}}} & \quad\Rightarrow\quad{} & {\mathit{vt}} \\
& & | & {\mathit{rt}}{:}{\href{../binary/types.html#binary-reftype}{\mathtt{reftype}}} & \quad\Rightarrow\quad{} & {\mathit{rt}} \\
\end{array}\end{split}\]
Note
The value type \(\href{../valid/conventions.html#syntax-valtype-ext}{\mathsf{bot}}\) cannot occur in a module.
Value types can occur in contexts where type indices are also allowed, such as in the case of block types.
Thus, the binary format for types corresponds to the signed LEB128 encoding of small negative \({{\href{../syntax/values.html#syntax-int}{\mathit{s}\kern-0.1em}}}{N}\) values, so that they can coexist with (positive) type indices in the future.
Result Types
Result types are encoded by the respective lists of value types.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-resulttype}{\mathtt{resulttype}}} & ::= & {t^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/types.html#binary-valtype}{\mathtt{valtype}}}) & \quad\Rightarrow\quad{} & {t^\ast} \\
\end{array}\end{split}\]
Composite Types
Composite types are encoded by a distinct byte followed by a type encoding of the respective form.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-mut}{\mathtt{mut}}} & ::= & \mathtt{0x00} & \quad\Rightarrow\quad{} & \epsilon \\
& & | & \mathtt{0x01} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-mut}{\mathsf{mut}} \\[0.8ex]
& {\href{../binary/types.html#binary-comptype}{\mathtt{comptype}}} & ::= & \mathtt{0x5E}~~{\mathit{yt}}{:}{\href{../binary/types.html#binary-fieldtype}{\mathtt{fieldtype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-comptype}{\mathsf{array}}~{\mathit{yt}} \\
& & | & \mathtt{0x5F}~~{{\mathit{yt}}^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/types.html#binary-fieldtype}{\mathtt{fieldtype}}}) & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-comptype}{\mathsf{struct}}~{{\mathit{yt}}^\ast} \\
& & | & \mathtt{0x60}~~{t_1^\ast}{:}{\href{../binary/types.html#binary-resulttype}{\mathtt{resulttype}}}~~{t_2^\ast}{:}{\href{../binary/types.html#binary-resulttype}{\mathtt{resulttype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-comptype}{\mathsf{func}}~({t_1^\ast} \href{../syntax/types.html#syntax-functype}{\rightarrow} {t_2^\ast}) \\[0.8ex]
& {\href{../binary/types.html#binary-fieldtype}{\mathtt{fieldtype}}} & ::= & {\mathit{zt}}{:}{\href{../binary/types.html#binary-storagetype}{\mathtt{storagetype}}}~~{\href{../syntax/types.html#syntax-mut}{\mathsf{mut}}^?}{:}{\href{../binary/types.html#binary-mut}{\mathtt{mut}}} & \quad\Rightarrow\quad{} & {\href{../syntax/types.html#syntax-mut}{\mathsf{mut}}^?}~{\mathit{zt}} \\[0.8ex]
& {\href{../binary/types.html#binary-storagetype}{\mathtt{storagetype}}} & ::= & t{:}{\href{../binary/types.html#binary-valtype}{\mathtt{valtype}}} & \quad\Rightarrow\quad{} & t \\
& & | & {\mathit{pt}}{:}{\href{../binary/types.html#binary-packtype}{\mathtt{packtype}}} & \quad\Rightarrow\quad{} & {\mathit{pt}} \\[0.8ex]
& {\href{../binary/types.html#binary-packtype}{\mathtt{packtype}}} & ::= & \mathtt{0x77} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-storagetype}{\mathsf{i\scriptstyle16}} \\
& & | & \mathtt{0x78} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-storagetype}{\mathsf{i\scriptstyle8}} \\
\end{array}\end{split}\]
Recursive Types
Recursive types are encoded by the byte \(\mathtt{0x4E}\) followed by a list of sub types.
Additional shorthands are recognized for unary recursions and sub types without super types.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-rectype}{\mathtt{rectype}}} & ::= & \mathtt{0x4E}~~{{\mathit{st}}^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/types.html#binary-subtype}{\mathtt{subtype}}}) & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~{{\mathit{st}}^\ast} \\
& & | & {\mathit{st}}{:}{\href{../binary/types.html#binary-subtype}{\mathtt{subtype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-rectype}{\mathsf{rec}}~{\mathit{st}} \\[0.8ex]
& {\href{../binary/types.html#binary-subtype}{\mathtt{subtype}}} & ::= & \mathtt{0x4F}~~{x^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}})~~{\mathit{ct}}{:}{\href{../binary/types.html#binary-comptype}{\mathtt{comptype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}~{x^\ast}~{\mathit{ct}} \\
& & | & \mathtt{0x50}~~{x^\ast}{:}{\href{../binary/conventions.html#binary-list}{\mathtt{list}}}({\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}})~~{\mathit{ct}}{:}{\href{../binary/types.html#binary-comptype}{\mathtt{comptype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~{x^\ast}~{\mathit{ct}} \\
& & | & {\mathit{ct}}{:}{\href{../binary/types.html#binary-comptype}{\mathtt{comptype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-subtype}{\mathsf{sub}}~\href{../syntax/types.html#syntax-subtype}{\mathsf{final}}~\epsilon~{\mathit{ct}} \\
\end{array}\end{split}\]
Limits
Limits are encoded with a preceding flag indicating whether a maximum is present, and a flag for the address type.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-limits}{\mathtt{limits}}} & ::= & \mathtt{0x00}~~n{:}{\href{../binary/values.html#binary-int}{\def\mathdef1835#1{{\mathtt{u}#1}}\mathdef1835{\mathtt{64}}}} & \quad\Rightarrow\quad{} & (\href{../syntax/types.html#syntax-numtype}{\mathsf{i\scriptstyle32}}, {}[ n \href{../syntax/types.html#syntax-limits}{\,{..}\,} {2^{64}} - 1 ]) \\
& & | & \mathtt{0x01}~~n{:}{\href{../binary/values.html#binary-int}{\def\mathdef1835#1{{\mathtt{u}#1}}\mathdef1835{\mathtt{64}}}}~~m{:}{\href{../binary/values.html#binary-int}{\def\mathdef1835#1{{\mathtt{u}#1}}\mathdef1835{\mathtt{64}}}} & \quad\Rightarrow\quad{} & (\href{../syntax/types.html#syntax-numtype}{\mathsf{i\scriptstyle64}}, {}[ n \href{../syntax/types.html#syntax-limits}{\,{..}\,} m ]) \\
& & | & \mathtt{0x04}~~n{:}{\href{../binary/values.html#binary-int}{\def\mathdef1835#1{{\mathtt{u}#1}}\mathdef1835{\mathtt{64}}}} & \quad\Rightarrow\quad{} & (\href{../syntax/types.html#syntax-numtype}{\mathsf{i\scriptstyle32}}, {}[ n \href{../syntax/types.html#syntax-limits}{\,{..}\,} {2^{64}} - 1 ]) \\
& & | & \mathtt{0x05}~~n{:}{\href{../binary/values.html#binary-int}{\def\mathdef1835#1{{\mathtt{u}#1}}\mathdef1835{\mathtt{64}}}}~~m{:}{\href{../binary/values.html#binary-int}{\def\mathdef1835#1{{\mathtt{u}#1}}\mathdef1835{\mathtt{64}}}} & \quad\Rightarrow\quad{} & (\href{../syntax/types.html#syntax-numtype}{\mathsf{i\scriptstyle64}}, {}[ n \href{../syntax/types.html#syntax-limits}{\,{..}\,} m ]) \\
\end{array}\end{split}\]
Memory Types
Memory types are encoded with their limits.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-memtype}{\mathtt{memtype}}} & ::= & ({\mathit{at}}, {\mathit{lim}}){:}{\href{../binary/types.html#binary-limits}{\mathtt{limits}}} & \quad\Rightarrow\quad{} & {\mathit{at}}~{\mathit{lim}}~\href{../syntax/types.html#syntax-memtype}{\mathsf{page}} \\
\end{array}\end{split}\]
Table Types
Table types are encoded with their limits and the encoding of their element reference type.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-tabletype}{\mathtt{tabletype}}} & ::= & {\mathit{rt}}{:}{\href{../binary/types.html#binary-reftype}{\mathtt{reftype}}}~~({\mathit{at}}, {\mathit{lim}}){:}{\href{../binary/types.html#binary-limits}{\mathtt{limits}}} & \quad\Rightarrow\quad{} & {\mathit{at}}~{\mathit{lim}}~{\mathit{rt}} \\
\end{array}\end{split}\]
Global Types
Global types are encoded by their value type and a flag for their mutability.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-globaltype}{\mathtt{globaltype}}} & ::= & t{:}{\href{../binary/types.html#binary-valtype}{\mathtt{valtype}}}~~{\href{../syntax/types.html#syntax-mut}{\mathsf{mut}}^?}{:}{\href{../binary/types.html#binary-mut}{\mathtt{mut}}} & \quad\Rightarrow\quad{} & {\href{../syntax/types.html#syntax-mut}{\mathsf{mut}}^?}~t \\
\end{array}\end{split}\]
Tag Types
Tag types are encoded by a type index denoting a function type.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-tagtype}{\mathtt{tagtype}}} & ::= & \mathtt{0x00}~~x{:}{\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}} & \quad\Rightarrow\quad{} & x \\
\end{array}\end{split}\]
Note
In future versions of WebAssembly,
the preceding zero byte may encode additional flags.
External Types
External types are encoded by a distiguishing byte followed by an encoding of the respective form of type.
\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}l@{}l@{}}
& {\href{../binary/types.html#binary-externtype}{\mathtt{externtype}}} & ::= & \mathtt{0x00}~~x{:}{\href{../binary/modules.html#binary-typeidx}{\mathtt{typeidx}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-externtype}{\mathsf{func}}~x \\
& & | & \mathtt{0x01}~~{\mathit{tt}}{:}{\href{../binary/types.html#binary-tabletype}{\mathtt{tabletype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-externtype}{\mathsf{table}}~{\mathit{tt}} \\
& & | & \mathtt{0x02}~~{\mathit{mt}}{:}{\href{../binary/types.html#binary-memtype}{\mathtt{memtype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-externtype}{\mathsf{mem}}~{\mathit{mt}} \\
& & | & \mathtt{0x03}~~{\mathit{gt}}{:}{\href{../binary/types.html#binary-globaltype}{\mathtt{globaltype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-externtype}{\mathsf{global}}~{\mathit{gt}} \\
& & | & \mathtt{0x04}~~x{:}{\href{../binary/types.html#binary-tagtype}{\mathtt{tagtype}}} & \quad\Rightarrow\quad{} & \href{../syntax/types.html#syntax-tagtype}{\mathsf{tag}}~x \\
\end{array}\end{split}\]