wasm_component_types (wasm v0.16.0)
View SourceDecode a component's type section into wasm_canon value descriptors.
Composition needs the signature of each function an interface offers: to bridge one
component's import onto another's export, the runtime lifts and lowers the call, and that
needs the parameter and result descriptors. Nothing else derives a signature from the
binary (a direct component call takes the signature from its caller), so this module reads
the component type section (0x07) and the import section (0x0a) and resolves, for each
imported interface, its functions' signatures.
It decodes the primitives, the aggregates (list, record, tuple, variant, enum,
flags, option, result), resource handles and the async future, stream and
error-context; any other byte is read as an index into the type space.
wasm_component:describe/1 reads the same types with their names kept:
func_type/2 for a function type, with its parameter names, and imports/2
for the imported interfaces in the order the binary lists them.
Summary
Functions
For a component of only types, imports and exports, as wit-component writes to
export an interface: each exported function that is a function it imports,
[{ExportName, ImportName}] in the order it exports them. A component with
anything else gives [].
The signature of function type Idx in a type space from type_space/1, every
type reference in it resolved, or error when Idx is not a function type.
As func_sig/2, with the parameter names, the whole result and whether the
type is async, for wasm_component:describe/1.
The imported interfaces of a component and each of their functions' signatures.
The component's imports in the order its import sections list them, each
with its functions, for wasm_component:describe/1. An interface lists its
functions in the order its type declares them, each under the name a host
map keys it by ([method]descriptor.read); a function imported on its own
is listed alone, under its import name. Types is the component's type
space. A type import names no function and is left out.
An import, export or instance-export name: a kind byte, a u32 length, the bytes.
A name with no kind byte: a u32 length, then the bytes.
Each defined resource type's destructor, as #{TypeIndex => CoreFuncIndex},
from the component's section bytes or from a type space type_space/1
already built.
As type_space/1, for a reader that must not guess: a section this decoder
cannot read throws a malformed error with kind unsupported_type, which
wasm_error:capture/1 turns into a value.
The component's whole type index space, every type section included.
The value descriptor at type index Idx of a type_space/1 table, with every
type reference in it resolved; error when the index holds no value type.
Types
Functions
For a component of only types, imports and exports, as wit-component writes to
export an interface: each exported function that is a function it imports,
[{ExportName, ImportName}] in the order it exports them. A component with
anything else gives [].
-spec func_sig(non_neg_integer(), #{non_neg_integer() => typedef()}) -> {ok, sig()} | error.
The signature of function type Idx in a type space from type_space/1, every
type reference in it resolved, or error when Idx is not a function type.
-spec func_type(non_neg_integer(), #{non_neg_integer() => typedef()}) -> {ok, func_type()} | error.
As func_sig/2, with the parameter names, the whole result and whether the
type is async, for wasm_component:describe/1.
The imported interfaces of a component and each of their functions' signatures.
Sec is the component's section stream (the bytes after the 8-byte preamble). Returns
#{InterfaceName => #{FuncName => sig()}} for every import whose extern is an instance
(an interface). An import that is a bare function is returned under the interface name
with a single FuncName equal to the import name.
-spec imports(binary(), #{non_neg_integer() => typedef()}) -> [#{name := binary(), kind := interface | func, funcs := [#{func := binary(), params := [{binary(), valdesc()}], result := valdesc() | none | {named, [{binary(), valdesc()}]}, async := boolean()}]}].
The component's imports in the order its import sections list them, each
with its functions, for wasm_component:describe/1. An interface lists its
functions in the order its type declares them, each under the name a host
map keys it by ([method]descriptor.read); a function imported on its own
is listed alone, under its import name. Types is the component's type
space. A type import names no function and is left out.
An import, export or instance-export name: a kind byte, a u32 length, the bytes.
A name with no kind byte: a u32 length, then the bytes.
-spec resource_dtors(binary() | #{non_neg_integer() => typedef()}) -> #{non_neg_integer() => non_neg_integer()}.
Each defined resource type's destructor, as #{TypeIndex => CoreFuncIndex},
from the component's section bytes or from a type space type_space/1
already built.
A resource type may name a core function the runtime runs when an owned handle of
that type is dropped. The indices are those of type_space/1, the whole type
space, so a resource defined in a later type section is found under its real
index; reading only the first section missed it, and its destructor never ran.
A resource with no destructor, or a type that is not a resource, is omitted.
Never raises.
-spec strict_type_space(binary()) -> #{non_neg_integer() => typedef()}.
As type_space/1, for a reader that must not guess: a section this decoder
cannot read throws a malformed error with kind unsupported_type, which
wasm_error:capture/1 turns into a value.
-spec type_space(binary()) -> #{non_neg_integer() => typedef()}.
The component's whole type index space, every type section included.
A component's type indices are assigned, in section order, by every type
definition, every type imported, every type aliased and every type exported (an
export of a type introduces a new index for it). The signature of a lifted
function, and the resource type an own/borrow in it names, are indices into
this space, so a caller that checks resource handles against an export's real
signature needs all of it. An entry that is not a definition (an import, alias
or export) is other, but for a type imported or exported as equal to a value
type, which is that value type. Tolerant: a section this decoder cannot read
yields #{}, never a partial space whose indices would be wrong.
-spec value_desc(non_neg_integer(), #{non_neg_integer() => typedef()}) -> {ok, valdesc()} | error.
The value descriptor at type index Idx of a type_space/1 table, with every
type reference in it resolved; error when the index holds no value type.