WasmRuntime Class

A WebAssembly runtime written in .NET: it decodes and validates the modules (WasmRuntime.Compile), and creates the stores where they are instantiated (WasmRuntime.CreateStore). The code is interpreted, and the functions that run often are compiled to .NET code when the platform supports dynamic code.

public sealed class WasmRuntime : IDisposable

Remarks

The WebAssembly 3.0 features are enabled (the reference types, the typed function references, the GC types, the exceptions, the tail calls, memory64, the multiple memories, the SIMD and relaxed SIMD instructions, the wide arithmetic), and the threads (the shared memories and the atomic instructions).

The bytes of a memory are a .NET array (WasmMemory.Data), replaced when the memory grows. The bytes of a shared memory are allocated once for the maximum size of the memory (WasmStoreOptions.SharedMemoryAllocator): the stores of every thread can import it.

The runtime and the modules can be used from any thread; a store is used by one thread at a time. The running code calls WasmStoreOptions.CheckInterrupt periodically, so that the host can interrupt it.

Implements

Constructors

NameDescription
WasmRuntime(WasmRuntimeOptions?)Creates a runtime.

WasmRuntime(WasmRuntimeOptions?)

WasmRuntime.cs

Creates a runtime.

public WasmRuntime(WasmRuntimeOptions? options = null)

Parameters

options WasmRuntimeOptions?
The options, or null for the default options.

Exceptions

ArgumentOutOfRangeException
The interval of the interruption checks or the maximum sizes are not positive.

Properties

NameDescription
OptionsGets the options of the runtime.

Gets the options of the runtime.

public WasmRuntimeOptions Options { get; }

Property value

WasmRuntimeOptions

Methods

NameDescription
Compile(byte[])Compiles a module. It can be called from any thread.
Compile(byte[], ReadOnlySpan<byte>)Compiles a module, with the code of its functions from a code cache (WasmModule.CreateCodeCache) when the cache was created for this binary by this build of the engine: the validation and the compilation of the code are skipped. Otherwise, the module is compiled from its binary (WasmModule.IsLoadedFromCodeCache is false). It can be called from any thread.
Compile(ReadOnlySpan<byte>)Compiles a module. It can be called from any thread.
CreateStore(WasmStoreOptions?)Creates a store, where the modules of the runtime are instantiated. A store is used by one thread at a time.
Dispose()Releases the timer of the interruptions. The stores and the modules must not be used afterwards.
Validate(ReadOnlySpan<byte>)Validates a module (https://webassembly.github.io/spec/core/appendix/embedding.html#mathrm-module-validate-xref-syntax-modules-syntax-module-mathit-module). It can be called from any thread.
WatToWasm(string)Converts the text format of a module to its binary format (https://webassembly.github.io/spec/core/text/index.html).

Compile overloads

Compile(byte[])

WasmRuntime.cs

Compiles a module. It can be called from any thread.

public WasmModule Compile(byte[] bytes)
Parameters
bytes byte[]
The binary of the module. The module keeps the array: do not change it.
Returns

WasmModule The module, which can be instantiated by the stores of the runtime.

Exceptions
WasmCompileException
The module is not valid.
ObjectDisposedException
The runtime is disposed.

Compile(byte[], ReadOnlySpan<byte>)

WasmRuntime.cs

Compiles a module, with the code of its functions from a code cache (WasmModule.CreateCodeCache) when the cache was created for this binary by this build of the engine: the validation and the compilation of the code are skipped. Otherwise, the module is compiled from its binary (WasmModule.IsLoadedFromCodeCache is false). It can be called from any thread.

public WasmModule Compile(byte[] bytes, ReadOnlySpan<byte> codeCache)
Parameters
bytes byte[]
The binary of the module. The module keeps the array: do not change it.
codeCache ReadOnlySpan<byte>
The code cache of the module, or an empty span.
Returns

WasmModule The module, which can be instantiated by the stores of the runtime.

Exceptions
WasmCompileException
The module is not valid.
ObjectDisposedException
The runtime is disposed.
Remarks

A code cache is trusted like a compiled assembly: its code is not validated again. Store it where only the application can write. The cache detects the changes of the binary, of the engine and the corruptions, but not a cache written on purpose to match the binary.

Compile(ReadOnlySpan<byte>)

WasmRuntime.cs

Compiles a module. It can be called from any thread.

public WasmModule Compile(ReadOnlySpan<byte> bytes)
Parameters
bytes ReadOnlySpan<byte>
The binary of the module, which is copied.
Returns

WasmModule The module, which can be instantiated by the stores of the runtime.

Exceptions
WasmCompileException
The module is not valid.
ObjectDisposedException
The runtime is disposed.

CreateStore(WasmStoreOptions?)

WasmRuntime.cs

Creates a store, where the modules of the runtime are instantiated. A store is used by one thread at a time.

public WasmStore CreateStore(WasmStoreOptions? options = null)

Parameters

options WasmStoreOptions?
The options of the store, or null for the default options.

Returns

WasmStore The store. Dispose it when it is no longer used.

Exceptions

ObjectDisposedException
The runtime is disposed.

Releases the timer of the interruptions. The stores and the modules must not be used afterwards.

public void Dispose()

Validate(ReadOnlySpan<byte>)

WasmRuntime.cs

Validates a module (https://webassembly.github.io/spec/core/appendix/embedding.html#mathrm-module-validate-xref-syntax-modules-syntax-module-mathit-module). It can be called from any thread.

public bool Validate(ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>
The binary of the module.

Returns

bool Whether the module is valid.

Exceptions

ObjectDisposedException
The runtime is disposed.

WatToWasm(string)

WasmRuntime.cs

Converts the text format of a module to its binary format (https://webassembly.github.io/spec/core/text/index.html).

public static byte[] WatToWasm(string text)

Parameters

text string
The text of the module.

Returns

byte[] The binary of the module. It is not validated.

Exceptions

FormatException
The text is not valid.