1

Part 3 of multiple questions (here part 2: MemoryView utilization with latest JSImport API syntax to interop with Javascript - Blazor)

I'd like to find a solution to receiving RETURNed Task values (or other pointer sharing syntax) from javascript interop, back to Blazor.

Here's an example that works for sending MemoryView parameters asynchronously to Javascript, but not for receiving a MemoryView back. Credit to Mister Magoo for the original versions of these examples before I modified them.

The import

[JSImport("getMessage", "SampleJS")]
[return: JSMarshalAs<JSType.Promise<JSType.Any>>()]
internal static partial Task<object> 
  GetWelcomeMessage([JSMarshalAs<JSType.MemoryView>] ArraySegment<byte> bytes);

JS module

export async function getMessage(dataPointer) {
  var maskedData = new Uint8Array(dataPointer.slice())
  console.log(maskedData)
  return await new Promise((resolve) => {
    setTimeout(() => {
      resolve(maskedData);
    }, 2000);
  });
}

This just logs the data (bytes) received, waits 2s and returns the same data.

Usage

byte[] sampleData = Encoding.UTF8.GetBytes("Hello from C#");
ArraySegment<byte> sampleDataPointer = new ArraySegment<byte>(sampleData);
object? result = await GetWelcomeMessage(sampleDataPointer);
if (result is byte[] bytes)
{
    message = Encoding.UTF8.GetString(bytes);
    Console.WriteLine($"Got {message} from {result.GetType()}");
}

How can I convert this so there is not marshaled copying of the returned result data, rather it returns a pointer? Would this be technically impossible because the javascript memory would be demolished upon return?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.