wasm_tools_component (wasm_tools v0.1.0)

View Source

Read, 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:

exporttool name
add, a function of the worldadd
example:calc/ops@1.0.0#add, in an interfaceops_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

component()

-opaque component()

def()

-type def() ::
          #{name := binary() | undefined,
            export := binary(),
            interface := binary() | none,
            func := binary(),
            kind := func | constructor | method | static,
            params := [{binary(), term()}],
            result := term(),
            callable := true | {false, atom()},
            meta := map()}.

error()

-type error() :: #{class := atom(), kind := atom(), msg := binary(), ctx := map()}.

source()

-type source() :: {file, file:filename_all()} | {binary, binary()}.

Functions

base_name(Def)

-spec base_name(def()) -> binary() | undefined.

The tool name an export gets from itself alone, before collisions are looked at: undefined when it is not a valid name.

def/2

-spec def(component(), binary()) -> {ok, def()} | {error, error()}.

The definition of one export, by tool name or by export name.

defs/1

-spec defs(component()) -> [def()].

Every export of a component, as a definition.

digest/1

-spec digest(component()) -> binary().

from_holder(Holder)

-spec from_holder(pid()) -> {ok, component()} | {error, error()}.

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.

holder/1

-spec holder(component()) -> pid().

inspect(Source)

-spec inspect(source()) -> {ok, map()} | {error, error()}.

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).

is_component/1

-spec is_component(term()) -> boolean().

Whether a term is a loaded component.

load/2

-spec load(source(), map()) -> {ok, component()} | {error, error()}.

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.

observer/1

-spec observer(component()) -> wasm_tools_call:observer() | undefined.

policy/1

-spec policy(component()) -> wasm_tools_policy:policy().

rehold/2

-spec rehold(component(), pid()) -> {ok, component()} | {error, error()}.

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.

select(Component, Exports, Namespace)

-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.

tool/3

-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.

unload/1

-spec unload(component()) -> ok.

Stop a component's holder. Calls to it then answer unloaded.