WasiComponentContext Class

The environment of a WASI component (WASI 0.2 and WASI 0.3, https://github.com/WebAssembly/WASI): its arguments, environment variables, standard streams, clocks, random numbers, and the file system of its preopened directories. Define its imports (WasiComponentContext.DefineImports), instantiate the component, and run it (WasiComponentContext.Run).

public sealed class WasiComponentContext

Remarks

The imports are the interfaces of wasi:cli, wasi:io, wasi:clocks, wasi:random and wasi:filesystem, in version 0.2.12 and 0.3.2: a component that imports an older version of the same major version (0.2.x or 0.3.x) uses them too. The sockets and HTTP are not supported.

A context is the state of one program: use one context per instance, from the thread of the store. The asynchronous functions of WASI 0.3 and the threads of the components are not supported on the browser.

Example

using var runtime = new WasmRuntime();

// A component that exports wasi:cli/run, compiled from its binary (File.ReadAllBytes("app.wasm")) or its text
var component = runtime.CompileComponent(WasmRuntime.WatToWasm(text));

using var output = new MemoryStream();
var wasi = new WasiComponentContext(new WasiOptions
{
    Arguments = ["app.wasm"],
    StandardOutput = output,
});

using var store = runtime.CreateStore();
var instance = store.InstantiateComponent(component, wasi.DefineImports());
var exitCode = WasiComponentContext.Run(instance);

Assert.Equal(0, exitCode);
Assert.Equal("Hello, WASI!\n", Encoding.UTF8.GetString(output.ToArray()));

Constructors

NameDescription
WasiComponentContext(WasiOptions?)Creates the environment of a component.

WasiComponentContext(WasiOptions?)

WasiComponentContext.cs

Creates the environment of a component.

public WasiComponentContext(WasiOptions? options = null)

Parameters

options WasiOptions?
The options, or null for a program without arguments, environment variables, input, output and files.

Exceptions

ArgumentException
The name of an environment variable is empty or contains =.

Methods

NameDescription
DefineImports(ComponentImports?)Defines the interfaces of WASI 0.2 and WASI 0.3.
HandleHttpRequest(WasmComponentInstance, HttpRequestMessage, CancellationToken)Handles an HTTP request with a component of the world wasi:http/service: calls the function handle of its export wasi:http/handler (WASI 0.3), and returns its response once the component wrote its body. The component sends its own requests with WasiOptions.HttpClient.
Run(WasmComponentInstance)Runs a command: calls the function run of the export wasi:cli/run of the instance (WASI 0.3 when the component exports both versions), and returns the exit code of the program: 0 when run returns ok, 1 when it returns error, or the code given to wasi:cli/exit.

DefineImports(ComponentImports?)

WasiComponentContext.cs

Defines the interfaces of WASI 0.2 and WASI 0.3.

public ComponentImports DefineImports(ComponentImports? imports = null)

Parameters

imports ComponentImports?
The imports where the interfaces are defined, or null to create them.

Returns

ComponentImports The imports.

HandleHttpRequest(WasmComponentInstance, HttpRequestMessage, CancellationToken)

WasiComponentContext.cs

Handles an HTTP request with a component of the world wasi:http/service: calls the function handle of its export wasi:http/handler (WASI 0.3), and returns its response once the component wrote its body. The component sends its own requests with WasiOptions.HttpClient.

public HttpResponseMessage HandleHttpRequest(WasmComponentInstance instance, HttpRequestMessage request, CancellationToken cancellationToken = default)

Parameters

instance WasmComponentInstance
The instance of the component, whose imports are the imports of this context.
request HttpRequestMessage
The request: its URI is absolute, or its header Host gives the authority.
cancellationToken CancellationToken
The token to stop reading the body of the response.

Returns

HttpResponseMessage The response.

Exceptions

InvalidOperationException
The component does not export wasi:http/handler.
WasmTrapException
The component trapped.

Remarks

The call runs on the current thread, as the thread of the store: handle the requests of a store one at a time. The body of the response is read before the method returns. An error of the handler is a response with the status 500.

Example

using var runtime = new WasmRuntime();

// A component of the world wasi:http/service: it exports wasi:http/handler
var component = runtime.CompileComponent(WasmRuntime.WatToWasm(text));
var wasi = new WasiComponentContext();
using var store = runtime.CreateStore();
var instance = store.InstantiateComponent(component, wasi.DefineImports());

// Each call handles a request on the thread of the store: a server of the host calls it for each request
using var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/items");
using var response = wasi.HandleHttpRequest(instance, request);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);

Run(WasmComponentInstance)

WasiComponentContext.cs

Runs a command: calls the function run of the export wasi:cli/run of the instance (WASI 0.3 when the component exports both versions), and returns the exit code of the program: 0 when run returns ok, 1 when it returns error, or the code given to wasi:cli/exit.

public static int Run(WasmComponentInstance instance)

Parameters

instance WasmComponentInstance
The instance of the component.

Returns

int The exit code.

Exceptions

InvalidOperationException
The component does not export wasi:cli/run.
WasmTrapException
The component trapped.