google / jni-bind

JNI Bind is a set of advanced syntactic sugar for writing efficient correct JNI Code in C++17 (and up).
Apache License 2.0
266 stars 31 forks source link

Add direct and indirect support for arrays. #342

Open jwhpryor opened 1 month ago

jwhpryor commented 1 month ago

Arrays of directly spannable types should be able to allocate themselves as direct or indirect (indirect being the default).

I propose the following syntax:

LocalArray<jbyte> direct_array { ArrayDirect{}, 128 };
CHECK(direct_array.IsDirect());

For this to be meaningful, arrays likely need to have a global flavour. Arrays of objects must never be constructible in this manner.

LocalArray<jobject, kClass> direct_array_of_objects {ArrayDirect{}, 10 };  // NO: fails to compile

If an array is allocated directly, the intention is almost certainly to be used for a longer duration than a single native call, so arrays need global support so that the corresponding unlock call can be invoked as the array falls out of scope.

For arrays of objects they will need to have tests added specifically for GlobalArray when it is constructed from a local object.