Is there documentation on how to use JS Interop MemoryView? #102386
-
Hello, this page references a MemoryView js object for marshaling data in javascript to a Span. I can't seem to find any documentation on how to utilize this class so large js arrays don't have to be copied? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
cc @pavelsavara |
Beta Was this translation helpful? Give feedback.
-
If you pass The API is this runtime/src/mono/browser/runtime/dotnet.d.ts Lines 661 to 679 in 794b0c2 It forces you to copy from it or into in, because the C# |
Beta Was this translation helpful? Give feedback.
-
@pavelsavara Thanks for the quick response. I am trying to pass a file blob from javascript to .NET. I was hoping there was some way to work with this data as a stream to avoid having a huge memory allocation. Is there anyway that you are aware of to accomplish this? I don't see anything specifically in the documentation so I'm guessing I will be stuck marshaling the underlying blob to a byte[] or base64 string. Thanks again for your help. |
Beta Was this translation helpful? Give feedback.
-
My application supports an offline disconnected mode. This mode is implemented via a service worker and a shared worker. The shared worker spins up a .NET wasm instance and loads a subset of the server side code built for offline utilizing the ASP.NET core test host. The service worker than forwards any requests to the API to this worker which then sends them through to the test host running in wasm. It does this by translating the requests and re-executing on the client that created from the test host. For most cases this is just JSON based requests and not a problem. However we have file upload functionality that sends file data. Currently I'm sending this data as base64 which is going likely to cause performance issue if a large file were uploaded. |
Beta Was this translation helpful? Give feedback.
You can also pass
ArraySegment
which will GC pin it and you will have long term buffer withIMemoryView
interface on JS side. You have todispose()
it, when you are done.You can also allocate via
Marshal.AllocHGlobal
and then pass it to JS asIntPtr
.Ideally allocating the buffer once and keep using it to transfer bytes on each chunk of your stream.
Then you can use
localHeapViewU8
API to get wasm linear memory.runtime/src/mono/browser/runtime/dotnet.d.ts
Lines 610 to 613 in 794b0c2
Do it on each chunk, b…