Skip to content

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

Description

@hiinaspace

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

if (!type.IsValueType)
, 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions