wasi_preview3 (wasm v0.16.0)
View SourceThe WASI 0.3 host: wasi:cli, wasi:clocks, wasi:random, and
wasi:filesystem, wasi:sockets and wasi:http through
wasi_p3_filesystem, wasi_p3_sockets and wasi_p3_http.
WASI 0.3 replaces wasi:io's streams and pollables with the Component Model's
own stream<T>, future<T> and async func. Each interface here is keyed
Interface@0.3, so it serves every 0.3.x import and never answers a 0.2 one
(see wasm_component's import resolution). A 0.3 program built through the
wasip1 adapter imports both versions, so command/1 is the 0.2 host and this
one together, and run_command/3 runs a 0.2 or a 0.3 command.
{ok, #{stdout := Out, exit_code := 0}} =
wasi_preview3:run_command(Bin, <<>>, #{args => [~"prog"]}).- stdout and stderr:
write-via-streamtakes the guest's stream and passes every write to the sink as it happens (wasm_async:host_sink/2). - stdin:
read-via-streamgives the guest a stream that a producer fills with the input. wait-forandwait-untilare async imports: the guest's task waits on a timer while the instance's other tasks run.
Summary
Functions
wasi:cli at 0.3: stdio through streams, the environment, exit, terminals.
wasi:clocks at 0.3: the monotonic clock with async wait-for and
wait-until, the system clock, and a timezone that reports UTC.
Every import a wasi:cli/command needs at 0.2 and at 0.3: the 0.2 host of
wasi_preview2:command/1 with cli, clocks and random at 0.3 beside it. Opts
is as for wasi_preview2:command/1.
As wasi_preview2:host_remake/1: the 0.3 host's preopens are 0.2's.
wasi:random at 0.3: the same functions as 0.2.
Run a command component, 0.2 or 0.3, as wasi_preview2:run_command/3 does.
A 0.3 wasi:cli/run export is async, and its task is run to its end.
Serve one request through a wasi:http handler, 0.2 or 0.3, as
wasi_preview2:run_serve/3 does. A 0.3 handler is async: its response is
taken once it has one, and its body read while the task writes it; what the
guest does after that runs too, before the instance is destroyed.
As run_serve/3, with the imports serve_imports/2 built: a server that
serves many requests builds them once, not once per request.
The imports run_serve/4 serves component Bin with, under the options
Extra (as for run_serve/3): the 0.2 host, and the 0.3 host too when the
component names a 0.3 interface. Nothing in them belongs to one request.
The WASI 0.2 and 0.3 interfaces this host serves, declared for
wasm_component:snapshot/2, as wasi_preview2:snapshot_hooks/0 declares the
0.2 ones. A 0.3 stdio stream is a stream end, not a host entry: a guest that
holds one after init leaves a task frame open, and the capture is refused.
Types
Functions
wasi:cli at 0.3: stdio through streams, the environment, exit, terminals.
-spec clocks() -> host().
wasi:clocks at 0.3: the monotonic clock with async wait-for and
wait-until, the system clock, and a timezone that reports UTC.
Every import a wasi:cli/command needs at 0.2 and at 0.3: the 0.2 host of
wasi_preview2:command/1 with cli, clocks and random at 0.3 beside it. Opts
is as for wasi_preview2:command/1.
As wasi_preview2:host_remake/1: the 0.3 host's preopens are 0.2's.
-spec random() -> host().
wasi:random at 0.3: the same functions as 0.2.
-spec run_command(component(), binary(), map()) -> {ok, #{stdout := binary(), stderr := binary(), exit_code := integer()}} | {error, term()}.
Run a command component, 0.2 or 0.3, as wasi_preview2:run_command/3 does.
A 0.3 wasi:cli/run export is async, and its task is run to its end.
-spec run_serve(component(), map(), map()) -> serve_result().
Serve one request through a wasi:http handler, 0.2 or 0.3, as
wasi_preview2:run_serve/3 does. A 0.3 handler is async: its response is
taken once it has one, and its body read while the task writes it; what the
guest does after that runs too, before the instance is destroyed.
A 0.3 request can carry trailers, a header list, which the handler reads
from the request's trailers future. A 0.3 response that sets trailers answers
{ok, Status, Headers, Body, Trailers}; one that sets none answers the
4-tuple.
-spec run_serve(component(), map(), map(), map()) -> serve_result().
As run_serve/3, with the imports serve_imports/2 built: a server that
serves many requests builds them once, not once per request.
Imports = wasi_preview3:serve_imports(Bin, #{}),
{ok, 200, _, _} = wasi_preview3:run_serve(Bin, Request, #{}, Imports).
The imports run_serve/4 serves component Bin with, under the options
Extra (as for run_serve/3): the 0.2 host, and the 0.3 host too when the
component names a 0.3 interface. Nothing in them belongs to one request.
-spec snapshot_hooks() -> #{binary() => stateless}.
The WASI 0.2 and 0.3 interfaces this host serves, declared for
wasm_component:snapshot/2, as wasi_preview2:snapshot_hooks/0 declares the
0.2 ones. A 0.3 stdio stream is a stream end, not a host entry: a guest that
holds one after init leaves a task frame open, and the capture is refused.