public V8Object hash(final Map<String, Object> hash) { return register(v8, V8ObjectUtils.toV8Object(v8, hash)); }
/** * 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(); } } }
@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; }
.collect(Collectors.joining("\n ", "{\n ", "\n}"))); v8.add("$options", toV8Object(v8, options));
result.push((V8Value) value); } else if (value instanceof Map) { V8Object object = toV8Object(v8, (Map) value, cache); result.push(object); } else if (value instanceof List) {
result.add(key, (V8Value) value); } else if (value instanceof Map) { V8Object object = toV8Object(v8, (Map) value, cache); result.add(key, object); } else if (value instanceof List) {
/** * 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(); } } }
@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; }
public void testParentChildMap() { Map<String, Object> parent = new Hashtable<String, Object>(); Map<String, Object> child = new Hashtable<String, Object>(); parent.put("child", child); child.put("parent", parent); V8Object v8Object = V8ObjectUtils.toV8Object(v8, parent); V8Object v8_child = v8Object.getObject("child"); V8Object v8_parent = v8_child.getObject("parent"); assertEquals(v8Object, v8_parent); v8Object.close(); v8_child.close(); v8_parent.close(); }
private int registerAndRelease(final String name, final Map<String, ? extends Object> map) { V8Object object = V8ObjectUtils.toV8Object(v8, map); v8.add(name, object); int size = getNumberOfProperties(object); object.close(); return size; }
@Test public void testMapContainsSelf() { Map<String, Object> map = new Hashtable<String, Object>(); map.put("self", map); map.put("self2", map); map.put("self3", map); V8Object v8Object = V8ObjectUtils.toV8Object(v8, map); V8Object self = v8Object.getObject("self"); assertEquals(v8Object, self); assertEquals(3, v8Object.getKeys().length); v8Object.close(); self.close(); }
@Test public void testByteBufferInMap() { ArrayBuffer arrayBuffer = new ArrayBuffer(v8, ByteBuffer.allocateDirect(8)); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("buffer", arrayBuffer); V8Object object = V8ObjectUtils.toV8Object(v8, map); V8ArrayBuffer v8ArrayBuffer = (V8ArrayBuffer) object.get("buffer"); assertEquals(arrayBuffer.getV8ArrayBuffer().setWeak(), v8ArrayBuffer); v8ArrayBuffer.close(); object.close(); }
@Test public void testV8ArrayBufferInMap() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 10); Map<String, Object> map = new HashMap<String, Object>(); map.put("buffer", buffer); V8Object v8Object = V8ObjectUtils.toV8Object(v8, map); V8Value result = (V8Value) v8Object.get("buffer"); assertEquals(buffer, result); buffer.close(); result.close(); v8Object.close(); }
result.push((V8Value) value); } else if (value instanceof Map) { V8Object object = toV8Object(v8, (Map) value, cache); result.push(object); } else if (value instanceof List) {
result.add(key, (V8Value) value); } else if (value instanceof Map) { V8Object object = toV8Object(v8, (Map) value, cache); result.add(key, object); } else if (value instanceof List) {
@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(); }
@Test public void testTypedArrayInMap() { 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(); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("array", int8Array); V8Object object = V8ObjectUtils.toV8Object(v8, map); V8TypedArray v8Array = (V8TypedArray) object.get("array"); assertEquals((byte) 7, v8Array.get(0)); assertEquals(V8Value.INT_8_ARRAY, v8Array.getType()); v8Array.close(); object.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(); }
@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(); }
@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; }