leaningtech/cheerpj-meta

Conversion rules - question about buffer support

Closed this issue · 1 comments

zb3 commented

Suppose a native function takes a ByteBuffer direct nio buffer as a parameter. What would be the most optimal way to convert it to an Uint8Array such that ideally no copy occurs and the array operates directly on the allocated memory? Is that possible?

(for example LWJGL uses this convention, the java code passes the data using nio buffers)

I am assuming you are referring to native methods implemented manually in JavaScript following the instructions here: https://cheerpj.com/docs/guides/Implementing-Java-native-methods-in-JavaScript

In this case if the arguments are primitive arrays they will passed without copies to JavaScript. See the docs here for more information: https://cheerpj.com/docs/reference/CJ3Library#conversion-rules

You should be able to extract the primitive arrays from the ByteBuffer objects either in Java, so that JavaScript only sees the primitive arrays. You can also achieve this on the JavaScript side using the library mode interfaces documented here: https://cheerpj.com/docs/reference/CJ3Library

For further discussion please join our discord (https://discord.leaningtech.com), GitHub issues are not most appropriate place for discussing features.