bytecodealliance / bytecodealliance/wasmtime-dotnet

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

Open
#174 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
519
Forks
67
Avg merge
1h 7m
Merged PRs (30d)
1

Description

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:

```wat
(module
(import "" "getX" (func $.getX (param externref) (result f32)))
(func (export "run") (param externref) (result f32)
local.get 0
call $.getX
)
)
```

```csharp
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("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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.