WasmModule Class

A compiled module (see WasmRuntime.Compile). It is immutable: it can be instantiated by the stores of its runtime, from any thread.

public abstract class WasmModule

Properties

NameDescription
BytesGets the binary of the module.
ExportsGets the exports of the module, in the order of the export section.
ImportsGets the imports of the module, in the order of the import section (https://webassembly.github.io/spec/core/appendix/embedding.html#mathrm-module-imports-xref-syntax-modules-syntax-module-mathit-module).
IsLoadedFromCodeCacheGets whether the code of the functions was loaded from a code cache (see WasmRuntime.Compile).
RuntimeGets the runtime that compiled the module.

Gets the binary of the module.

public ReadOnlyMemory<byte> Bytes { get; }

Property value

ReadOnlyMemory<byte>

Exports

Gets the exports of the module, in the order of the export section.

public abstract IReadOnlyList<WasmExport> Exports { get; }

Imports

Gets the imports of the module, in the order of the import section (https://webassembly.github.io/spec/core/appendix/embedding.html#mathrm-module-imports-xref-syntax-modules-syntax-module-mathit-module).

public abstract IReadOnlyList<WasmImport> Imports { get; }

IsLoadedFromCodeCache

Gets whether the code of the functions was loaded from a code cache (see WasmRuntime.Compile).

public abstract bool IsLoadedFromCodeCache { get; }

Property value

bool

Runtime

Gets the runtime that compiled the module.

public abstract WasmRuntime Runtime { get; }

Property value

WasmRuntime

Methods

NameDescription
CreateCodeCache()Creates the code cache of the module: the validated and compiled code of its functions. Pass it to WasmRuntime.Compile with the same binary, for instance in a later run of the application, to skip the validation and the compilation of the code. The cache is only valid for the build of the engine that created it, and only for the platforms of the same kind (with or without dynamic code).
GetCustomSections(string)Returns the contents of the custom sections of a name (https://webassembly.github.io/spec/core/binary/modules.html#custom-section), in the order of the module.
IsMutableI16ArrayParameter(WasmFunctionType, int)Returns whether a parameter of a function type of the imports or of the exports of the module is (ref null (array (mut i16))): a nullable reference to a final mutable i16 array type without supertype in a recursion group of its own (e.g. the arrays of char codes of the JS String Builtins). A concrete heap type is not described by WasmValueType: the runtime compares the types.

CreateCodeCache()

Creates the code cache of the module: the validated and compiled code of its functions. Pass it to WasmRuntime.Compile with the same binary, for instance in a later run of the application, to skip the validation and the compilation of the code. The cache is only valid for the build of the engine that created it, and only for the platforms of the same kind (with or without dynamic code).

public abstract byte[] CreateCodeCache()

Returns

byte[] The bytes of the cache.

Example

var bytes = WasmRuntime.WatToWasm("""
    (module
      (func (export "add") (param i32 i32) (result i32)
        (i32.add (local.get 0) (local.get 1))))
    """);

// The first run compiles the module from its binary, and saves its code cache (e.g. in a file)
byte[] codeCache;
using (var runtime = new WasmRuntime())
{
    var module = runtime.Compile(bytes);
    Assert.False(module.IsLoadedFromCodeCache);
    codeCache = module.CreateCodeCache();
}

// A later run gives the cache with the same binary: the code is not validated and compiled again. A cache that
// does not match the binary or the build of the engine is ignored: the module is compiled from its binary.
using (var runtime = new WasmRuntime())
{
    var module = runtime.Compile(bytes, codeCache);
    Assert.True(module.IsLoadedFromCodeCache);

    using var store = runtime.CreateStore();
    var add = store.Instantiate(module).GetFunction("add")!;
    Assert.Equal(42, add.Call(WasmValue.FromI32(40), WasmValue.FromI32(2))[0].I32);
}

GetCustomSections(string)

WasmObjects.cs

Returns the contents of the custom sections of a name (https://webassembly.github.io/spec/core/binary/modules.html#custom-section), in the order of the module.

public IReadOnlyList<ReadOnlyMemory<byte>> GetCustomSections(string name)

Parameters

name string
The name of the sections.

Returns

IReadOnlyList<ReadOnlyMemory<byte>>

IsMutableI16ArrayParameter(WasmFunctionType, int)

WasmObjects.cs

Returns whether a parameter of a function type of the imports or of the exports of the module is (ref null (array (mut i16))): a nullable reference to a final mutable i16 array type without supertype in a recursion group of its own (e.g. the arrays of char codes of the JS String Builtins). A concrete heap type is not described by WasmValueType: the runtime compares the types.

public virtual bool IsMutableI16ArrayParameter(WasmFunctionType functionType, int parameterIndex)

Parameters

functionType WasmFunctionType
A function type of an import or of an export of the module.
parameterIndex int
The index of the parameter.

Returns

bool