private Object getArrayItem(final V8Array array, final int index) { try { int type = array.getType(index); switch (type) { case INTEGER: return array.getInteger(index); case DOUBLE: return array.getDouble(index); case BOOLEAN: return array.getBoolean(index); case STRING: return array.getString(index); case V8_ARRAY: case V8_TYPED_ARRAY: return array.getArray(index); case V8_OBJECT: return array.getObject(index); case V8_FUNCTION: return array.getObject(index); case V8_ARRAY_BUFFER: return array.get(index); case UNDEFINED: return V8.getUndefined(); } } catch (V8ResultUndefined e) { // do nothing } return null; }
/** * Returns all the property names for the given object. * * @param kind Indicate whether named, indexed or both kinds of properties are requested. * @param limit Limit the number of properties returned to the specified value * @return All the property names for a given object */ public String[] getPropertyNames(final PropertyKind kind, final int limit) { V8Array parameters = new V8Array(v8Object.getRuntime()); parameters.push(kind.index); parameters.push(limit); V8Array propertyNames = null; try { propertyNames = v8Object.executeArrayFunction(PROPERTY_NAMES, parameters); String[] result = new String[propertyNames.length()]; for (int i = 0; i < result.length; i++) { result[i] = propertyNames.getString(i); } return result; } finally { parameters.close(); if (propertyNames != null) { propertyNames.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); }
@Test public void testAddBoolean() { V8Array array = new V8Array(v8); array.push(true); array.push(false); assertEquals(2, array.length()); assertTrue(array.getBoolean(0)); assertFalse(array.getBoolean(1)); array.close(); }
@Test public void testGetArrayElementFromProperties() { V8Array v8Array = new V8Array(v8); v8Array.push("1").push(2).push(3.3); String result1 = v8Array.getString("0"); int result2 = v8Array.getInteger("1"); double result3 = v8Array.getDouble("2"); assertEquals("1", result1); assertEquals(2, result2); assertEquals(3.3, result3, 0.000001); v8Array.close(); }
@Test public void testArrayFunctionCall() { v8.executeVoidScript("function add(a,b,c,d) {return [a,b,c,d];}"); V8Array parameters = new V8Array(v8); parameters.push(true); parameters.push(false); parameters.push(7); parameters.push("foo"); V8Array result = v8.executeArrayFunction("add", parameters); assertTrue(result.getBoolean(0)); assertFalse(result.getBoolean(1)); assertEquals(7, result.getInteger(2)); assertEquals("foo", result.getString(3)); parameters.close(); result.close(); }
/**** Mixed Array ****/ @Test public void testMixedArray() { V8Array array = v8.executeArrayScript("['a', 3, 3.1, true];"); assertEquals(4, array.length()); assertEquals("a", array.getString(0)); assertEquals(3, array.getInteger(1)); assertEquals(3.1, array.getDouble(2), 0.00001); assertTrue(array.getBoolean(3)); array.close(); }
@SuppressWarnings("unchecked") private static List<? super Object> toList(final V8Array array, final V8Map<Object> cache, final TypeAdapter adapter) { if (array == null) { return Collections.emptyList(); } if (cache.containsKey(array)) { return (List<? super Object>) cache.get(array); } List<? super Object> result = new ArrayList<Object>(); cache.put(array, result); for (int i = 0; i < array.length(); i++) { Object object = null; int type = V8Value.UNDEFINED; try { object = array.get(i); type = array.getType(i); Object value = getValue(object, type, cache, adapter); if (value != IGNORE) { result.add(value); } } finally { if (object instanceof Releasable) { ((Releasable) object).release(); } } } return result; }
/*** Get Array ***/ @Test public void testArrayGetArray() { V8Array array = v8.executeArrayScript("[[1,2,3],['first','second'],[true]];"); V8Array array1 = array.getArray(0); V8Array array2 = array.getArray(1); V8Array array3 = array.getArray(2); assertEquals(3, array1.length()); assertEquals(2, array2.length()); assertEquals(1, array3.length()); array.close(); array1.close(); array2.close(); array3.close(); }
private static void b64(final V8 v8) { v8.registerJavaMethod((JavaCallback) (receiver, args) -> { byte[] bytes = args.get(0).toString().getBytes(StandardCharsets.UTF_8); return BaseEncoding.base64().encode(bytes); }, "btoa"); v8.registerJavaMethod((JavaCallback) (receiver, args) -> { byte[] atob = BaseEncoding.base64().decode(args.get(0).toString()); return new String(atob, StandardCharsets.UTF_8); }, "atob"); }
/** * Get all the BreakPoint IDs as an array. * * @return A list of BreakPoint IDs. */ public int[] getScriptBreakPointIDs() { V8Array breakPoints = debugObject.executeArrayFunction(SCRIPT_BREAK_POINTS, null); try { int[] result = new int[breakPoints.length()]; for (int i = 0; i < breakPoints.length(); i++) { V8Object breakPoint = breakPoints.getObject(i); try { result[i] = breakPoint.executeIntegerFunction(NUMBER, null); } finally { breakPoint.close(); } } return result; } finally { breakPoints.close(); } }
@Test(expected = V8ResultUndefined.class) public void testGetBooleansWithoutBooleans() { V8Array a = v8.executeArrayScript("[true, 'a', false, false, true];"); try { a.getBooleans(0, 5); } finally { a.close(); } }