private void notifyReleaseHandlers(final V8 runtime) { for (V8Runnable handler : releaseHandlers) { handler.run(runtime); } }
/** * Creates a Map from a V8Object using a deep copy. 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. * * @return A map representing a deep copy of the V8Object rooted at 'object'. */ public static Map<String, ? super Object> toMap(final V8Object object) { return toMap(object, DEFAULT_TYPE_ADAPTER); }
/** * Creates a List from a V8Array using a deep copy. 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. * * @return A list representing a deep copy of the V8Array rooted at 'array'. */ public static List<? super Object> toList(final V8Array array) { return toList(array, DEFAULT_TYPE_ADAPTER); }
/** * Shutdown all executors associated with this runtime. * If force terminate is specified, it will forcefully terminate * the executors, otherwise it will simply signal that they * should terminate. * * @param forceTerminate Specify if the executors should be * forcefully terminated, or simply notified to shutdown when ready. */ public void shutdownExecutors(final boolean forceTerminate) { checkThread(); if (executors == null) { return; } for (V8Executor executor : executors.values()) { if (forceTerminate) { executor.forceTermination(); } else { executor.shutdown(); } } }
/** * 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(); } }
/** * 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); }
/** * Gets a Java Object representing the value with the given key in the V8Object. * If the value is a primitive (int, boolean or double) then a boxed instance * is returned. If the value is a String, then a String is returned. If * the value is a V8Object or V8Array, then a Map or List is returned. * * @param object The object on which to lookup the value. The object is not * released. * @param key The key to use to lookup the value. * * @return A Java Object representing the value at a given key. */ public static Object getValue(final V8Object object, final String key) { return getValue(object, key, DEFAULT_TYPE_ADAPTER); }
public V8Array array(final List<? extends Object> value) { return register(v8, V8ObjectUtils.toV8Array(v8, value)); }
public V8Object hash(final Map<String, Object> hash) { return register(v8, V8ObjectUtils.toV8Object(v8, hash)); }
@Override public void close() { this.clear(); }
/** * Returns the executor associated with the given key. * * @param key The key the executor was associated with. * @return The executor or null if it does not exist. */ public V8Executor getExecutor(final V8Object key) { checkThread(); if (executors == null) { return null; } return executors.get(key); }
/** * Removes the executor from this runtime. The executor is * *NOT* shutdown, simply removed from the list of known * executors. * * @param key The key the executor was associated with. * @return The executor or null if it does not exist. */ public V8Executor removeExecutor(final V8Object key) { checkThread(); if (executors == null) { return null; } return executors.remove(key); }
/** * Returns the number of handles currently being tracked by this * memory manager. * * Throws an IllegalStateException if the memory manager is used after it's * been released. * * @return The object reference count */ public int getObjectReferenceCount() { checkReleased(); return references.size(); }
/** * Creates and registered a Memory Manager. After this, all V8 handles will be * tracked until release is called. * * @param v8 The V8 runtime to register this Memory Manager on */ public MemoryManager(final V8 v8) { this.v8 = v8; memoryManagerReferenceHandler = new MemoryManagerReferenceHandler(); v8.addReferenceHandler(memoryManagerReferenceHandler); }
/** * Releases all the resources associated with this map. A * map can be used again once it's released, although * if it's used again it should be released again. */ @Override @Deprecated public void release() { close(); }
private static V8ArrayBuffer toV8ArrayBuffer(final V8 v8, final ArrayBuffer arrayBuffer, final Map<Object, V8Value> cache) { if (cache.containsKey(arrayBuffer)) { return (V8ArrayBuffer) cache.get(arrayBuffer); } V8ArrayBuffer result = arrayBuffer.getV8ArrayBuffer(); cache.put(arrayBuffer, result); return result; }
private static V8TypedArray toV8TypedArray(final V8 v8, final TypedArray typeArray, final Map<Object, V8Value> cache) { if (cache.containsKey(typeArray)) { return (V8TypedArray) cache.get(typeArray); } V8TypedArray result = typeArray.getV8TypedArray(); cache.put(typeArray, result); return result; }
@Override public Object adapt(final int type, final Object value) { if (type == typeToAdapt) { return adapt(value); } return TypeAdapter.DEFAULT; }
/** * Create a Java Object from a result from V8. 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. * @return A Java object representing the input. */ public static Object getValue(final Object v8Object) { return getValue(v8Object, DEFAULT_TYPE_ADAPTER); }