/** * Invoke a JavaScript function and return the result as a boolean. If the * result is not a boolean, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A boolean representing the result of the function call or V8ResultUndefined * if the result is not a boolean. */ public boolean executeBooleanFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeBooleanFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForWrongReturnTypeOfBooleanFunction() { v8.executeVoidScript("function foo() {return 'foo';}"); v8.executeBooleanFunction("foo", null); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForNoReturnInBooleanFunction() { v8.executeVoidScript("function foo() {};"); v8.executeBooleanFunction("foo", null); }
@Test(expected = V8ScriptExecutionException.class) public void testV8ScriptExceptionBooleanCall() { v8.executeVoidScript(errorFunction); v8.executeBooleanFunction("myFunction", null); }
@Test public void testObjectMethodReturnsBoolean() { ICallback callback = mock(ICallback.class); doReturn(true).when(callback).objectMethodNoParameter(); v8.registerJavaMethod(callback, "objectMethodNoParameter", "foo", new Class<?>[] {}); boolean result = v8.executeBooleanFunction("foo", null); assertTrue(result); }
@Test public void testBooleanFunctionCallNullParameters() { v8.executeVoidScript("function foo() {return true;}"); boolean result = v8.executeBooleanFunction("foo", null); assertTrue(result); }
/** * Invoke a JavaScript function and return the result as a boolean. If the * result is not a boolean, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A boolean representing the result of the function call or V8ResultUndefined * if the result is not a boolean. */ public boolean executeBooleanFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeBooleanFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/*** Boolean Function ***/ @Test public void testSimpleBooleanFunction() { v8.executeVoidScript("function foo() {return true;}"); boolean result = v8.executeBooleanFunction("foo", null); assertTrue(result); }
@Test public void testBooleanFunctionCallNoParameters() { v8.executeVoidScript("function foo() {return true;}"); V8Array parameters = new V8Array(v8); boolean result = v8.executeBooleanFunction("foo", parameters); assertTrue(result); parameters.close(); }
@Test(expected = V8ResultUndefined.class) public void testBooleanFunctionNotBoolean() { v8.executeVoidScript("function add(x, y) {return 'bar';}"); V8Array parameters = new V8Array(v8); parameters.push(7); parameters.push(8); try { v8.executeBooleanFunction("add", parameters); } finally { parameters.close(); } }
@Test(expected = V8ResultUndefined.class) public void testBooleanFunctionNoReturn() { v8.executeVoidScript("function add(x, y) {;}"); V8Array parameters = new V8Array(v8); parameters.push(7); parameters.push(8); try { v8.executeBooleanFunction("add", parameters); } finally { parameters.close(); } }
@Test public void testBooleanFunctionCall() { v8.executeVoidScript("function add(x, y) {return x&&y;}"); V8Array parameters = new V8Array(v8); parameters.push(true); parameters.push(true); boolean result = v8.executeBooleanFunction("add", parameters); assertTrue(result); parameters.close(); }
@Test(expected = Error.class) public void testSharingObjectsAsFunctionCallParameters_BooleanFunction() { V8 engine = null; V8 engine2 = null; try { engine = V8.createV8Runtime(); engine2 = V8.createV8Runtime(); engine.executeScript("b = function(param){return param;}"); engine2.executeScript("a = [false];"); V8Array a = (V8Array) engine2.get("a"); engine.executeBooleanFunction("b", a); } finally { engine.release(false); engine2.release(false); } }
/** * Invoke a JavaScript function and return the result as a boolean. If the * result is not a boolean, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A boolean representing the result of the function call or V8ResultUndefined * if the result is not a boolean. */ public boolean executeBooleanFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeBooleanFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a boolean. If the * result is not a boolean, or does not exist, then V8ResultUndefined is thrown. * * @param name The name of the JS Function to call. * * @param parameters The parameters to pass to the function. Parameters must be released. * * @return A boolean representing the result of the function call or V8ResultUndefined * if the result is not a boolean. */ public boolean executeBooleanFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeBooleanFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }