private void releaseArguments(final Object[] args, final boolean hasVarArgs) { if (hasVarArgs && ((args.length > 0) && (args[args.length - 1] instanceof Object[]))) { Object[] varArgs = (Object[]) args[args.length - 1]; for (Object object : varArgs) { if (object instanceof V8Value) { ((V8Value) object).close(); } } } for (Object arg : args) { if (arg instanceof V8Value) { ((V8Value) arg).close(); } } }
/** * Releases the native resources associated with this V8Value. * * @deprecated use close() instead. */ @Override @Deprecated public void release() { close(); }
@Override public V remove(final Object key) { V result = map.remove(key); V8Value twin = twinMap.remove(key); if (twin != null) { twin.close(); } return result; }
protected void weakReferenceReleased(final long objectID) { V8Value v8Value = v8WeakReferences.get(objectID); if (v8Value != null) { v8WeakReferences.remove(objectID); try { v8Value.close(); } catch (Exception e) { // Swallow these exceptions. The V8 GC is running, and // if we return to V8 with Java exception on our stack, // we will be in a world of hurt. } } }
@Override public void clear() { map.clear(); for (V8Value V8Value : twinMap.keySet()) { V8Value.close(); } twinMap.clear(); }
/** * 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(); } } }
/** * 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(); } } }
/** * 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(); } } }
} finally { for (V8Value v8Object : cache.values()) { v8Object.close();
/** * Releases this Memory Manager and all V8Objects that were created while * this memory manager was active. */ public void release() { v8.getLocker().checkThread(); if (released) { return; } releasing = true; try { for (V8Value reference : references) { reference.close(); } v8.removeReferenceHandler(memoryManagerReferenceHandler); references.clear(); } finally { releasing = false; } released = true; }
/** * Releases the native resources associated with this V8Value. * * @deprecated use close() instead. */ @Override @Deprecated public void release() { close(); }
@Override public V remove(final Object key) { V result = map.remove(key); V8Value twin = twinMap.remove(key); if (twin != null) { twin.close(); } return result; }
@Test public void testInt8Array() { V8Value result = (V8Value) v8.executeScript("var ints = new Int8Array(); ints"); assertNotNull(result); result.close(); }
@Test public void testFloat32Array() { V8Value result = (V8Value) v8.executeScript("var floats = new Float32Array(); floats"); assertNotNull(result); result.close(); }
@Test public void testUint8Array() { V8Value result = (V8Value) v8.executeScript("var ints = new Uint8Array(); ints"); assertNotNull(result); result.close(); }
@Test public void testInt32Array() { V8Value result = (V8Value) v8.executeScript("var ints = new Int32Array(); ints"); assertNotNull(result); result.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(); }
@Test public void testReleaseTwinDoesNotReleaseOriginal() { V8Object v8Object = new V8Object(v8); V8Value twin = v8Object.twin(); twin.close(); assertFalse(v8Object.isReleased()); v8Object.close(); }
@Test public void testReleaseObjectDoesNotReleaseTwin() { V8Object v8Object = new V8Object(v8); V8Value twin = v8Object.twin(); v8Object.close(); assertFalse(twin.isReleased()); twin.close(); }
@SuppressWarnings("resource") @Test public void testClearWeakMakesObjectWeak() { V8Value object = new V8Object(v8).setWeak().clearWeak(); assertFalse(object.isWeak()); object.close(); }