dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.23k stars 4.45k forks source link

[WASM] Issues with callback parameters marshaling #101637

Closed maxkatz6 closed 2 weeks ago

maxkatz6 commented 2 weeks ago

Description

That's a simple bug, which I will send a PR for. Not sure if this issue is really necessary but creating just in case.

After trying out .NET 9 preview and nightly builds with Avalonia we have found an issue with some of our JSImport interop methods. Specifically, this one was acting weird, raising an error "Value is not an integer" each time when third callback parameter had a decimal point.

Additionally, if you change other two argument types to double, it will work as expected.

Reproduction Steps

JS method:

export function jsFunc(callback) {
    callback(10, 20, 15.5);
}

.NET mapping:

[JSImport("jsFunc", "moduleName")]
public static partial void JsFunc(
    [JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>]
    Action<int, int, double> callback);

After that it's enough to call this method, for example:

JsFunc((a, b, c) => Console.WriteLine($"{a} {b} {c}"))

Expected behavior

Callback parameters should be converted independently from each other. Changing type of second parameter should not affect how third parameter is converter.

Actual behavior

Third parameter is casted to the type of second parameter.

Regression?

Yes, it's a regression compared to .NET 8.

Known Workarounds

Adjust JSImport definition to expect second and third callback parameters to be of the same type.

Configuration

No response

Other information

No response