/** * 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); }
stringArray = new String[length]; array.getStrings(0, length, stringArray); return stringArray; } else if (arrayType == V8Value.BYTE) {
/** * 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 testGetStrings2Undefined() { V8Array undefined = v8.getArray("array"); undefined.getStrings(0, 1, new String[1]); }
@Test(expected = UnsupportedOperationException.class) public void testGetStringsUndefined() { V8Array undefined = v8.getArray("array"); undefined.getStrings(0, 1); }
@Test(expected = IndexOutOfBoundsException.class) public void testGetStringSmallerArray() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd']"); String[] result = new String[3]; try { a.getStrings(0, 4, result); } finally { a.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetSubArrayOfStringsOutOfBounds() { V8Array a = v8.executeArrayScript("['a', 7, 'c', 'd', 'e']"); try { a.getStrings(3, 3); } finally { a.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetStringsWithoutStrings() { V8Array a = v8.executeArrayScript("['a', 7, 'c', 'd', 'e'];"); try { a.getStrings(0, 5); } finally { a.close(); } }
stringArray = new String[length]; array.getStrings(0, length, stringArray); return stringArray; } else if (arrayType == V8Value.BYTE) {
@Test public void testGetStringSameSizeArray() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd']"); String[] result = new String[4]; int size = a.getStrings(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetStringBiggerArray() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd']"); String[] result = new String[40]; int size = a.getStrings(0, 4, result); assertEquals(4, size); a.close(); }
@Test public void testGetSubArrayOfStrings() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd', 'e'];"); String[] result = a.getStrings(4, 1); assertEquals(1, result.length); assertEquals("e", result[0]); a.close(); }
@Test public void testGetSubArrayOfStrings2() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd', 'e'];"); String[] result = a.getStrings(3, 2); assertEquals(2, result.length); assertEquals("d", result[0]); assertEquals("e", result[1]); a.close(); }
@Test public void testArrayGetStrings_Unicode() { V8Array array = v8.executeArrayScript("['🎉','🌞','💐'];"); String[] result = array.getStrings(0, 3); assertEquals("🎉", result[0]); assertEquals("🌞", result[1]); assertEquals("💐", result[2]); array.close(); }
@Test public void testGetStringPopulatesArray() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd']"); String[] result = new String[4]; a.getStrings(0, 4, result); assertEquals("a", result[0]); assertEquals("b", result[1]); assertEquals("c", result[2]); assertEquals("d", result[3]); a.close(); }
@Test public void testGetArrayOfStrings() { V8Array a = v8.executeArrayScript("['a', 'b', 'c', 'd', 'e'];"); String[] result = a.getStrings(0, 5); assertEquals(5, result.length); assertEquals("a", result[0]); assertEquals("b", result[1]); assertEquals("c", result[2]); assertEquals("d", result[3]); assertEquals("e", 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); }
stringArray = new String[length]; array.getStrings(0, length, stringArray); return stringArray; } else if (arrayType == V8Value.BYTE) {
stringArray = new String[length]; array.getStrings(0, length, stringArray); return stringArray; } else if (arrayType == V8Value.BYTE) {