Call a component
View SourceThis 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:
| option | what it does |
|---|---|
capabilities | what the component may reach, nothing by default (Capabilities) |
limits | fuel, deadline, memory, heap; untrusted by default |
host | functions of your application it imports (Host interfaces) |
meta | titles and descriptions of its tools, for MCP |
owner | the process the component belongs to, the caller by default |
observer | a fun told about the load and each call (Observability) |
sha256 | the digest the bytes must have, in hex |
max_bytes | the 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.
| WIT | Erlang |
|---|---|
bool | true, false |
s8 to s64, u8 to u64 | an integer |
f32, f64 | a float |
char | an integer, the code point |
string | a UTF-8 binary |
list<u8> | a binary |
list<T> | a list |
| record | a map with binary keys |
| tuple | a tuple |
| variant | {Case, Payload}, or {Case, undefined} for a case with no payload |
| enum | a binary |
| flags | a 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}:
| class | when |
|---|---|
invalid | the component, an export or an option cannot be used as asked |
link | something is missing: an import, a tool, a loaded component |
argument | an argument does not fit its type; ctx holds its path |
trap | the guest or a host function failed |
exhaustion | a limit was reached: fuel, memory, heap, host calls |
timeout | the call ran past its deadline |
cancelled | the call was cancelled, or its caller or registry went away |
overloaded | a 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).
%% => okawait/2with a timeout that runs out leaves the call running: wait again or cancel it.cancel/1returns 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/1lists them withcallable => {false, Why}. Use erlang_wasm'swasm_componentfor those.