Call a component

View Source

This page is about loading one component and calling its functions from Erlang: the values you pass and get back, the errors, the deadline and the cancel. You need nothing else for a single component; Registries are for several.

Load it

{ok, _} = application:ensure_all_started(wasm_tools),
{ok, Data} = wasm_tools:load({file, "test/fixtures/component/data_transform.component.wasm"}).

load/2 takes options:

optionwhat it does
capabilitieswhat the component may reach, nothing by default (Capabilities)
limitsfuel, deadline, memory, heap; untrusted by default
hostfunctions of your application it imports (Host interfaces)
metatitles and descriptions of its tools, for MCP
ownerthe process the component belongs to, the caller by default
observera fun told about the load and each call (Observability)
sha256the digest the bytes must have, in hex
max_bytesthe largest component accepted, 64 MiB by default

A source is {file, Path} or {binary, Bytes}. To load from anywhere else, read the bytes yourself and pass {binary, Bytes} with its sha256.

Name a function

A function of the world is named as in its WIT (add). A function of an exported interface is named after the interface: ops_add for add in example:calc/ops@1.0.0. inspect/1 and list/1 show every name; when two names collide, the functions keep only their export name (example:alpha/ops@1.0.0#run), which a call also accepts.

Pass values

Arguments are a map keyed by parameter name. A missing option parameter is none.

WITErlang
booltrue, false
s8 to s64, u8 to u64an integer
f32, f64a float
charan integer, the code point
stringa UTF-8 binary
list<u8>a binary
list<T>a list
recorda map with binary keys
tuplea tuple
variant{Case, Payload}, or {Case, undefined} for a case with no payload
enuma binary
flagsa list of binaries
option<T>none or {some, V}
result<T, E>{ok, V} or {error, E}
wasm_tools:call(Data, <<"area">>, #{<<"s">> => {<<"rect">>, {3.0, 4.0}},
                                    <<"u">> => <<"metric">>}).
%% => {ok, 12.0}
wasm_tools:call(Data, <<"grant">>, #{<<"current">> => [<<"exec">>],
                                     <<"extra">> => [<<"read">>]}).
%% => {ok, [<<"read">>, <<"exec">>]}

Read results

A function that returns result<T, E> answers {ok, {ok, T}} or {ok, {error, E}}: the call worked, and the function said which.

Person = #{<<"name">> => <<" ">>, <<"age">> => 3, <<"email">> => none, <<"tags">> => []},
wasm_tools:call(Data, <<"normalize">>, #{<<"p">> => Person}).
%% => {ok, {error, {<<"empty-name">>, undefined}}}

Handle errors

Every error is a map #{class, kind, msg, ctx}:

classwhen
invalidthe component, an export or an option cannot be used as asked
linksomething is missing: an import, a tool, a loaded component
argumentan argument does not fit its type; ctx holds its path
trapthe guest or a host function failed
exhaustiona limit was reached: fuel, memory, heap, host calls
timeoutthe call ran past its deadline
cancelledthe call was cancelled, or its caller or registry went away
overloadeda registry runs as many calls as it may, or the node loads no more modules this second
wasm_tools:call(Data, <<"normalize">>,
                #{<<"p">> => Person#{<<"age">> => 300}}).
%% => {error, #{class := argument, ctx := #{path := [<<"p">>, <<"age">>]}}}

Bound a call

Each call runs in a fresh instance, in a process of its own, with the component's limits. A call can lower them, never raise them:

{ok, Limits} = wasm_tools:load({file, "test/fixtures/component/limits.component.wasm"},
                               #{limits => #{fuel => infinity, timeout => 5000}}),
wasm_tools:call(Limits, <<"forever">>, #{}, #{timeout => 50}).
%% => {error, #{class := timeout, kind := deadline}}

Start, wait, cancel

start_call/4 returns at once. The process that started the call waits for it with await/2 or stops it with cancel/1:

{ok, Job} = wasm_tools:start_call(Limits, <<"forever">>, #{}, #{}),
{error, #{class := timeout, kind := await}} = wasm_tools:await(Job, 20),
wasm_tools:cancel(Job).
%% => ok
  • await/2 with a timeout that runs out leaves the call running: wait again or cancel it.
  • cancel/1 returns once the call's process is gone and leaves no message behind. A cancelled call is not awaited.
  • If the process that started a call exits, the call stops.

Describe and check

{ok, #{params := Params}} = wasm_tools:describe(Data, <<"split">>),
ok = wasm_tools:check(Data, <<"split">>, #{<<"input">> => <<"a,b">>, <<"sep">> => $,}),
Params.
%% => [{<<"input">>, string}, {<<"sep">>, char}]
ok = wasm_tools:unload(Data),
ok = wasm_tools:unload(Limits).

Notes

  • A component is a value: pass it to any process. Its compiled code is kept while the process that loaded it lives (Lifecycle).
  • Resources, async functions and streams cannot be called with a fresh instance per call; inspect/1 lists them with callable => {false, Why}. Use erlang_wasm's wasm_component for those.