public V8Array array(final List<? extends Object> value) { return register(v8, V8ObjectUtils.toV8Array(v8, value)); }
@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; }
result.push(object); } else if (value instanceof List) { V8Array array = toV8Array(v8, (List) value, cache); result.push(array); } else {
/** * 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(); } } }
result.add(key, object); } else if (value instanceof List) { V8Array array = toV8Array(v8, (List) value, cache); result.add(key, array); } else {
.onFailure(x -> { log.debug("png-fallback resulted in exception", x); callback.call(null, toV8Array(v8, Arrays.asList(x.getMessage()))); }); return V8.UNDEFINED;
@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; }
/** * 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(); } } }
result.push(object); } else if (value instanceof List) { V8Array array = toV8Array(v8, (List) value, cache); result.push(array); } else {
@Test public void testListContainsSelf() { List<Object> list = new ArrayList<Object>(); list.add(list); V8Array v8Array = V8ObjectUtils.toV8Array(v8, list); V8Object self = v8Array.getObject(0); assertEquals(v8Array, self); self.close(); v8Array.close(); }
result.add(key, object); } else if (value instanceof List) { V8Array array = toV8Array(v8, (List) value, cache); result.add(key, array); } else {
private int registerAndRelease(final String name, final List<? extends Object> list) { V8Array array = V8ObjectUtils.toV8Array(v8, list); v8.add(name, array); int size = array.length(); array.close(); return size; }
@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; }
@Test public void testArrayBufferInList() { ArrayBuffer arrayBuffer = new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)); List<Object> list = new ArrayList<Object>(); list.add(arrayBuffer); V8Array array = V8ObjectUtils.toV8Array(v8, list); V8ArrayBuffer v8ArrayBuffer = (V8ArrayBuffer) array.get(0); assertEquals(arrayBuffer.getV8ArrayBuffer().setWeak(), v8ArrayBuffer); v8ArrayBuffer.close(); array.close(); }
@Test public void testV8ArrayBufferInList() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 10); List<Object> list = new ArrayList<Object>(); list.add(buffer); V8Array v8Array = V8ObjectUtils.toV8Array(v8, list); V8Value result = (V8Value) v8Array.get(0); assertEquals(buffer, result); buffer.close(); result.close(); v8Array.close(); }
@Override public Object invoke(final V8Object receiver, final V8Array parameters) { final List<Object> data = V8ObjectUtils.toList(parameters); V8Thread t = new V8Thread(new V8Runnable() { @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(); } }); t.start(); try { t.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } return V8ObjectUtils.toV8Array(parameters.getRuntime(), result); } }
@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(); }
@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(); }
@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 testTypedArrayInList() { TypedArray int8Array = new TypedArray(v8, new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)), V8Value.INT_8_ARRAY, 0, 8); V8TypedArray v8TypedArray = int8Array.getV8TypedArray(); V8ArrayBuffer v8ArrayBuffer = v8TypedArray.getBuffer(); v8ArrayBuffer.put(0, (byte) 7); v8TypedArray.close(); v8ArrayBuffer.close(); List<Object> list = new ArrayList<Object>(); list.add(int8Array); V8Array array = V8ObjectUtils.toV8Array(v8, list); V8Array v8Array = (V8Array) array.get(0); assertEquals((byte) 7, v8Array.get(0)); assertEquals(V8Value.INT_8_ARRAY, v8Array.getType()); v8Array.close(); array.close(); }