Skip to content

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

Closed

Description

@Matheos96

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

Activity

  1. transferred this issue fromdotnet/aspnetcoreon Jan 29, 2024
  2. ghost added
    untriaged
    on Jan 29, 2024
  3. ghost added
    needs-area-label
    on Jan 29, 2024
  4. added and removed
    needs-area-label
    on Jan 29, 2024
  5. added
    arch-wasm
    os-browser
    on Jan 30, 2024
  6. maraf commented on Jan 30, 2024

    @maraf
    Member
  7. pavelsavara commented on Jan 30, 2024

    @pavelsavara
    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.

  8. ghost removed
    untriaged
    on Jan 30, 2024
  9. Matheos96 commented on Jan 30, 2024

    @Matheos96
    Author

    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.

  10. locked and limited conversation to collaborators on Mar 1, 2024
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

    arch-wasmarea-System.Runtime.InteropServices.JavaScriptos-browser

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions