/** * 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); }
booleanArray = new boolean[length]; array.getBooleans(0, length, booleanArray); return booleanArray; } else if (arrayType == V8Value.STRING) {
/** * 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 testGetBooleansUndefined() { V8Array undefined = v8.getArray("array"); undefined.getBooleans(0, 1); }
@Test(expected = UnsupportedOperationException.class) public void testGetBooleans2Undefined() { V8Array undefined = v8.getArray("array"); undefined.getBooleans(0, 1, new boolean[1]); }
@Test(expected = V8ResultUndefined.class) public void testGetBooleansWithoutBooleans() { V8Array a = v8.executeArrayScript("[true, 'a', false, false, true];"); try { a.getBooleans(0, 5); } finally { a.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetSubArrayOfBooleansOutOfBounds() { V8Array a = v8.executeArrayScript("[true, true, true, true, false];"); try { a.getBooleans(3, 3); } finally { a.close(); } }
@Test(expected = IndexOutOfBoundsException.class) public void testGetBooleanSmallerArray() { V8Array a = v8.executeArrayScript("[true, true, false, false]"); boolean[] result = new boolean[3]; try { a.getBooleans(0, 4, result); } finally { a.close(); } }
booleanArray = new boolean[length]; array.getBooleans(0, length, booleanArray); return booleanArray; } else if (arrayType == V8Value.STRING) {
@Test public void testGetBooleanSameSizeArray() { V8Array a = v8.executeArrayScript("[true, false, false, true]"); boolean[] result = new boolean[4]; int size = a.getBooleans(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetBooleanBiggerArray() { V8Array a = v8.executeArrayScript("[false, false, false, true]"); boolean[] result = new boolean[40]; int size = a.getBooleans(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetSubArrayOfBooleans() { V8Array a = v8.executeArrayScript("[true, false, true, true, false];"); boolean[] result = a.getBooleans(4, 1); assertEquals(1, result.length); assertFalse(result[0]); a.close(); }
@Test public void testGetSubArrayOfBooleans2() { V8Array a = v8.executeArrayScript("[true, false, true, true, false];"); boolean[] result = a.getBooleans(3, 2); assertEquals(2, result.length); assertTrue(result[0]); assertFalse(result[1]); a.close(); }
@Test public void testGetBooleanPopulatesArray() { V8Array a = v8.executeArrayScript("[true, false, false, true]"); boolean[] result = new boolean[4]; a.getBooleans(0, 4, result); assertTrue(result[0]); assertFalse(result[1]); assertFalse(result[2]); assertTrue(result[3]); a.close(); }
@Test public void testGetArrayOfBooleans() { V8Array a = v8.executeArrayScript("[true, false, true, true, false];"); boolean[] result = a.getBooleans(0, 5); assertEquals(5, result.length); assertTrue(result[0]); assertFalse(result[1]); assertTrue(result[2]); assertTrue(result[3]); assertFalse(result[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); }
/** * 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); }
booleanArray = new boolean[length]; array.getBooleans(0, length, booleanArray); return booleanArray; } else if (arrayType == V8Value.STRING) {
booleanArray = new boolean[length]; array.getBooleans(0, length, booleanArray); return booleanArray; } else if (arrayType == V8Value.STRING) {