Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
james-hu opened this issue Feb 7, 2024 · 5 comments
Closed

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

james-hu opened this issue Feb 7, 2024 · 5 comments

Comments

@james-hu
Copy link

james-hu commented Feb 7, 2024

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
Copy link
Member

jasongin commented Feb 8, 2024

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.

  • 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
Copy link
Author

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

  • If you mean the JS object returned from the .NET method I want to call, because the method can't be called (the JS class instance does not have such function on it) there is no way to get the returned value.

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

  • Other methods exist on the class instance as long as their return types are not Memory<T>. I tried changing the return type from Memory<byte> to string or byte[], both made that method available to Node.js.

How are you getting the JS instance of the class?

  • The type definition was generated through MSBuild.
  • I believe the class instance was created through reflection becaue I suspect the code generate didn't work (as described in Is code/dll still being generated for marshalling? #207 )
  • I hope there is a way to tell whether refection is used or generated code is used.
  • I suspect the missing of that method is caused by the missing of generated code.

@james-hu
Copy link
Author

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

@jasongin
Copy link
Member

jasongin commented Mar 9, 2024

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants