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 WasmModuleProperties
| Name | Description |
|---|---|
| Bytes | Gets the binary of the module. |
| Exports | Gets the exports of the module, in the order of the export section. |
| 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). |
| IsLoadedFromCodeCache | Gets whether the code of the functions was loaded from a code cache (see WasmRuntime.Compile). |
| Runtime | Gets the runtime that compiled the module. |
Bytes
WasmObjects.csExports
Gets the exports of the module, in the order of the export section.
public abstract IReadOnlyList<WasmExport> Exports { get; }Property value
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; }Property value
IsLoadedFromCodeCache
Gets whether the code of the functions was loaded from a code cache (see WasmRuntime.Compile).
public abstract bool IsLoadedFromCodeCache { get; }Property value
Runtime
Gets the runtime that compiled the module.
public abstract WasmRuntime Runtime { get; }Property value
Methods
| Name | Description |
|---|---|
| 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.csReturns 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)IsMutableI16ArrayParameter(WasmFunctionType, int)
WasmObjects.csReturns 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
functionTypeWasmFunctionType- A function type of an import or of an export of the module.
parameterIndexint- The index of the parameter.