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 WasiComponentContextRemarks
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
| Name | Description |
|---|---|
| WasiComponentContext(WasiOptions?) | Creates the environment of a component. |
WasiComponentContext(WasiOptions?)
WasiComponentContext.csCreates the environment of a component.
public WasiComponentContext(WasiOptions? options = null)Parameters
optionsWasiOptions?- 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
| Name | Description |
|---|---|
| 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.csDefines the interfaces of WASI 0.2 and WASI 0.3.
public ComponentImports DefineImports(ComponentImports? imports = null)Parameters
importsComponentImports?- The imports where the interfaces are defined, or null to create them.
Returns
ComponentImports The imports.
HandleHttpRequest(WasmComponentInstance, HttpRequestMessage, CancellationToken)
WasiComponentContext.csHandles 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
instanceWasmComponentInstance- The instance of the component, whose imports are the imports of this context.
requestHttpRequestMessage- The request: its URI is absolute, or its header Host gives the authority.
cancellationTokenCancellationToken- 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.csRuns 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
instanceWasmComponentInstance- The instance of the component.
Returns
int The exit code.
Exceptions
InvalidOperationException- The component does not export
wasi:cli/run. WasmTrapException- The component trapped.