microsoft / node-api-dotnet

Advanced interoperability between .NET and JavaScript in the same process.
MIT License
524 stars 58 forks source link

Is Memory<byte> supported as return type in dotnet? #208

Closed james-hu closed 8 months ago

james-hu commented 9 months ago

I tried to use Memory<byte> as return type of a method on dotnet side and call the method from Javascript. However, although the generated .d.ts file contains the method signature, the Javascript code is not able to call that method. The error message says the object does not have such method/property/function. I tried to change the return type to string or byte[] then the method could be called from Javascript.

Is Memory<byte> supported?

BTW, I suspected marshaller code needs to be generated and utlised, however, I couldn't find any generated file. I've created another issue for that: #207

jasongin commented 9 months ago

A .NET Memory<byte> should get converted to JS UInt8Array, in both typedefs and runtime.

But there might be some other reason you're unable to call the method.

james-hu commented 9 months ago

Can you check what properties are defined on the JS object? Object.keys(obj)

Do you see other expected methods/properties on the object?

How are you getting the JS instance of the class?

james-hu commented 9 months ago

BTW, currently I use base64 encoded string to pass binary data from .NET to Node.js as a workaround.

jasongin commented 8 months ago

I confirmed in #220 with more testing that Memory<byte> is working as expected. So there is likely some other problem here.

the JS class instance does not have such function on it

This sounds like there was some other problem with the method that is unrelated to the fact that it returns Memory<byte>. If you want to troubleshoot further, it might help to share relevant snippets of the code.

jasongin commented 8 months ago

I'm closing this for now since the originally reported problem is not reproducible. But feel free to re-open with more information if you're still having this problem.