wasi_preview3 (wasm v0.16.0)

View Source

The 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-stream takes the guest's stream and passes every write to the sink as it happens (wasm_async:host_sink/2).
  • stdin: read-via-stream gives the guest a stream that a producer fills with the input.
  • wait-for and wait-until are 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

component()

-type component() :: binary() | wasm_component:prepared().

fields()

-type fields() :: [{binary(), binary()}].

host()

-type host() :: #{{binary(), binary()} => term()}.

serve_result()

-type serve_result() ::
          {ok, 0..65535, fields(), binary()} |
          {ok, 0..65535, fields(), binary(), Trailers :: fields()} |
          {error, term()}.

Functions

cli(Opts)

-spec cli(map()) -> host().

wasi:cli at 0.3: stdio through streams, the environment, exit, terminals.

clocks()

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

command(Opts)

-spec command(map()) -> host().

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.

host_remake(Opts)

-spec host_remake(map()) -> fun((term()) -> {ok, atom(), term()} | {error, atom()}).

As wasi_preview2:host_remake/1: the 0.3 host's preopens are 0.2's.

random()

-spec random() -> host().

wasi:random at 0.3: the same functions as 0.2.

run_command(Bin, Stdin, Extra)

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

run_serve(Bin, Request, Extra)

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

run_serve(Bin, Request, Extra, Imports)

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

serve_imports(Bin, Extra)

-spec serve_imports(component(), map()) -> map().

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.

snapshot_hooks()

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