WasiContext Class

The environment of a WASI Preview 1 program (wasi_snapshot_preview1): its arguments, environment variables, standard streams, clocks, random numbers, and the file system of its preopened directories. Define its imports (WasiContext.DefineImports), instantiate the module, and run it (WasiContext.Run).

public sealed class WasiContext : IDisposable

Remarks

A context is the state of one program, such as its open files: use one context per instance, from one thread at a time (the thread of the store). Dispose it to close the files the program left open.

The functions of WASI read and write the memory that the module exports as memory. They do not support the sockets (they return WasiErrno.NotSocket) and the signals (WasiErrno.NotImplemented).

Example

using var runtime = new WasmRuntime();

// A command that writes "Hello, WASI!" to its standard output (file descriptor 1) with fd_write
var module = runtime.Compile(WasmRuntime.WatToWasm("""
    (module
      (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
      (memory (export "memory") 1)
      (data (i32.const 8) "Hello, WASI!\n")
      (func (export "_start")
        ;; An iovec at 0: the address and the length of the text
        (i32.store (i32.const 0) (i32.const 8))
        (i32.store (i32.const 4) (i32.const 13))
        (drop (call $fd_write (i32.const 1) (i32.const 0) (i32.const 1) (i32.const 32)))))
    """));

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

using var store = runtime.CreateStore();
var instance = store.Instantiate(module, wasi.DefineImports(store));
var exitCode = wasi.Run(instance);

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

Constructors

NameDescription
WasiContext(WasiOptions?)Creates the environment of a program.

WasiContext(WasiOptions?)

WasiContext.cs

Creates the environment of a program.

public WasiContext(WasiOptions? options = null)

Parameters

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

Exceptions

ArgumentException
An argument or an environment variable contains a null character, or the name of an environment variable is empty or contains =.

Methods

NameDescription
DefineImports(WasmStore, WasmImports?)Defines the functions of WASI Preview 1, the imports of the module wasi_snapshot_preview1.
Dispose()Closes the files that the program left open. The streams of WasiOptions are not closed.
Initialize(WasmInstance)Initializes a reactor (a library): calls the export _initialize of the instance, when it has one. Then call the exports of the instance.
Run(WasmInstance)Runs a command: calls the export _start of the instance, and returns the exit code of the program, 0 when _start returns, or the code of proc_exit.

DefineImports(WasmStore, WasmImports?)

WasiContext.cs

Defines the functions of WASI Preview 1, the imports of the module wasi_snapshot_preview1.

public WasmImports DefineImports(WasmStore store, WasmImports? imports = null)

Parameters

store WasmStore
The store that creates the host functions, where the module is instantiated.
imports WasmImports?
The imports where the functions are defined, or null to create them.

Returns

WasmImports The imports.

Exceptions

ObjectDisposedException
The context is disposed.

Closes the files that the program left open. The streams of WasiOptions are not closed.

public void Dispose()

Initialize(WasmInstance)

WasiContext.cs

Initializes a reactor (a library): calls the export _initialize of the instance, when it has one. Then call the exports of the instance.

public void Initialize(WasmInstance instance)

Parameters

instance WasmInstance
An instance of the module, instantiated with the imports of WasiContext.DefineImports.

Exceptions

WasiExitException
The program called proc_exit.
WasmTrapException
The program trapped.
ObjectDisposedException
The context is disposed.

Run(WasmInstance)

WasiContext.cs

Runs a command: calls the export _start of the instance, and returns the exit code of the program, 0 when _start returns, or the code of proc_exit.

public int Run(WasmInstance instance)

Parameters

instance WasmInstance
An instance of the module, instantiated with the imports of WasiContext.DefineImports.

Returns

int The exit code of the program.

Exceptions

InvalidOperationException
The instance does not export a function _start.
WasmTrapException
The program trapped.
ObjectDisposedException
The context is disposed.