Open hiinaspace opened 1 year ago
Hi @hiinaspace:
The intention behind this is that externref
is a reference to an external (host) object, so it is intentionally limiting the support for .NET types to those that are reference types. It also prevents unintentional boxing of value types were they implicitly allowed.
As Vector3
is a value type, it gets rejected. Using object
and manually boxing a Vector3
, as you've discovered, should work.
I think you'll have to stick with using object
and doing an explicit boxing/unboxing of the value until I'm convinced that implicitly boxing value types for externref
is the right approach.
At a bare minimum, we should improve this exception message to note that it's because the type is a value type and .NET types are required to be reference types to work with externref
.
I see, thanks for the clarification. My java background assuming that all non-primitives are references doesn't work in c#...
I agree that a better error message would be nice, in lieu of autoboxing.
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:
However, as written, I get
If I change the function argument type from
Vector3
to justobject
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 setsType.IsValueType == true
.Is that test of IsValueType necessary? I don't know if this is just a weird unity environment thing.