/** * Creates a new V8Runtime and loads the required * native libraries if they are not already loaded. * The current thread is given the lock to this runtime. * * @return A new isolated V8 Runtime. */ public static V8 createV8Runtime() { return createV8Runtime(null, null); }
/** * Creates a new V8Runtime and loads the required native libraries if they * are not already loaded. An alias is also set for the global scope. For example, * 'window' can be set as the global scope name. * * The current thread is given the lock to this runtime. * * @param globalAlias The name to associate with the global scope. * * @return A new isolated V8 Runtime. */ public static V8 createV8Runtime(final String globalAlias) { return createV8Runtime(globalAlias, null); }
/** * Create a new ConcurrentV8. A ConcurrentV8 allows multiple * threads to work with the same V8 engine by releasing * the locks between calls. */ public ConcurrentV8() { v8 = V8.createV8Runtime(); v8.getLocker().release(); }
private V8Engine v8(String id, String scope) { return cache.computeIfAbsent(Thread.currentThread().getName() + ":" + id, name -> { V8 v8 = V8.createV8Runtime(scope); MemoryManager mem = new MemoryManager(v8); console(v8, id); assets(getClass(), v8); b64(v8); return new V8Engine(v8, mem, id); }); }
@Override public void run() { runtime = V8.createV8Runtime(); try { target.run(runtime); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.close(); runtime = null; } } } }
V8 v8 = V8.createV8Runtime(GLOBAL); final NodeJS node = new NodeJS(v8); v8.registerJavaMethod(new JavaVoidCallback() {
/** * Creates a new V8Runtime and loads the required * native libraries if they are not already loaded. * The current thread is given the lock to this runtime. * * @return A new isolated V8 Runtime. */ public static V8 createV8Runtime() { return createV8Runtime(null, null); }
@Override public void run() { synchronized (this) { runtime = V8.createV8Runtime(); runtime.registerJavaMethod(new ExecutorTermination(), "__j2v8__checkThreadTerminate"); setup(runtime);
@Test public void testV8MultipleReleaseHandlers() { V8 testV8 = V8.createV8Runtime(); V8Runnable releaseHandler1 = mock(V8Runnable.class); V8Runnable releaseHandler2 = mock(V8Runnable.class); testV8.addReleaseHandler(releaseHandler1); testV8.addReleaseHandler(releaseHandler2); testV8.close(); verify(releaseHandler1, times(1)).run(any(V8.class)); // cannot check against the real v8 because it's released. verify(releaseHandler2, times(1)).run(any(V8.class)); // cannot check against the real v8 because it's released. }
@Test public void testV8UnknownReleaseHandleRemoved() { V8 testV8 = V8.createV8Runtime(); V8Runnable releaseHandler1 = mock(V8Runnable.class); V8Runnable releaseHandler2 = mock(V8Runnable.class); testV8.addReleaseHandler(releaseHandler1); testV8.removeReleaseHandler(releaseHandler2); testV8.close(); verify(releaseHandler1, times(1)).run(any(V8.class)); // cannot check against the real v8 because it's released. }
@SuppressWarnings("resource") @Test(expected = IllegalStateException.class) public void testISEThrownOnShutdown() { V8 v8_ = V8.createV8Runtime(); new V8Object(v8_); v8_.release(true); }
@Before public void setup() { V8.setFlags("--expose-debug-as=" + DebugHandler.DEBUG_OBJECT_NAME); v8 = V8.createV8Runtime(); debugHandler = new DebugHandler(v8); debugHandler.setScriptBreakpoint("script", 14); breakHandler = mock(BreakHandler.class); debugHandler.addBreakHandler(breakHandler); }
@Before public void setup() { V8.setFlags("--expose-debug-as=" + DebugHandler.DEBUG_OBJECT_NAME); v8 = V8.createV8Runtime(); debugHandler = new DebugHandler(v8); debugHandler.setScriptBreakpoint("script", 2); breakHandler = mock(BreakHandler.class); debugHandler.addBreakHandler(breakHandler); }
@Test public void testReleaseAfterV8Released() { V8Locker v8Locker = new V8Locker(v8); v8.close(); v8Locker.release(); v8 = V8.createV8Runtime(); // Create a new runtime so the teardown doesn't fail }
@Test public void testAlternateGlobalAlias() { v8.close(); v8 = V8.createV8Runtime("document"); v8.executeVoidScript("var global = Function('return this')();"); assertTrue(v8.executeBooleanScript("global === document")); }
@Test public void testToStringWorksOnReleasedV8Runtime_V8Function() { @SuppressWarnings("resource") V8Object v8Object = new V8Function(v8); v8.release(false); assertEquals("[Function released]", v8Object.toString()); v8 = V8.createV8Runtime(); }
@Test public void testToStringWorksOnReleasedV8Runtime_V8Array() { @SuppressWarnings("resource") V8Object v8Object = new V8Array(v8); v8.release(false); assertEquals("[Array released]", v8Object.toString()); v8 = V8.createV8Runtime(); }
@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 testNestedExecutorExecution() throws InterruptedException { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor(""); executor.start(); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); key.close(); runtime.close(); executor.join(); }
@Test public void testGetNestedExecutor() { V8 runtime = V8.createV8Runtime(); runtime.terminateExecution(); V8Executor executor = new V8Executor(""); V8Object key = new V8Object(runtime); runtime.registerV8Executor(key, executor); assertEquals(executor, runtime.getExecutor(key)); key.close(); runtime.close(); }