/** * 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); }
doubleArray = new double[length]; array.getDoubles(0, length, doubleArray); return doubleArray; } else if (arrayType == V8Value.BOOLEAN) {
/** * 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 testGetDoubles2Undefined() { V8Array undefined = v8.getArray("array"); undefined.getDoubles(0, 1, new double[1]); }
@Test(expected = UnsupportedOperationException.class) public void testGetDoublesUndefined() { V8Array undefined = v8.getArray("array"); undefined.getDoubles(0, 1); }
doubleArray = new double[length]; array.getDoubles(0, length, doubleArray); return doubleArray; } else if (arrayType == V8Value.BOOLEAN) {
@Test(expected = IndexOutOfBoundsException.class) public void testGetDoublesSmallerArray() { V8Array a = v8.executeArrayScript("[1,2,3.3,4.4]"); double[] result = new double[3]; try { a.getDoubles(0, 4, result); } finally { a.close(); } }
@Test public void testGetDoubleSameSizeArray() { V8Array a = v8.executeArrayScript("[1,2.2,3.3,4]"); double[] result = new double[4]; int size = a.getDoubles(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetDoublesBiggerArray() { V8Array a = v8.executeArrayScript("[1.1,2.2,3,4]"); double[] result = new double[40]; int size = a.getDoubles(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetSubArrayOfDoubles() { V8Array a = v8.executeArrayScript("[1.1,2.1,3.1,4.1,5.1];"); double[] result = a.getDoubles(4, 1); assertEquals(1, result.length); assertEquals(5.1, result[0], 0.000001); a.close(); }
@Test public void testGetSubArrayOfDoubles2() { V8Array a = v8.executeArrayScript("[1.1,2.1,3.1,4.1,5.1];"); double[] result = a.getDoubles(3, 2); assertEquals(2, result.length); assertEquals(4.1, result[0], 0.000001); assertEquals(5.1, result[1], 0.000001); a.close(); }
@Test public void testGetDoublesPopulatesArray() { V8Array a = v8.executeArrayScript("[1.1,2.2,3.3,4]"); double[] result = new double[4]; a.getDoubles(0, 4, result); assertEquals(1.1, result[0], 0.000001); assertEquals(2.2, result[1], 0.000001); assertEquals(3.3, result[2], 0.000001); assertEquals(4, result[3], 0.000001); a.close(); }
@Test public void testGetArrayOfDoubles() { V8Array a = v8.executeArrayScript("[1.1,2.1,3.1,4.1,5.1];"); double[] result = a.getDoubles(0, 5); assertEquals(5, result.length); assertEquals(1.1, result[0], 000001); assertEquals(2.1, result[1], 000001); assertEquals(3.1, result[2], 000001); assertEquals(4.1, result[3], 000001); assertEquals(5.1, result[4], 000001); 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); }
doubleArray = new double[length]; array.getDoubles(0, length, doubleArray); return doubleArray; } else if (arrayType == V8Value.BOOLEAN) {
doubleArray = new double[length]; array.getDoubles(0, length, doubleArray); return doubleArray; } else if (arrayType == V8Value.BOOLEAN) {