@SuppressWarnings("unchecked") private static Object getV8Result(final V8 v8, final Object value, final Map<Object, V8Value> cache) { if (cache.containsKey(value)) { return cache.get(value); } if (value instanceof Map<?, ?>) { return toV8Object(v8, (Map<String, ? extends Object>) value, cache); } else if (value instanceof List<?>) { return toV8Array(v8, (List<? extends Object>) value, cache); } else if (value instanceof TypedArray) { return toV8TypedArray(v8, (TypedArray) value, cache); } else if (value instanceof ArrayBuffer) { return toV8ArrayBuffer(v8, (ArrayBuffer) value, cache); } return value; }
/** * 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); }
Object result = getV8Result(v8, value, cache); if (result instanceof V8Value) { return ((V8Value) result).twin();
private static Object getValue(final Object value, final int valueType, final V8Map<Object> cache, final TypeAdapter adapter) { Object adapterResult = adapter.adapt(valueType, value); if (TypeAdapter.DEFAULT != adapterResult) { return adapterResult; } switch (valueType) { case V8Value.INTEGER: case V8Value.DOUBLE: case V8Value.BOOLEAN: case V8Value.STRING: return value; case V8Value.V8_FUNCTION: return IGNORE; case V8Value.V8_ARRAY_BUFFER: return new ArrayBuffer((V8ArrayBuffer) value); case V8Value.V8_TYPED_ARRAY: return new TypedArray((V8TypedArray) value); case V8Value.V8_ARRAY: return toList((V8Array) value, cache, adapter); case V8Value.V8_OBJECT: return toMap((V8Object) value, cache, adapter); case V8Value.NULL: return null; case V8Value.UNDEFINED: return V8.getUndefined(); default: throw new IllegalStateException("Cannot convert type " + V8Value.getStringRepresentation(valueType)); } }
.collect(Collectors.joining("\n ", "{\n ", "\n}"))); v8.add("$options", toV8Object(v8, options)); .onFailure(x -> { log.debug("png-fallback resulted in exception", x); callback.call(null, toV8Array(v8, Arrays.asList(x.getMessage()))); }); return V8.UNDEFINED;
/** * Pushes a Java Object to a V8Array by first converting it to a V8Value if needed. * If the value is a boxed primitive, then the primitive will be pushed. If the object * is a Map / List then a deep copy will be performed, converting the object to a * V8Object / V8Array first. * * @param v8 The runtime on which to create any needed V8Values. * @param array The array to push the elements to. * @param value The value to push to the array. */ public static void pushValue(final V8 v8, final V8Array array, final Object value) { Map<Object, V8Value> cache = new Hashtable<Object, V8Value>(); try { pushValue(v8, array, value, cache); } finally { for (V8Value v8Object : cache.values()) { v8Object.close(); } } }
public V8Object hash(final Map<String, Object> hash) { return register(v8, V8ObjectUtils.toV8Object(v8, hash)); }
public V8Array array(final List<? extends Object> value) { return register(v8, V8ObjectUtils.toV8Array(v8, value)); }
@Test public void testCloneV8Array() { V8Array list = v8.executeArrayScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Array v8Object = V8ObjectUtils.toV8Array(v8, V8ObjectUtils.toList(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }
@Test public void testCloneV8Object() { V8Object list = v8.executeObjectScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Object v8Object = V8ObjectUtils.toV8Object(v8, V8ObjectUtils.toMap(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }
/** * Creates a List from a V8Array using a deep copy. All elements * in the V8Array are released after they are accessed. However, the root * array itself is not released. * * @param array The root of the V8Array graph. * * @return A list representing a deep copy of the V8Array rooted at 'array'. */ public static List<? super Object> toList(final V8Array array) { return toList(array, DEFAULT_TYPE_ADAPTER); }
/** * Creates a Map from a V8Object using a deep copy. All elements * in the V8Object are released after they are accessed. However, the root * object itself is not released. * * @param object The root of the V8Object graph. * * @return A map representing a deep copy of the V8Object rooted at 'object'. */ public static Map<String, ? super Object> toMap(final V8Object object) { return toMap(object, DEFAULT_TYPE_ADAPTER); }
@SuppressWarnings("unchecked") @Test public void testCloneV8Array_GetValue() { V8Array list = v8.executeArrayScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Array v8Object = V8ObjectUtils.toV8Array(v8, (List<? extends Object>) V8ObjectUtils.getValue(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }
@SuppressWarnings("unchecked") @Test public void testCloneV8ObjectsWithCircularStructure_GetValue() { V8Object parent = v8.executeObjectScript("var parent = {};\n" + "var child = {parent : parent};\n" + "parent.child = child\n;" + "parent;"); V8Object v8Object = V8ObjectUtils.toV8Object(v8, (Map<String, ? extends Object>) V8ObjectUtils.getValue(parent)); assertEquals(1, v8Object.getKeys().length); assertEquals("child", v8Object.getKeys()[0]); parent.close(); v8Object.close(); }
private static V8Object toV8Object(final V8 v8, final Map<String, ? extends Object> map, final Map<Object, V8Value> cache) { if (cache.containsKey(map)) { return (V8Object) cache.get(map); } V8Object result = new V8Object(v8); cache.put(map, result); try { for (Entry<String, ? extends Object> entry : map.entrySet()) { setValue(v8, result, entry.getKey(), entry.getValue(), cache); } } catch (IllegalStateException e) { result.close(); throw e; } return result; }
private static V8Array toV8Array(final V8 v8, final List<? extends Object> list, final Map<Object, V8Value> cache) { if (cache.containsKey(new ListWrapper(list))) { return (V8Array) cache.get(new ListWrapper(list)); } V8Array result = new V8Array(v8); cache.put(new ListWrapper(list), result); try { for (int i = 0; i < list.size(); i++) { Object value = list.get(i); pushValue(v8, result, value, cache); } } catch (IllegalStateException e) { result.close(); throw e; } return result; }
/** * Creates a V8Object from a java.util.Map. This is a deep copy, so if the map * contains other maps (or lists) they will also be converted. * * @param v8 The runtime on which to create the result. * @param map The map to convert to a V8Object. * * @return A V8Object representing the map. */ public static V8Object toV8Object(final V8 v8, final Map<String, ? extends Object> map) { Map<Object, V8Value> cache = new Hashtable<Object, V8Value>(); try { return toV8Object(v8, map, cache).twin(); } finally { for (V8Value v8Object : cache.values()) { v8Object.close(); } } }
/** * Creates a V8Array from a java.util.List. This is a deep copy, so if the list * contains other lists (or maps) they will also be converted. * * @param v8 The runtime on which to create the result. * @param list The list to convert to a V8Array. * * @return A V8Array representing the list. */ public static V8Array toV8Array(final V8 v8, final List<? extends Object> list) { Map<Object, V8Value> cache = new Hashtable<Object, V8Value>(); try { return toV8Array(v8, list, cache).twin(); } finally { for (V8Value v8Object : cache.values()) { v8Object.close(); } } }
@Override public void run(final V8 runtime) { runtime.registerJavaMethod(new Sort(), "_sort"); runtime.executeVoidScript(sortAlgorithm); V8Array parameters = V8ObjectUtils.toV8Array(runtime, data); V8Array _result = runtime.executeArrayFunction("sort", parameters); result = V8ObjectUtils.toList(_result); _result.close(); parameters.close(); } });
@Test public void testCloneV8ObjectsWithCircularStructure() { V8Object parent = v8.executeObjectScript("var parent = {};\n" + "var child = {parent : parent};\n" + "parent.child = child\n;" + "parent;"); V8Object v8Object = V8ObjectUtils.toV8Object(v8, V8ObjectUtils.toMap(parent)); assertEquals(1, v8Object.getKeys().length); assertEquals("child", v8Object.getKeys()[0]); parent.close(); v8Object.close(); }