/** * Creates a Java array from a V8Array. The type of the Array must be specified. * Currently, only INTEGER, DOUBLE, BOOLEAN and STRING are supported. * The V8Array must only contain elements of type 'arrayType'. * * This method will use J2V8's bulk array copy making it faster than iterating over * all the elements in the array. * * @param array The V8Array to convert to a Java Array. * @param arrayType The type of the V8Array to convert. * * @return A Java array representing a V8Array. */ public static Object getTypedArray(final V8Array array, final int arrayType) { int length = array.length(); if (arrayType == V8Value.INTEGER) { return array.getIntegers(0, length); } else if (arrayType == V8Value.DOUBLE) { return array.getDoubles(0, length); } else if (arrayType == V8Value.BOOLEAN) { return array.getBooleans(0, length); } else if (arrayType == V8Value.STRING) { return array.getStrings(0, length); } throw new RuntimeException("Unsupported bulk load type: " + arrayType); }
intArray = new int[length]; array.getIntegers(0, length, intArray); return intArray; } else if (arrayType == V8Value.DOUBLE) {
/** * Creates a Java array from a V8Array. The type of the Array must be specified. * Currently, only INTEGER, DOUBLE, BOOLEAN and STRING are supported. * The V8Array must only contain elements of type 'arrayType'. * * This method will use J2V8's bulk array copy making it faster than iterating over * all the elements in the array. * * @param array The V8Array to convert to a Java Array. * @param arrayType The type of the V8Array to convert. * * @return A Java array representing a V8Array. */ public static Object getTypedArray(final V8Array array, final int arrayType) { int length = array.length(); if (arrayType == V8Value.INTEGER) { return array.getIntegers(0, length); } else if (arrayType == V8Value.DOUBLE) { return array.getDoubles(0, length); } else if (arrayType == V8Value.BOOLEAN) { return array.getBooleans(0, length); } else if (arrayType == V8Value.STRING) { return array.getStrings(0, length); } throw new RuntimeException("Unsupported bulk load type: " + arrayType); }
@Test(expected = UnsupportedOperationException.class) public void testGetInts2Undefined() { V8Array undefined = v8.getArray("array"); undefined.getIntegers(0, 1, new int[1]); }
@Test(expected = UnsupportedOperationException.class) public void testGetIntsUndefined() { V8Array undefined = v8.getArray("array"); undefined.getIntegers(0, 1); }
intArray = new int[length]; array.getIntegers(0, length, intArray); return intArray; } else if (arrayType == V8Value.DOUBLE) {
@Test(expected = V8ResultUndefined.class) public void testGetIntsWithoutInts() { V8Array a = v8.executeArrayScript("[1,'a',3,4,5];"); try { a.getIntegers(0, 5); } finally { a.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetSubArrayOfIntsOutOfBounds() { V8Array a = v8.executeArrayScript("[1,2,3,4,5];"); try { a.getIntegers(3, 3); } finally { a.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetDoublesWithoutDoubles() { V8Array a = v8.executeArrayScript("[1.1,'a',3.1,4.1,5.1];"); try { a.getIntegers(0, 5); } finally { a.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetSubArrayOfDoublesOutOfBounds() { V8Array a = v8.executeArrayScript("[1.1,2.1,3.1,4.1,5.1];"); try { a.getIntegers(3, 3); } finally { a.close(); } }
@Test(expected = IndexOutOfBoundsException.class) public void testGetIntsSmallerArray() { V8Array a = v8.executeArrayScript("[1,2,3,4]"); int[] result = new int[3]; try { a.getIntegers(0, 4, result); } finally { a.close(); } }
@Test public void testGetIntsSameSizeArray() { V8Array a = v8.executeArrayScript("[1,2,3,4]"); int[] result = new int[4]; int size = a.getIntegers(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetIntsBiggerArray() { V8Array a = v8.executeArrayScript("[1,2,3,4]"); int[] result = new int[40]; int size = a.getIntegers(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetSubArrayOfInts() { V8Array a = v8.executeArrayScript("[1,2,3,4,5];"); int[] result = a.getIntegers(4, 1); assertEquals(1, result.length); assertEquals(5, result[0]); a.close(); }
@Test public void testGetTypedRangeArrayValue() { V8Array result = (V8Array) v8.executeScript("var buf = new ArrayBuffer(100); var ints = new Int32Array(buf); for(var i = 0; i < 25; i++) {ints[i] = i;}; ints"); assertEquals(25, result.length()); int[] ints = result.getIntegers(0, 25); for (int i = 0; i < ints.length; i++) { assertEquals(i, ints[i]); } result.close(); }
@Test public void testGetSubArrayOfInts2() { V8Array a = v8.executeArrayScript("[1,2,3,4,5];"); int[] result = a.getIntegers(3, 2); assertEquals(2, result.length); assertEquals(4, result[0]); assertEquals(5, result[1]); a.close(); }
@Test public void testGetIntsPopulatesArray() { V8Array a = v8.executeArrayScript("[1,2,3,4]"); int[] result = new int[4]; a.getIntegers(0, 4, result); assertEquals(1, result[0]); assertEquals(2, result[1]); assertEquals(3, result[2]); assertEquals(4, result[3]); a.close(); }
@Test public void testGetArrayOfInts() { V8Array a = v8.executeArrayScript("[1,2,3,4,5];"); int[] result = a.getIntegers(0, 5); assertEquals(5, result.length); assertEquals(1, result[0]); assertEquals(2, result[1]); assertEquals(3, result[2]); assertEquals(4, result[3]); assertEquals(5, result[4]); a.close(); }
@Test public void testGetIntsWithDoubles() { V8Array a = v8.executeArrayScript("[1,1.1,3,4,5];"); int[] ints = a.getIntegers(0, 5); assertEquals(5, ints.length); assertEquals(1, ints[0]); assertEquals(1, ints[1]); assertEquals(3, ints[2]); assertEquals(4, ints[3]); assertEquals(5, ints[4]); a.close(); }
/** * Creates a Java array from a V8Array. The type of the Array must be specified. * Currently, only INTEGER, DOUBLE, BOOLEAN and STRING are supported. * The V8Array must only contain elements of type 'arrayType'. * * This method will use J2V8's bulk array copy making it faster than iterating over * all the elements in the array. * * @param array The V8Array to convert to a Java Array. * @param arrayType The type of the V8Array to convert. * * @return A Java array representing a V8Array. */ public static Object getTypedArray(final V8Array array, final int arrayType) { int length = array.length(); if (arrayType == V8Value.INTEGER) { return array.getIntegers(0, length); } else if (arrayType == V8Value.DOUBLE) { return array.getDoubles(0, length); } else if (arrayType == V8Value.BOOLEAN) { return array.getBooleans(0, length); } else if (arrayType == V8Value.STRING) { return array.getStrings(0, length); } throw new RuntimeException("Unsupported bulk load type: " + arrayType); }