Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing float[] (Single[]) to JS from .NET via [JSImport] #97633

Closed
1 task done
Matheos96 opened this issue Jan 28, 2024 · 4 comments
Closed
1 task done

Support passing float[] (Single[]) to JS from .NET via [JSImport] #97633

Matheos96 opened this issue Jan 28, 2024 · 4 comments
Labels
arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript os-browser Browser variant of arch-wasm

Comments

@Matheos96
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I recently tried (and failed) to pass a simple float[] from .NET to JS. This is obviously not supported, as per the docs:
image

I got it working by casting my floats to doubles using Linq, quite an ugly way which probably could do in a better way:
var myDoubles = myFloats.Select(f => (double)f).ToArray();
passing this to JS works.

Describe the solution you'd like

It struck me, if the "workaround" is this simple, why could it not be indirectly supported by the [JSImport] API itself? Either through casting floats to doubles or something more clever? I mean, whatever we can fit in a float, could most definately fit in a double. If you want to explicit then force us to use [JSMarshalAs<JSType.Array<JSType.Number>>] or something to make it work.

Thoughts?

Additional context

No response

@javiercn javiercn transferred this issue from dotnet/aspnetcore Jan 29, 2024
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 29, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 29, 2024
@SingleAccretion SingleAccretion added area-System.Runtime.InteropServices.JavaScript and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 29, 2024
@maraf maraf added arch-wasm WebAssembly architecture os-browser Browser variant of arch-wasm labels Jan 30, 2024
@ghost
Copy link

ghost commented Jan 30, 2024

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I recently tried (and failed) to pass a simple float[] from .NET to JS. This is obviously not supported, as per the docs:
image

I got it working by casting my floats to doubles using Linq, quite an ugly way which probably could do in a better way:
var myDoubles = myFloats.Select(f => (double)f).ToArray();
passing this to JS works.

Describe the solution you'd like

It struck me, if the "workaround" is this simple, why could it not be indirectly supported by the [JSImport] API itself? Either through casting floats to doubles or something more clever? I mean, whatever we can fit in a float, could most definately fit in a double. If you want to explicit then force us to use [JSMarshalAs<JSType.Array<JSType.Number>>] or something to make it work.

Thoughts?

Additional context

No response

Author: Matheos96
Assignees: -
Labels:

arch-wasm, untriaged, area-System.Runtime.InteropServices.JavaScript, os-browser

Milestone: -

@maraf
Copy link
Member

maraf commented Jan 30, 2024

cc @pavelsavara

@pavelsavara
Copy link
Member

It struck me, if the "workaround" is this simple, why could it not be indirectly supported by the [JSImport] API itself?

Indeed, there is duplicate feature request for that. #97380

Thoughts?

I found few ways how you can do it here https://stackoverflow.com/questions/69805135/how-does-c-sharp-convert-double-arrays-to-float-arrays-or-int-arrays

I guess float[] floatValues = Array.ConvertAll(values, Convert.ToSingle); would be fastest.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jan 30, 2024
@Matheos96
Copy link
Author

Matheos96 commented Jan 30, 2024

It struck me, if the "workaround" is this simple, why could it not be indirectly supported by the [JSImport] API itself?

Indeed, there is duplicate feature request for that. #97380

Thoughts?

I found few ways how you can do it here https://stackoverflow.com/questions/69805135/how-does-c-sharp-convert-double-arrays-to-float-arrays-or-int-arrays

I guess float[] floatValues = Array.ConvertAll(values, Convert.ToSingle); would be fastest.

Ok. Thanks, sounds good. I guess your example in my case would be something like
double[] doubleValues = Array.ConvertAll(floatValues, Convert.ToDouble);
as my values are float but they need to be double for it to work, as it is today.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript os-browser Browser variant of arch-wasm
Projects
None yet
Development

No branches or pull requests

4 participants