wasmtime-dotnet icon indicating copy to clipboard operation
wasmtime-dotnet copied to clipboard

Unable to create a function with parameter when Type.IsValueType is true

Open hiinaspace opened this issue 2 years ago • 3 comments

I have a really simple adaption of the externref sample, running inside unity 2021.3, whose csharp runtime is ostensibly .NET standard 2.1 compatible:

(module
  (import "" "getX" (func $.getX (param externref) (result f32)))
  (func (export "run") (param externref) (result f32)
    local.get 0
    call $.getX
  )
)
public class WasmTest : MonoBehaviour
{
    public TextAsset watSource;

    void Start()
    {
        var engine = new Engine();

        var module = Module.FromText(
            engine,
            watSource.name,
            watSource.text
        );

        var linker = new Linker(engine);
        var store = new Store(engine);

        linker.Define(
            "",
            "getX",
            Function.FromCallback(store, (Vector3 vec) => ((Vector3)vec).x)
        );

        var instance = linker.Instantiate(store, module);
        var run = instance.GetFunction<object, float>("run");
        float res = run(transform.position);
        Debug.Log($"the x is {res}");
    }
}

However, as written, I get

WasmtimeException: Unable to create a function with parameter of type 'UnityEngine.Vector3'.
Wasmtime.Function.GetFunctionType (System.Type type, System.Boolean hasReturn, System.Collections.Generic.List`1[T] parameters, System.Collections.Generic.List`1[T] results, System.Boolean& hasCaller, System.Boolean& returnsTuple) (at C:/Users/s/source/repos/wasmtime-dotnet/src/Function.cs:2411)
Wasmtime.Function..ctor (Wasmtime.IStore store, System.Delegate callback, System.Boolean hasReturn) (at C:/Users/s/source/repos/wasmtime-dotnet/src/Function.cs:2242)
Wasmtime.Function.FromCallback[T,TResult] (Wasmtime.IStore store, System.Func`2[T,TResult] callback) (at C:/Users/s/source/repos/wasmtime-dotnet/src/Function.cs:300)
WasmTest.Start () (at Assets/wasmtest/WasmTest.cs:25)

If I change the function argument type from Vector3 to just object and do the cast manually, it works as expected. Looks like it's because it falls out of Value.TryGetKind at https://github.com/bytecodealliance/wasmtime-dotnet/blob/387edd242f105d5394399acd19c25509ebb42b02/src/Value.cs#L210 , because unity sets Type.IsValueType == true.

Is that test of IsValueType necessary? I don't know if this is just a weird unity environment thing.

hiinaspace avatar Nov 03 '22 01:11 hiinaspace