/** * 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(); } } }
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; }
/** * 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(); } } }
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; }
@Test public void testPushMap() { V8Array array = new V8Array(v8); Map<String, String> map = new HashMap<String, String>(); map.put("foo", "bar"); V8ObjectUtils.pushValue(v8, array, map); V8Object result = array.getObject(0); assertEquals("bar", result.getString("foo")); result.close(); array.close(); }
@Test public void testPushList() { V8Array array = new V8Array(v8); List<String> list = new ArrayList<String>(); list.add("one"); V8ObjectUtils.pushValue(v8, array, list); V8Array result = array.getArray(0); assertEquals("one", result.getString(0)); result.close(); array.close(); }
@Test public void testPushDouble() { V8Array array = new V8Array(v8); V8ObjectUtils.pushValue(v8, array, 7.8); assertEquals(7.8, array.getDouble(0), 0.000001); array.close(); }
@Test public void testPushBoolean() { V8Array array = new V8Array(v8); V8ObjectUtils.pushValue(v8, array, true); assertTrue(array.getBoolean(0)); array.close(); }
@Test public void testPushString() { V8Array array = new V8Array(v8); V8ObjectUtils.pushValue(v8, array, "string"); assertEquals("string", array.getString(0)); array.close(); }
@Test public void testPushInteger() { V8Array array = new V8Array(v8); V8ObjectUtils.pushValue(v8, array, 7); assertEquals(7, array.getInteger(0)); array.close(); }
@Test public void testPushV8ObjectToArray() { V8Array array = new V8Array(v8); V8Object child = new V8Object(v8); V8ObjectUtils.pushValue(v8, array, child); V8Object result = (V8Object) array.get(0); assertEquals(child, result); array.close(); result.close(); child.close(); }
@Test public void testPushV8ArrayToArray() { V8Array array = new V8Array(v8); V8Array child = new V8Array(v8); V8ObjectUtils.pushValue(v8, array, child); V8Object result = (V8Object) array.get(0); assertEquals(child, result); array.close(); result.close(); child.close(); }
@Test public void testPushV8TypedArrayToArray() { V8Array array = new V8Array(v8); V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 10); V8Object child = new V8TypedArray(v8, buffer, V8Value.INT_8_ARRAY, 0, 10); V8ObjectUtils.pushValue(v8, array, child); V8Object result = (V8Object) array.get(0); assertEquals(child, result); array.close(); result.close(); child.close(); buffer.close(); }
/** * 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.release(); } } }
/** * 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.release(); } } }
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.release(); 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.release(); throw e; } return result; }