Function
A Function represents a callable piece of code. This may be a WASM function that can be called by C# code, or a C# function which can be called by WASM code. The Function class is a wrapper around the wasmtime::Func struct.
In many cases you will not need to directly use this type. The autogenerated wrapper code already handles calling functions in the most efficient way possible.
Properties​
IReadOnlyList<ValueKind> Parameters​
Get the types of the parameters that this function is called with.
IReadOnlyList<ValueKind> Results​
Get the types of the values that this function returns.
bool IsNull​
Get a value indicating if this Function represents the "null" function.
Function Null​
Get a "null" function reference.
Methods​
bool CheckTypeSignature(Type? returnType = null, params Type[] parameters)​
Check if the given match the Results/Parameters of this Function. This can be used as a type check before calling Invoke.
// func.Results == [ ValueKind.Float32 ]
// func.Parameters == [ ValueKind.Int32, ValueKind.Int64 ]
// True
func.CheckTypeSignature(typeof(float), typeof(int), typeof(long));
// False
func.CheckTypeSignature(null, typeof(long), typeof(double));
Invoke​
There are a thee Invoke functions which call this function and return a result.
object? Invoke()object? Invoke(params ValueBox[] arguments)object? Invoke(ReadOnlySpan<ValueBox> arguments)
Using these methods always allocates, prefer using WrapAction/WrapFunc.
WrapAction/WrapFunc​
Attempt to "Wrap" a Function as a standard C# Action or Func. Using a wrapper is less error prone (type safe), faster to execute and does not allocate on every call.
An Action<T> represents a function which accepts a value (type T) and returns nothing. A Func<T> represents a function which accepts nothing and return a value (type T).
A wrapper cannot be created if the types do not match the signature of the Function. In this case Wrap will return null.
Function func = some_func;
// Try to wrap the function as an "Action" which accepts an int.
Action<int> action = func.WrapAction<int>();
// Invoke it like a normal C# method call.
action(10);
FromCallback​
Convert a standard C# Action or Func into a Function. This can be used to allow WASM code to call back into C# code.