Modules

WebAssembly programs are organized intomodules,which are the unit of deployment, loading, and compilation.A module collects definitions fortypes,tags, andglobals,memories,tables,functions.In addition, it can declareimports andexportsand provide initialization in the form ofdata andelement segments,or astart function.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-module}{\mathit{module}}} & ::= & \href{../syntax/modules.html#syntax-module}{\mathsf{module}}~{{\href{../syntax/types.html#syntax-rectype}{\mathit{type}}}^\ast}~{{\href{../syntax/modules.html#syntax-import}{\mathit{import}}}^\ast}~{{\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}}^\ast}~{{\href{../syntax/modules.html#syntax-global}{\mathit{global}}}^\ast}~{{\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}}^\ast}~{{\href{../syntax/modules.html#syntax-table}{\mathit{table}}}^\ast}~{{\href{../syntax/modules.html#syntax-func}{\mathit{func}}}^\ast}~{{\href{../syntax/modules.html#syntax-data}{\mathit{data}}}^\ast}~{{\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}}^\ast}~{{\href{../syntax/modules.html#syntax-start}{\mathit{start}}}^?}~{{\href{../syntax/modules.html#syntax-export}{\mathit{export}}}^\ast} \\\end{array}\end{split}\]

Each of the lists — and thus the entire module — may be empty.

Indices

Definitions are referenced with zero-basedindices.Each class of definition has its ownindex space, as distinguished by the following classes.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} & ::= & {\href{../syntax/values.html#syntax-int}{\mathit{u\scriptstyle\kern-0.1em32}}} \\& {\href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-funcidx}{\mathit{funcidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-globalidx}{\mathit{globalidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-memidx}{\mathit{memidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-tagidx}{\mathit{tagidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-elemidx}{\mathit{elemidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-dataidx}{\mathit{dataidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-labelidx}{\mathit{labelidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-localidx}{\mathit{localidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\& {\href{../syntax/modules.html#syntax-fieldidx}{\mathit{fieldidx}}} & ::= & {\href{../syntax/modules.html#syntax-idx}{\mathit{idx}}} \\\end{array}\end{split}\]

The index space fortags,globals,memories,tables, andfunctionsincludes respectiveimports declared in the same module.The indices of these imports precede the indices of other definitions in the same index space.

Data indices referencedata segments and element indices referenceelement segments.

The index space forlocals is only accessible inside afunction and includes the parameters of that function, which precede the local variables.

Label indices referencestructured control instructions inside an instruction sequence.

Eachaggregate type provides an index space for itsfields.

Conventions

  • The meta variable\(l\) ranges over label indices.

  • The meta variables\(x\),\(y\) range over indices in any of the other index spaces.

  • For every index space\({\mathit{abcidx}}\), the notation\({\mathrm{abcidx}}(A)\) denotes the set of indices from that index space occurring free in\(A\). Sometimes this set is reinterpreted as thelist of its elements.

Note

For example, if\({{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}}^\ast}\) is\((\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{data{.}drop}}~1)~(\href{../syntax/instructions.html#syntax-instr-memory}{\mathsf{memory{.}init}}~2~3)\), then\({\mathrm{dataidx}}_{\mathit{instrs}}({{\href{../syntax/instructions.html#syntax-instr}{\mathit{instr}}}^\ast}) = 1~3\), or equivalently, the set\(\{ 1, 3 \}\).

Types

The\({\href{../syntax/types.html#syntax-rectype}{\mathit{type}}}\) section of a module defines a list ofrecursive types, each consisting of a list ofsub types referenced by individualtype indices.Allfunction,structure, orarray types used in a module must be defined in this section.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/types.html#syntax-rectype}{\mathit{type}}} & ::= & \href{../syntax/modules.html#syntax-type}{\mathsf{type}}~{\href{../syntax/types.html#syntax-rectype}{\mathit{rectype}}} \\\end{array}\end{split}\]

Tags

The\({\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}}\) section of a module defines a list oftags:

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-tag}{\mathit{tag}}} & ::= & \href{../syntax/modules.html#syntax-tag}{\mathsf{tag}}~{\href{../syntax/types.html#syntax-tagtype}{\mathit{tagtype}}} \\\end{array}\end{split}\]

Thetype index of a tag must refer to afunction type that declares itstag type.

Tags are referenced throughtag indices,starting with the smallest index not referencing a tagimport.

Globals

The\({\href{../syntax/modules.html#syntax-global}{\mathit{global}}}\) section of a module defines a list ofglobal variables (orglobals for short):

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-global}{\mathit{global}}} & ::= & \href{../syntax/modules.html#syntax-global}{\mathsf{global}}~{\href{../syntax/types.html#syntax-globaltype}{\mathit{globaltype}}}~{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}} \\\end{array}\end{split}\]

Each global stores a single value of the type specified in theglobal type.It also specifies whether a global is immutable or mutable.Moreover, each global is initialized with a value given by aconstant initializerexpression.

Globals are referenced throughglobal indices,starting with the smallest index not referencing a globalimport.

Memories

The\({\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}}\) section of a module defines a list oflinear memories (ormemories for short) as described by theirmemory type:

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-mem}{\mathit{mem}}} & ::= & \href{../syntax/modules.html#syntax-mem}{\mathsf{memory}}~{\href{../syntax/types.html#syntax-memtype}{\mathit{memtype}}} \\\end{array}\end{split}\]

A memory is a list of raw uninterpreted bytes.The minimum size in thelimits of itsmemory type specifies the initial size of that memory, while its maximum, if present, restricts the size to which it can grow later.Both are in units ofpage size.

Memories can be initialized throughdata segments.

Memories are referenced throughmemory indices,starting with the smallest index not referencing a memoryimport.Most constructs implicitly reference memory index\(0\).

Tables

The\({\href{../syntax/modules.html#syntax-table}{\mathit{table}}}\) section of a module defines a list oftables described by theirtable type:

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-table}{\mathit{table}}} & ::= & \href{../syntax/modules.html#syntax-table}{\mathsf{table}}~{\href{../syntax/types.html#syntax-tabletype}{\mathit{tabletype}}}~{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}} \\\end{array}\end{split}\]

A table is an array of opaque values of a particularreference type that is specified by thetable type.Each table slot is initialized with a value given by aconstant initializerexpression.Tables can further be initialized throughelement segments.

The minimum size in thelimits of the table type specifies the initial size of that table, while its maximum restricts the size to which it can grow later.

Tables are referenced throughtable indices,starting with the smallest index not referencing a tableimport.Most constructs implicitly reference table index\(0\).

Functions

The\({\href{../syntax/modules.html#syntax-func}{\mathit{func}}}\) section of a module defines a list offunctions with the following structure:

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-func}{\mathit{func}}} & ::= & \href{../syntax/modules.html#syntax-func}{\mathsf{func}}~{\href{../syntax/modules.html#syntax-typeidx}{\mathit{typeidx}}}~{{\href{../syntax/modules.html#syntax-local}{\mathit{local}}}^\ast}~{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}} \\& {\href{../syntax/modules.html#syntax-local}{\mathit{local}}} & ::= & \href{../syntax/modules.html#syntax-local}{\mathsf{local}}~{\href{../syntax/types.html#syntax-valtype}{\mathit{valtype}}} \\\end{array}\end{split}\]

Thetype index of a function declares its signature by reference to afunction type defined in the module.The parameters of the function are referenced through 0-basedlocal indices in the function’s body; they are mutable.

The locals declare a list of mutable local variables and their types.These variables are referenced throughlocal indices in the function’s body.The index of the first local is the smallest index not referencing a parameter.

A function’sexpression is aninstruction sequence that represents the body of the function.Upon termination it must produce a stack matching the function type’sresult type.

Functions are referenced throughfunction indices,starting with the smallest index not referencing a functionimport.

Data Segments

The\({\href{../syntax/modules.html#syntax-data}{\mathit{data}}}\) section of a module defines a list ofdata segments,which can be used to initialize a range of memory from a staticlist ofbytes.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-data}{\mathit{data}}} & ::= & \href{../syntax/modules.html#syntax-data}{\mathsf{data}}~{{\href{../syntax/values.html#syntax-byte}{\mathit{byte}}}^\ast}~{\href{../syntax/modules.html#syntax-datamode}{\mathit{datamode}}} \\& {\href{../syntax/modules.html#syntax-datamode}{\mathit{datamode}}} & ::= & \href{../syntax/modules.html#syntax-datamode}{\mathsf{active}}~{\href{../syntax/modules.html#syntax-memidx}{\mathit{memidx}}}~{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}} ~~|~~ \href{../syntax/modules.html#syntax-datamode}{\mathsf{passive}} \\\end{array}\end{split}\]

Similar to element segments, data segments have a mode that identifies them as eitheractive orpassive.A passive data segment’s contents can be copied into a memory using the\(\mathsf{memory{.}init}\) instruction.An active data segment copies its contents into a memory duringinstantiation, as specified by amemory index and aconstantexpression defining an offset into that memory.

Data segments are referenced throughdata indices.

Element Segments

The\({\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}}\) section of a module defines a list ofelement segments,which can be used to initialize a subrange of a table from a staticlist of elements.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-elem}{\mathit{elem}}} & ::= & \href{../syntax/modules.html#syntax-elem}{\mathsf{elem}}~{\href{../syntax/types.html#syntax-reftype}{\mathit{reftype}}}~{{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}}^\ast}~{\href{../syntax/modules.html#syntax-elemmode}{\mathit{elemmode}}} \\& {\href{../syntax/modules.html#syntax-elemmode}{\mathit{elemmode}}} & ::= & \href{../syntax/modules.html#syntax-elemmode}{\mathsf{active}}~{\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}}}~{\href{../syntax/instructions.html#syntax-expr}{\mathit{expr}}} ~~|~~ \href{../syntax/modules.html#syntax-elemmode}{\mathsf{passive}} ~~|~~ \href{../syntax/modules.html#syntax-elemmode}{\mathsf{declare}} \\\end{array}\end{split}\]

Each element segment defines areference type and a corresponding list ofconstant elementexpressions.

Element segments have a mode that identifies them as eitheractive,passive, ordeclarative.A passive element segment’s elements can be copied to a table using the\(\mathsf{table{.}init}\) instruction.An active element segment copies its elements into a table duringinstantiation, as specified by atable index and aconstantexpression defining an offset into that table.A declarative element segment is not available at runtime but merely serves to forward-declare references that are formed in code with instructions like\(\mathsf{ref{.}func}\).The offset is given by anotherconstantexpression.

Element segments are referenced throughelement indices.

Start Function

The\({\href{../syntax/modules.html#syntax-start}{\mathit{start}}}\) section of a module declares thefunction index of astart function that is automatically invoked when the module isinstantiated, aftertables andmemories have been initialized.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-start}{\mathit{start}}} & ::= & \href{../syntax/modules.html#syntax-start}{\mathsf{start}}~{\href{../syntax/modules.html#syntax-funcidx}{\mathit{funcidx}}} \\\end{array}\end{split}\]

Note

The start function is intended for initializing the state of a module.The module and its exports are not accessible externally before this initialization has completed.

Imports

The\({\href{../syntax/modules.html#syntax-import}{\mathit{import}}}\) section of a module defines a set ofimports that are required forinstantiation.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-import}{\mathit{import}}} & ::= & \href{../syntax/modules.html#syntax-import}{\mathsf{import}}~{\href{../syntax/values.html#syntax-name}{\mathit{name}}}~{\href{../syntax/values.html#syntax-name}{\mathit{name}}}~{\href{../syntax/types.html#syntax-externtype}{\mathit{externtype}}} \\\end{array}\end{split}\]

Each import is labeled by a two-levelname space, consisting of amodule name and anitem name for an entity within that module.Importable definitions aretags,globals,memories,tables, andfunctions.Each import is specified by a respectiveexternal type that a definition provided during instantiation is required to match.

Every import defines an index in the respectiveindex space.In each index space, the indices of imports go before the first index of any definition contained in the module itself.

Note

Unlike export names, import names are not necessarily unique.It is possible to import the same module/item name pair multiple times;such imports may even have different type descriptions, including different kinds of entities.A module with such imports can still be instantiated depending on the specifics of how anembedder allows resolving and supplying imports.However, embedders are not required to support such overloading,and a WebAssembly module itself cannot implement an overloaded name.

Exports

The\({\href{../syntax/modules.html#syntax-export}{\mathit{export}}}\) section of a module defines a set ofexports that become accessible to the host environment once the module has beeninstantiated.

\[\begin{split}\begin{array}[t]{@{}l@{}rrl@{}l@{}}& {\href{../syntax/modules.html#syntax-export}{\mathit{export}}} & ::= & \href{../syntax/modules.html#syntax-export}{\mathsf{export}}~{\href{../syntax/values.html#syntax-name}{\mathit{name}}}~{\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}} \\[0.8ex]& {\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}} & ::= & \href{../syntax/modules.html#syntax-externidx}{\mathsf{func}}~{\href{../syntax/modules.html#syntax-funcidx}{\mathit{funcidx}}} ~~|~~ \href{../syntax/modules.html#syntax-externidx}{\mathsf{global}}~{\href{../syntax/modules.html#syntax-globalidx}{\mathit{globalidx}}} ~~|~~ \href{../syntax/modules.html#syntax-externidx}{\mathsf{table}}~{\href{../syntax/modules.html#syntax-tableidx}{\mathit{tableidx}}} ~~|~~ \href{../syntax/modules.html#syntax-externidx}{\mathsf{memory}}~{\href{../syntax/modules.html#syntax-memidx}{\mathit{memidx}}} ~~|~~ \href{../syntax/modules.html#syntax-externidx}{\mathsf{tag}}~{\href{../syntax/modules.html#syntax-tagidx}{\mathit{tagidx}}} \\\end{array}\end{split}\]

Each export is labeled by a uniquename.Exportable definitions aretags,globals,memories,tables, andfunctions,which are referenced through a respective index.

Conventions

The following auxiliary notation is defined for sequences of exports, filtering out indices of a specific kind in an order-preserving fashion:

\[\begin{split}\begin{array}[t]{@{}lcl@{}l@{}}{\href{../syntax/modules.html#syntax-externidx}{\mathrm{funcs}}}(\epsilon) & = & \epsilon \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{funcs}}}((\href{../syntax/modules.html#syntax-externidx}{\mathsf{func}}~x)~{{\mathit{xx}}^\ast}) & = & x~{\href{../syntax/modules.html#syntax-externidx}{\mathrm{funcs}}}({{\mathit{xx}}^\ast}) \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{funcs}}}({\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}}~{{\mathit{xx}}^\ast}) & = & {\href{../syntax/modules.html#syntax-externidx}{\mathrm{funcs}}}({{\mathit{xx}}^\ast}) & \quad \mbox{otherwise} \\[0.8ex]{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tables}}}(\epsilon) & = & \epsilon \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tables}}}((\href{../syntax/modules.html#syntax-externidx}{\mathsf{table}}~x)~{{\mathit{xx}}^\ast}) & = & x~{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tables}}}({{\mathit{xx}}^\ast}) \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tables}}}({\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}}~{{\mathit{xx}}^\ast}) & = & {\href{../syntax/modules.html#syntax-externidx}{\mathrm{tables}}}({{\mathit{xx}}^\ast}) & \quad \mbox{otherwise} \\[0.8ex]{\href{../syntax/modules.html#syntax-externidx}{\mathrm{mems}}}(\epsilon) & = & \epsilon \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{mems}}}((\href{../syntax/modules.html#syntax-externidx}{\mathsf{memory}}~x)~{{\mathit{xx}}^\ast}) & = & x~{\href{../syntax/modules.html#syntax-externidx}{\mathrm{mems}}}({{\mathit{xx}}^\ast}) \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{mems}}}({\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}}~{{\mathit{xx}}^\ast}) & = & {\href{../syntax/modules.html#syntax-externidx}{\mathrm{mems}}}({{\mathit{xx}}^\ast}) & \quad \mbox{otherwise} \\[0.8ex]{\href{../syntax/modules.html#syntax-externidx}{\mathrm{globals}}}(\epsilon) & = & \epsilon \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{globals}}}((\href{../syntax/modules.html#syntax-externidx}{\mathsf{global}}~x)~{{\mathit{xx}}^\ast}) & = & x~{\href{../syntax/modules.html#syntax-externidx}{\mathrm{globals}}}({{\mathit{xx}}^\ast}) \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{globals}}}({\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}}~{{\mathit{xx}}^\ast}) & = & {\href{../syntax/modules.html#syntax-externidx}{\mathrm{globals}}}({{\mathit{xx}}^\ast}) & \quad \mbox{otherwise} \\[0.8ex]{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tags}}}(\epsilon) & = & \epsilon \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tags}}}((\href{../syntax/modules.html#syntax-externidx}{\mathsf{tag}}~x)~{{\mathit{xx}}^\ast}) & = & x~{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tags}}}({{\mathit{xx}}^\ast}) \\{\href{../syntax/modules.html#syntax-externidx}{\mathrm{tags}}}({\href{../syntax/modules.html#syntax-externidx}{\mathit{externidx}}}~{{\mathit{xx}}^\ast}) & = & {\href{../syntax/modules.html#syntax-externidx}{\mathrm{tags}}}({{\mathit{xx}}^\ast}) & \quad \mbox{otherwise} \\\end{array}\end{split}\]