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 : AttributeRemarks
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
AttributeWasmImportAttribute
Constructors
| Name | Description |
|---|---|
| WasmImportAttribute(string, string) | Initializes a new instance of the WasmImportAttribute class. |
WasmImportAttribute(string, string)
WasmImportAttribute.csInitializes a new instance of the WasmImportAttribute class.
Properties
| Name | Description |
|---|---|
| Module | Gets the module name of the import. |
| Name | Gets the name of the import. |