In the Android JNI binding, you can expose a Java method such as method(int[] intArray) {}
to JavaScript, pass it a JavaScript array, and expect the binding to convert that JavaScript array to int[]
. Does Android have the same handling for e.g. Uint8Array()
?
A Typed Array is a slab of memory with a typed view into it, much like how arrays work in C. Because a Typed Array is backed by raw memory, the JavaScript engine can pass the memory directly to native libraries without having to painstakingly convert the data to a native representation.
If you just wanted to return a single dimensional array of ints, then the NewIntArray() function is what you'd use to create the return value. If you wanted to create a single dimensional array of Strings then you'd use the NewObjectArray() function but with a different parameter for the class.
JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.
The JavaScript engine that runs in Android's WebView does not support Uint8Arrays, or any of the other typed arrays (as of 2.3.3).
EDIT: I did some more testing with the simulators and I have mixed things to report.
On the plus side, the JavaScript engine in Android 3.x's WebView does support typed arrays, with the exception of Float64Array.
On the minus side, the JNI interface to Java via WebView.addJavascriptInterface() does not convert the types at all. This is the code I used to test it:
var u8arr = new Uint8Array(4);
u8arr.set([2, 3, 5, 7]);
android.log(u8arr);
And the Java function for android.log() looks basically like this:
public void log(final int[] data) {
final String dataInfo = (data == null) ? "NULL" : "[" + data.length + "]";
Log.d("JsJni", "data=" + dataInfo);
/* ... */
}
When I called android.log with standard Array() type JavaScript object, I'd get the results one would expect (the array bound to data, type conversion, etc...) When android.log was called with the Uint8Array object (or any of the other typed arrays, for that matter, including Int8Array, Int16Array, and Int32Array), data is null.
It's the same between Android 3.0 and 3.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With