WasmImportAttribute Class

Marks a method as the host function of an import of a WebAssembly module. The source generator writes a DefineWasmImports(WasmStore store, WasmImports? imports = null) method in the partial type that declares the method: it creates a host function per marked method, whose type and conversions are written at build time.

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public sealed class WasmImportAttribute : Attribute

Remarks

The parameters and the results are Int32 or UInt32 (i32), Int64 or UInt64 (i64), Single (f32), Double (f64), Object (externref: the host value of a WasmHostReference) and WasmFunction (funcref). A reference type annotated as not nullable is a non-nullable reference ((ref extern), (ref func)). A method with several results returns a tuple.

Example

// A partial class: the generator adds the DefineWasmImports method
public sealed partial class Logger
{
    private readonly List<int> _values = [];

    public IReadOnlyList<int> Values => _values;

    // A host function of the import "env" "log" whose type is (func (param i32))
    [WasmImport("env", "log")]
    public void Log(int value) => _values.Add(value);

    // A static method: (func (param f64) (result f64))
    [WasmImport("env", "scale")]
    public static double Scale(double value) => value * 2;
}
Inheritance
Attribute WasmImportAttribute

Constructors

NameDescription
WasmImportAttribute(string, string)Initializes a new instance of the WasmImportAttribute class.

WasmImportAttribute(string, string)

WasmImportAttribute.cs

Initializes a new instance of the WasmImportAttribute class.

public WasmImportAttribute(string module, string name)

Parameters

module string
The module name of the import.
name string
The name of the import.

Properties

NameDescription
ModuleGets the module name of the import.
NameGets the name of the import.

Gets the module name of the import.

public string Module { get; }

Property value

string

Gets the name of the import.

public string Name { get; }

Property value

string