/** * Gets a Java Object representing the value with the given key in the V8Object. * If the value is a primitive (int, boolean or double) then a boxed instance * is returned. If the value is a String, then a String is returned. If * the value is a V8Object or V8Array, then a Map or List is returned. * * @param object The object on which to lookup the value. The object is not * released. * @param key The key to use to lookup the value. * * @return A Java Object representing the value at a given key. */ public static Object getValue(final V8Object object, final String key) { return getValue(object, key, DEFAULT_TYPE_ADAPTER); }
/** * Create a Java Object from a result from V8. V8 can return * basic Java types, or V8Values (V8Object, V8Array, etc...). This method * will attempt to convert the result into a pure Java object using a * deep copy. * * If the input is basic Java type (Integer, Double, Boolean, String) * it will be returned. If the input is a V8Value, it will be converted. * * All elements in the V8Object are released after they are accessed. * However, the root object itself is not released. * * @param v8Object The input to convert. * @return A Java object representing the input. */ public static Object getValue(final Object v8Object) { return getValue(v8Object, DEFAULT_TYPE_ADAPTER); }
@SuppressWarnings("unchecked") private List<String> filter(final V8Array args, final int i) { if (i < args.length()) { Object value = V8ObjectUtils.getValue(args, i); if (value == V8.getUndefined()) { return Collections.emptyList(); } List<String> filter = new ArrayList<>(); if (value instanceof Collection) { filter.addAll((Collection<? extends String>) value); } else { filter.add(value.toString()); } return filter; } return Collections.emptyList(); } }
/** * Create a Java Object from a result from V8 using a {@link TypeAdapter} to convert * objects. V8 can return basic Java types or V8Values (V8Object, V8Array, etc...). This * method will attempt to convert the result into a pure Java object using * a deep copy. * * If the input is basic Java type (Integer, Double, Boolean, String) * it will be returned. If the input is a V8Value, it will be converted. * * All elements in the V8Object are released after they are accessed. * However, the root object itself is not released. * * @param v8Object The input to convert. * @param adapter The {@link TypeAdapter} to use for the object conversions. * @return A Java object representing the input. */ public static Object getValue(final Object v8Object, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { if (v8Object instanceof V8Value) { int type = ((V8Value) v8Object).getV8Type(); return getValue(v8Object, type, cache, adapter); } else { return v8Object; } } finally { cache.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; }
@SuppressWarnings("unchecked") private static Map<String, ? super Object> toMap(final V8Object v8Object, final V8Map<Object> cache, final TypeAdapter adapter) { if (v8Object == null) { return Collections.emptyMap(); } if (cache.containsKey(v8Object)) { return (Map<String, ? super Object>) cache.get(v8Object); } Map<String, ? super Object> result = new V8PropertyMap<Object>(); cache.put(v8Object, result); String[] keys = v8Object.getKeys(); for (String key : keys) { Object object = null; int type = V8Value.UNDEFINED; try { object = v8Object.get(key); type = v8Object.getType(key); Object value = getValue(object, type, cache, adapter); if (value != IGNORE) { result.put(key, value); } } finally { if (object instanceof Releasable) { ((Releasable) object).release(); } } } return result; }
object = array.get(index); type = array.getType(index); Object result = getValue(object, type, cache, DEFAULT_TYPE_ADAPTER); if ((result == object) && (result instanceof V8Value)) { return ((V8Value) result).twin();
object = array.get(index); type = array.getType(index); Object result = getValue(object, type, cache, adapter); if ((result == object) && (result instanceof V8Value)) { return ((V8Value) result).twin();
object = v8Object.get(key); type = v8Object.getType(key); Object result = getValue(object, type, cache, adapter); if ((result == object) && (result instanceof V8Value)) { return ((V8Value) result).twin();
@SuppressWarnings("unchecked") @Test public void testEqualSiblings_GetValue() { V8Object parent = v8.executeObjectScript("var parent = {};\n" + "var child = {parent : parent};\n" + "parent.child1 = child\n;" + "parent.child2 = child\n;" + "parent;"); Map<String, Object> map = (Map<String, Object>) V8ObjectUtils.getValue(parent); assertEquals((map.get("child2")), (map.get("child1"))); parent.close(); }
@Override public Boolean invoke(final V8Object receiver, final V8Array parameters) { V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(parameters, 0)).getV8TypedArray(); try { return result.getType() == V8Value.FLOAT_64_ARRAY; } finally { result.close(); } } };
@SuppressWarnings("unchecked") @Test public void testCreateIntegerMapFromV8Object_GetValue() { V8Object object = v8.executeObjectScript("x = {a:1, b:2, c:3}; x"); Map<String, ? super Object> map = (Map<String, ? super Object>) V8ObjectUtils.getValue(object); assertEquals(3, map.size()); assertEquals(1, map.get("a")); assertEquals(2, map.get("b")); assertEquals(3, map.get("c")); object.close(); }
@SuppressWarnings("unchecked") @Test public void testCreateMixedMapFromV8Object_GetValue() { V8Object object = v8.executeObjectScript("x = {boolean:true, integer:1, double:3.14159, string:'hello'}; x"); Map<String, ? super Object> map = (Map<String, ? super Object>) V8ObjectUtils.getValue(object); assertEquals(4, map.size()); assertTrue((Boolean) map.get("boolean")); assertEquals(1, (int) (Integer) map.get("integer")); assertEquals(3.14159, (Double) map.get("double"), 0.0000001); assertEquals("hello", map.get("string")); object.close(); }
@SuppressWarnings("unchecked") @Test public void testCreateMapWithNulls_GetValue() { V8Object object = v8.executeObjectScript("x = {a:null}; x;"); Map<String, ? super Object> map = (Map<String, ? super Object>) V8ObjectUtils.getValue(object); assertEquals(1, map.size()); assertNull(map.get("a")); object.close(); }
@Override public Boolean invoke(final V8Object receiver, final V8Array parameters) { V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(parameters, 0)).getV8TypedArray(); try { return result.getType() == V8Value.FLOAT_32_ARRAY; } finally { result.close(); } } };
@Test public void testGetBooleanValueFromArray() { V8Array array = v8.executeArrayScript("[true, false]"); assertEquals(Boolean.TRUE, V8ObjectUtils.getValue(array, 0)); array.close(); }
@Test public void testObjectTypeAdapter_Map_GetValue() { V8Object object = v8.executeObjectScript("x = {b:{a:{}}}; x"); Object result = V8ObjectUtils.getValue(object, new SingleTypeAdapter(V8Value.V8_OBJECT) { @Override public Object adapt(final Object value) { return "object"; } }); assertEquals("object", result); object.close(); }
@Test public void testArrayBufferGetValue() { V8ArrayBuffer buf = (V8ArrayBuffer) v8.executeScript("var buf = new ArrayBuffer(100);\n" + "buf;\n"); V8ArrayBuffer result = ((ArrayBuffer) V8ObjectUtils.getValue(buf)).getV8ArrayBuffer(); assertEquals(100, result.limit()); buf.close(); result.close(); }
@Test public void testTypedArrayGetValue_Uint8ClampedArray() { V8Array int8ClampedArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var int8ClampedArray = new Uint8ClampedArray(buf);\n" + "int8ClampedArray[0] = 16;\n" + "int8ClampedArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(int8ClampedArray)).getV8TypedArray(); assertEquals(100, result.length()); assertEquals((short) 16, result.get(0)); int8ClampedArray.close(); result.close(); }
@Test public void testTypedArrayGetValue_Uint16Array() { V8Array intsArray = v8.executeArrayScript("var buf = new ArrayBuffer(100);\n" + "var intsArray = new Uint16Array(buf);\n" + "intsArray[0] = 16;\n" + "intsArray;\n"); V8TypedArray result = ((TypedArray) V8ObjectUtils.getValue(intsArray)).getV8TypedArray(); assertEquals(50, result.length()); assertEquals(16, result.get(0)); intsArray.close(); result.close(); }