@Override public void putAll(final Map<? extends V8Value, ? extends V> m) { for (java.util.Map.Entry<? extends V8Value, ? extends V> entry : m.entrySet()) { this.put(entry.getKey(), entry.getValue()); } }
@Test public void testAddAllWithDuplicates() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); V8Map<String> newMap = new V8Map<String>(); newMap.put(v1, "bar"); v1.close(); newMap.putAll(map); map.close(); assertEquals(1, newMap.size()); assertEquals("foo", newMap.values().iterator().next()); newMap.close(); }
/** * Registers an executor with this runtime. An executor is another * runtime with its own thread. By registering an executor, it can be * terminated when this runtime is released. * * @param key The key to associate the executor with. * @param executor The executor itself. */ public void registerV8Executor(final V8Object key, final V8Executor executor) { checkThread(); if (executors == null) { executors = new V8Map<V8Executor>(); } executors.put(key, executor); }
@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; }
@Override public void putAll(final Map<? extends V8Value, ? extends V> m) { for (java.util.Map.Entry<? extends V8Value, ? extends V> entry : m.entrySet()) { this.put(entry.getKey(), entry.getValue()); } }
@Test public void testEntrySet() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); Set<Entry<V8Value, String>> entrySet = map.entrySet(); assertEquals(1, entrySet.size()); assertEquals(v1, entrySet.iterator().next().getKey()); assertEquals("foo", entrySet.iterator().next().getValue()); v1.close(); map.close(); }
@Test public void testValues() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); Collection<String> values = map.values(); assertEquals(1, values.size()); assertEquals("foo", values.iterator().next()); v1.close(); map.close(); }
@Test public void testKeyset() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); Set<V8Value> keySet = map.keySet(); assertEquals(1, keySet.size()); assertEquals(v1, keySet.iterator().next()); v1.close(); map.close(); }
@Test public void testKeysetNotReleased() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); v1.close(); Set<V8Value> keySet = map.keySet(); assertEquals(1, keySet.size()); assertFalse(keySet.iterator().next().isReleased()); map.close(); }
@Test public void testReleaseMapReleasesKeys() { V8Object v8Object = new V8Object(v8); V8Map<String> map = new V8Map<String>(); map.put(v8Object, "foo"); v8Object.close(); map.close(); }
@Test public void testIsNotEmpty() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); v1.close(); assertFalse(map.isEmpty()); map.close(); }
@Test public void testReleaseAttachedMap() { V8 runtime = V8.createV8Runtime(); V8Map<String> v8Map = new V8Map<String>(); V8Object v8Object = new V8Object(runtime); v8Map.put(v8Object, "foo"); v8Object.close(); runtime.registerResource(v8Map); runtime.release(true); }
@Test public void testRemoveKeyReleasesKey() { V8Map<String> map = new V8Map<String>(); v8.executeScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); map.remove(v8Object); v8Object.close(); map.close(); }
@Test public void testContainsKey() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); assertTrue(map.containsKey(v8Object)); v8Object.close(); map.close(); }
@Test public void testGet() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); assertEquals("foo", map.get(v8Object)); v8Object.close(); map.close(); }
@Test public void testContainsValue() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); assertTrue(map.containsValue("foo")); v8Object.close(); map.close(); }
@Test public void testRemove() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); map.put(v8Object, "foo"); assertEquals("foo", map.remove(v8Object)); v8Object.close(); map.close(); }
@Test public void testPutUndefined() { V8Map<Object> map = new V8Map<Object>(); V8Object v1 = new V8Object(v8); map.put(v1, V8.getUndefined()); assertEquals(V8.getUndefined(), map.get(v1)); v1.close(); map.close(); }