private static void b64(final V8 v8) { v8.registerJavaMethod((JavaCallback) (receiver, args) -> { byte[] bytes = args.get(0).toString().getBytes(StandardCharsets.UTF_8); return BaseEncoding.base64().encode(bytes); }, "btoa"); v8.registerJavaMethod((JavaCallback) (receiver, args) -> { byte[] atob = BaseEncoding.base64().decode(args.get(0).toString()); return new String(atob, StandardCharsets.UTF_8); }, "atob"); }
void registerVoidCallback(final JavaVoidCallback callback, final long objectHandle, final String jsFunctionName) { MethodDescriptor methodDescriptor = new MethodDescriptor(); methodDescriptor.voidCallback = callback; long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, true); functionRegistry.put(methodID, methodDescriptor); }
void registerCallback(final JavaCallback callback, final long objectHandle, final String jsFunctionName) { long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, false); createAndRegisterMethodDescriptor(callback, methodID); }
void registerCallback(final Object object, final Method method, final long objectHandle, final String jsFunctionName, final boolean includeReceiver) { MethodDescriptor methodDescriptor = new MethodDescriptor(); methodDescriptor.object = object; methodDescriptor.method = method; methodDescriptor.includeReceiver = includeReceiver; long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, isVoidMethod(method)); functionRegistry.put(methodID, methodDescriptor); }
V8 v8 = V8.createV8Runtime(GLOBAL); final NodeJS node = new NodeJS(v8); v8.registerJavaMethod(new JavaVoidCallback() {
synchronized (this) { runtime = V8.createV8Runtime(); runtime.registerJavaMethod(new ExecutorTermination(), "__j2v8__checkThreadTerminate"); setup(runtime);
@Override protected void setup(final V8 runtime) { runtime.registerJavaMethod(V8ExecutorTest.this, "postMessage", "postMessage", new Class<?>[] { Object[].class }); } };
void registerVoidCallback(final JavaVoidCallback callback, final long objectHandle, final String jsFunctionName) { MethodDescriptor methodDescriptor = new MethodDescriptor(); methodDescriptor.voidCallback = callback; long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, true); functionRegistry.put(methodID, methodDescriptor); }
@Test(expected = V8ScriptExecutionException.class) public void testCallJavaMethodNullInt() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodWithIntParameter", "foo", new Class<?>[] { Integer.TYPE }); v8.executeVoidScript("foo(null)"); }
@Test public void testRegisterJavaVoidCallbackExecuteFunction() { JavaVoidCallback callback = mock(JavaVoidCallback.class); v8.registerJavaMethod(callback, "foo"); v8.executeVoidFunction("foo", null); verify(callback).invoke(any(V8Object.class), any(V8Array.class)); }
@Test(expected = V8ScriptExecutionException.class) public void testMissingIntParamtersWithVarArgs() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodVarArgsAndOthers", "foo", new Class<?>[] { Integer.TYPE, Integer.TYPE, Object[].class }); v8.executeVoidScript("foo(1)"); }
@Test public void testCallbackWithNullInStringParameterList() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodWithStringParameter", "foo", new Class<?>[] { String.class }); v8.executeVoidScript("foo(null);"); verify(callback).voidMethodWithStringParameter(null); }
@Test public void testV8ObjectMethodCalledFromVoidScript() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); v8.executeVoidScript("foo();"); verify(callback).v8ObjectMethodNoParameters(); }
@Test public void testMissingParamtersWithObjectParameters() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodWithObjectParameters", "foo", new Class<?>[] { Integer.class }); v8.executeVoidScript("foo(1)"); verify(callback).voidMethodWithObjectParameters(1); }
@Test public void testGetTypedArrayUInt8Type() { v8.registerJavaMethod(new JavaVoidCallback() { @Override public void invoke(final V8Object receiver, final V8Array parameters) { assertEquals(V8Value.V8_TYPED_ARRAY, parameters.getType(0)); } }, "javaMethod"); v8.executeScript("var buf = new ArrayBuffer(4); var ints = new Uint8ClampedArray(buf); ints[0] = 7; javaMethod(ints);"); }
@Test(expected = RuntimeException.class) public void testVoidMethodThrowsJavaException() { ICallback callback = mock(ICallback.class); doThrow(new RuntimeException("My Runtime Exception")).when(callback).voidMethodNoParameters(); v8.registerJavaMethod(callback, "voidMethodNoParameters", "foo", new Class<?>[] {}); try { v8.executeVoidScript("foo()"); } catch (V8ScriptExecutionException e) { assertEquals("My Runtime Exception", e.getCause().getMessage()); throw e; } }
@Test(expected = RuntimeException.class) public void testV8ObjectMethodThrowsJavaException() { ICallback callback = mock(ICallback.class); doThrow(new RuntimeException("My Runtime Exception")).when(callback).v8ObjectMethodNoParameters(); v8.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[] {}); try { v8.executeVoidScript("foo()"); } catch (V8ScriptExecutionException e) { assertEquals("My Runtime Exception", e.getCause().getMessage()); throw e; } }
@Test public void testVoidMethodCalledWithParameters() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodWithParameters", "foo", new Class<?>[] { Integer.TYPE, Double.TYPE, Boolean.TYPE, String.class, V8Object.class }); v8.executeVoidScript("foo(1,1.1, false, 'string', undefined);"); verify(callback).voidMethodWithParameters(1, 1.1, false, "string", new V8Object.Undefined()); }
@Test public void testV8ObjectMethodReturnsUndefined() { ICallback callback = mock(ICallback.class); doReturn(V8.getUndefined()).when(callback).v8ObjectMethodNoParameters(); v8.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); boolean result = v8.executeBooleanScript("typeof foo() === 'undefined'"); assertTrue(result); }
@Test(expected = V8ScriptExecutionException.class) public void testExceptionInJavaCall() { try { v8.registerJavaMethod(this, "callbackWithException", "callback", new Class<?>[] {}); v8.executeVoidScript("callback()"); } catch (V8ScriptExecutionException e) { assertEquals("Test Exception", e.getJSMessage()); assertTrue(e.getCause() instanceof RuntimeException); throw e; } }