wasi (wasm v0.1.0)
View SourceRun a WASI command module and get its exit status and output back.
Use this when your guest is a program with a main rather than a library of
exported functions. A _start module reports its exit status by trapping and
writes its output to whatever sink your capability map names; collecting both is
mechanical and easy to get subtly wrong, so it lives here rather than in every
caller.
{ok, Mod} = wasm:load_file("hello.wasm"),
{ok, 0, Stdout, _Stderr} =
wasi:run(Mod, #{args => [~"hello"],
dirs => [{~"/data", "/srv/app/data", read}]}).wasi_preview1 documents the capabilities. The short version: leave a key out
and the module does not have that capability, and no dirs means no filesystem
at all rather than one rooted at your working directory.
Summary
Functions
Build the WASI import map for a capability configuration.
Instantiate a module, run _start, and collect its exit status and output.
Functions
Build the WASI import map for a capability configuration.
-spec run(wasm:module_()) -> {ok, integer(), binary(), binary()} | {error, term()}.
Instantiate a module, run _start, and collect its exit status and output.
You get the exit status back from the trap proc_exit raises, because trapping
is the only way to unwind a WebAssembly stack. A module that returns from
_start without calling proc_exit exits 0, which is what the WASI command
model specifies.
Output is captured by pointing stdout and stderr at this process and draining
the mailbox, so any stdout or stderr you put in Config is overridden.