/** * 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); }
/** * Creates a List from a V8Array using a deep copy and a TypeAdapter to handle * type conversions. 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. * @param adapter The {@link TypeAdapter} to use for the object conversions. * * @return A list representing a deep copy of the V8Array rooted at 'array'. */ public static List<? super Object> toList(final V8Array array, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { return toList(array, cache, adapter); } finally { cache.close(); } }
/** * Creates a Map from a V8Object using a deep copy and a TypeAdapter to handle * type conversions. 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. * @param adapter The {@link TypeAdapter} to use for the object conversions. * * @return A map representing a deep copy of the V8Object rooted at 'object'. */ public static Map<String, ? super Object> toMap(final V8Object object, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { return toMap(object, cache, adapter); } finally { cache.close(); } }
/** * Create a Java Object from a result from V8 using a {@link TypeAdapter} to convert * objects. V8 can return basic Java types or V8Values (V8Object, V8Array, etc...). This * method will attempt to convert the result into a pure Java object using * a deep copy. * * If the input is basic Java type (Integer, Double, Boolean, String) * it will be returned. If the input is a V8Value, it will be converted. * * All elements in the V8Object are released after they are accessed. * However, the root object itself is not released. * * @param v8Object The input to convert. * @param adapter The {@link TypeAdapter} to use for the object conversions. * @return A Java object representing the input. */ public static Object getValue(final Object v8Object, final TypeAdapter adapter) { V8Map<Object> cache = new V8Map<Object>(); try { if (v8Object instanceof V8Value) { int type = ((V8Value) v8Object).getV8Type(); return getValue(v8Object, type, cache, adapter); } else { return v8Object; } } finally { cache.close(); } }
V8Map<Object> cache = new V8Map<Object>(); Object object = null; int type = V8Value.UNDEFINED;
V8Map<Object> cache = new V8Map<Object>(); Object object = null; int type = V8Value.UNDEFINED;
V8Map<Object> cache = new V8Map<Object>(); Object object = null; int type = V8Value.UNDEFINED;
@Test public void testReleaseEmptyMap() { V8Map<String> map = new V8Map<String>(); 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 testSizeEmpty() { V8Map<String> map = new V8Map<String>(); assertEquals(0, map.size()); map.close(); }
@Test public void testIsEmpty() { V8Map<String> map = new V8Map<String>(); assertTrue(map.isEmpty()); 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 testDoesNotContainValue() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); assertFalse(map.containsValue("foo")); 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 testDoesNotContainKey() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); assertFalse(map.containsKey(v8Object)); v8Object.close(); map.close(); }
@Test public void testGetMissing() { V8Map<String> map = new V8Map<String>(); v8.executeVoidScript("var x = {}"); V8Object v8Object = v8.getObject("x"); assertNull(map.get(v8Object)); v8Object.close(); 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 testAddDuplicateKey() { V8Map<String> map = new V8Map<String>(); V8Object v1 = new V8Object(v8); map.put(v1, "foo"); map.put(v1, "bar"); assertEquals("bar", map.get(v1)); v1.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(); }