Modules¶
WebAssembly programs are organized into modules, which are the unit of deployment, loading, and compilation. A module collects definitions for types, functions, tables, memories, and globals. In addition, it can declare imports and exports and provide initialization in the form of data and element segments, or a start function.
Each of the vectors – and thus the entire module – may be empty.
Indices¶
Definitions are referenced with zero-based indices. Each class of definition has its own index space, as distinguished by the following classes.
The index space for functions, tables, memories and globals includes respective imports declared in the same module. The indices of these imports precede the indices of other definitions in the same index space.
Element indices reference element segments and data indices reference data segments.
The index space for locals is only accessible inside a function and includes the parameters of that function, which precede the local variables.
Label indices reference structured control instructions inside an instruction sequence.
Conventions¶
The meta variable
ranges over label indices.The meta variables
range over indices in any of the other index spaces.The notation
denotes the set of indices from index space occurring free in . Sometimes this set is reinterpreted as the vector of its elements.
Note
For example, if
Types¶
The
All function types used in a module must be defined in this component. They are referenced by type indices.
Note
Future versions of WebAssembly may add additional forms of type definitions.
Functions¶
The
The
The
The
Functions are referenced through function indices, starting with the smallest index not referencing a function import.
Tables¶
The
A table is a vector of opaque values of a particular reference type.
The
Tables can be initialized through element segments.
Tables are referenced through table indices,
starting with the smallest index not referencing a table import.
Most constructs implicitly reference table index
Memories¶
The
A memory is a vector of raw uninterpreted bytes.
The
Memories can be initialized through data segments.
Memories are referenced through memory indices,
starting with the smallest index not referencing a memory import.
Most constructs implicitly reference memory index
Note
In the current version of WebAssembly, at most one memory may be defined or imported in a single module,
and all constructs implicitly reference this memory
Globals¶
The
Each global stores a single value of the given global type.
Its
Globals are referenced through global indices, starting with the smallest index not referencing a global import.
Element Segments¶
The initial contents of a table is uninitialized. Element segments can be used to initialize a subrange of a table from a static vector of elements.
The
Element segments have a mode that identifies them as either passive, active, or declarative.
A passive element segment’s elements can be copied to a table using the
The
Element segments are referenced through element indices.
Data Segments¶
The initial contents of a memory are zero bytes. Data segments can be used to initialize a range of memory from a static vector of bytes.
The
Like element segments, data segments have a mode that identifies them as either passive or active.
A passive data segment’s contents can be copied into a memory using the
Data segments are referenced through data indices.
Note
In the current version of WebAssembly, at most one memory is allowed in a module.
Consequently, the only valid
Start Function¶
The
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.
Exports¶
The
Each export is labeled by a unique name. Exportable definitions are functions, tables, memories, and globals, which are referenced through a respective descriptor.
Conventions¶
The following auxiliary notation is defined for sequences of exports, filtering out indices of a specific kind in an order-preserving fashion:
Imports¶
The
Each import is labeled by a two-level name space, consisting of a
Every import defines an index in the respective index 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