Accessing Java Arrays in JBI

Arrays that are passed to JBI code are passed as values of type jamaica_ref. The array length can be accessed through the macro JAMAICA_GET_ARRAY_LENGTH(array), while the following macros permit reading of array elements.

A corresponding set of macros is available to write array elements.

Example code that sums up the elements of an integer array within native code may look like this C routine.

      
jamaica_int32 Jam_Test_sum(
    jamaica_thread *ct, 
    jamaica_ref array) {
  jamaica_int32 Result, len, i, v;

  /* protect array from being GC'ed */
  Jamaica_SaveLocal(ct,array);
  {
    Result = 0;
    len = JAMAICA_GET_ARRAY_LENGTH(array);

    for(i=0; i<len; i++) {

      /* permit thread switch */
      JAMAICA_SYNCHRONIZATION_POINT(ct); 

      JAMAICA_GET_ARRAY32(array,i,v);
      Result = Result + v;

    }
  }
  Jamaica_ReleaseLocal(ct);  /* array */

  return Result;
}
      
    

The corresponding declaration in the Java class Test.java is

      
  public static native int sum(int[] a);