wasm_tools_component (wasm_tools v0.1.0)
View SourceRead, load and hold a component, and name its exports as tools.
This module is internal: you reach it through wasm_tools, and it can change
in any release. The README lists the supported API.
inspect/1 and load/2 read the bytes in a process of their own, with a size
cap, a heap limit and a timeout, so a hostile or broken component cannot hurt
the caller. They take the component's interface from erlang_wasm's
wasm_component:describe/1: nothing guest-side runs.
A loaded component is a value. load/2 also starts a holder: a small process
that keeps the component's compiled code in erlang_wasm's module cache
(wasm_component:hold/1), so each call does not compile it again, and that
belongs to the process that loaded it, as an ETS table does. The holder stops
on unload/1 or when its owner exits; after that a call answers
#{class := link, kind := unloaded}.
Naming, from the export alone:
| export | tool name |
|---|---|
add, a function of the world | add |
example:calc/ops@1.0.0#add, in an interface | ops_add |
A name matches ^[A-Za-z0-9_-]{1,64}$. Exports whose names collide (two
interfaces named ops) get no name: a native call reaches them by their
export name, and a registry or MCP names them among the exports you select.
Exports that cannot be called with a fresh instance per call are listed with
callable => {false, Why}: resource constructors and methods (a handle dies
with its instance), async functions, and any signature holding a resource, a
stream, a future or an error context.
Summary
Functions
The tool name an export gets from itself alone, before collisions are looked
at: undefined when it is not a valid name.
The definition of one export, by tool name or by export name.
Every export of a component, as a definition.
The component a holder keeps, so that a small reference (its pid) can stand
for the whole component, as MCP's tool data does. unloaded once the holder
has stopped.
What a component exports and imports, read without running guest code.
Whether a term is a loaded component.
Load a component: read it, check its imports against what the policy and the host interfaces provide, and start its holder.
The same component, with a holder of its own that belongs to Owner: what a
registry keeps, so it does not depend on whoever loaded the component.
The exports picked (all callable ones, or a list of export or tool names),
each with the name it gets under Namespace (undefined for none): the base
name prefixed with Namespace_. Names are computed among the exports picked;
two with the same name, or one with no valid name, are a name_conflict.
What wasm_tools_call needs to call one export: the prepared component, the
definition, the policy, and the host imports of the call.
Stop a component's holder. Calls to it then answer unloaded.
Types
Functions
The tool name an export gets from itself alone, before collisions are looked
at: undefined when it is not a valid name.
The definition of one export, by tool name or by export name.
Every export of a component, as a definition.
The component a holder keeps, so that a small reference (its pid) can stand
for the whole component, as MCP's tool data does. unloaded once the holder
has stopped.
What a component exports and imports, read without running guest code.
#{digest, size, exports, imports}: each export with its tool name
(undefined when it collides), export, interface, func, kind,
params, result and callable; each import with its interface, funcs
and provider: wasi (the WASI 0.2 host provides it), network (wasi:sockets,
always given, reaching nothing without the network capability) or
application (a host interface you give at load).
Whether a term is a loaded component.
Load a component: read it, check its imports against what the policy and the host interfaces provide, and start its holder.
Options: capabilities and limits (wasm_tools_policy), host (host
interfaces, wasm_tools_host), meta (per tool name or export name: title,
description, annotations), owner (the process the holder belongs to,
the caller by default), observer, sha256 (the expected digest, hex), and
max_bytes.
-spec observer(component()) -> wasm_tools_call:observer() | undefined.
-spec policy(component()) -> wasm_tools_policy:policy().
The same component, with a holder of its own that belongs to Owner: what a
registry keeps, so it does not depend on whoever loaded the component.
-spec select(component(), all | [binary()], binary() | undefined) -> {ok, [{binary(), def()}]} | {error, error()}.
The exports picked (all callable ones, or a list of export or tool names),
each with the name it gets under Namespace (undefined for none): the base
name prefixed with Namespace_. Names are computed among the exports picked;
two with the same name, or one with no valid name, are a name_conflict.
-spec tool(component(), def(), binary()) -> {ok, wasm_tools_call:tool()} | {error, error()}.
What wasm_tools_call needs to call one export: the prepared component, the
definition, the policy, and the host imports of the call.
-spec unload(component()) -> ok.
Stop a component's holder. Calls to it then answer unloaded.