dotnet-webassembly icon indicating copy to clipboard operation
dotnet-webassembly copied to clipboard

How can I pass a complex type to a function?

Open furesoft opened this issue 3 years ago • 3 comments

furesoft avatar Oct 31 '21 06:10 furesoft

Allocate it's structure in memory space, then pass the address of that memory to the function. This is basically how pointers or references work in other languages.

Alternatively, use separate parameters for each component of that object and reconstruct it within the function. This is how .NET passes structs to functions.

RyanLamansky avatar Nov 01 '21 23:11 RyanLamansky

Maybe this has been answered somewhere else, so sorry if asking again.

Does this mean that there is no way to pass data without doing a full copy? With Pinvoke we can send Pinned pointers. Is there anything like that? If not is there some way that you could define the Memory Space so it has access to shared memory some way? Maybe with https://docs.microsoft.com/en-us/dotnet/api/system.buffers.memorymanager-1?view=net-5.0

I'm really interested in an effectively zero copy path to calling functions that can manipulate, or return objects (by pointer is fine)

Astn avatar Nov 02 '21 21:11 Astn

You can do that by importing memory. The provider of that memory can hold on to the reference and directly access any of its contents even after the WASM has started operating. Managing memory shared inside and outside of WASM requires care, but enables zero-copy paths that use pointers.

RyanLamansky avatar Nov 03 '21 16:11 RyanLamansky