/** * Invoke a JavaScript function and return the result as a double. If the * result is not a double, 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 double representing the result of the function call or V8ResultUndefined * if the result is not a double. */ public double executeDoubleFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeDoubleFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForNoReturnInDoubleFunction() { v8.executeVoidScript("function foo() {};"); v8.executeDoubleFunction("foo", null); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForWrongReturnTypeOfDoubleFunction() { v8.executeVoidScript("function foo() {return 'foo';}"); v8.executeDoubleFunction("foo", null); }
@Test(expected = V8ScriptExecutionException.class) public void testV8ScriptExceptionDoubleCall() { v8.executeVoidScript(errorFunction); v8.executeDoubleFunction("myFunction", null); }
@Test public void testObjectMethodReturnsDouble() { ICallback callback = mock(ICallback.class); doReturn(7.7).when(callback).objectMethodNoParameter(); v8.registerJavaMethod(callback, "objectMethodNoParameter", "foo", new Class<?>[] {}); double result = v8.executeDoubleFunction("foo", null); assertEquals(7.7, result, 0.000001); }
@Test public void testDoubleFunctionCallNullParameters() { v8.executeVoidScript("function foo() {return 7.1;}"); double result = v8.executeDoubleFunction("foo", null); assertEquals(7.1, result, 0.000001); }
/*** Double Function ***/ @Test public void testSimpleDoubleFunction() { v8.executeVoidScript("function foo() {return 3.14 + 1;}"); double result = v8.executeDoubleFunction("foo", null); assertEquals(4.14, result, 0.000001); }
/** * Invoke a JavaScript function and return the result as a double. If the * result is not a double, 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 double representing the result of the function call or V8ResultUndefined * if the result is not a double. */ public double executeDoubleFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeDoubleFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
@Test public void testDoubleFunctionCallNoParameters() { v8.executeVoidScript("function foo() {return 7.2;}"); V8Array parameters = new V8Array(v8); double result = v8.executeDoubleFunction("foo", parameters); assertEquals(7.2, result, 0.0000001); parameters.close(); }
@Test(expected = V8ResultUndefined.class) public void testDoubleFunctionNotDouble() { v8.executeVoidScript("function add(x, y) {return 'bar';}"); V8Array parameters = new V8Array(v8); parameters.push(7); parameters.push(8); try { v8.executeDoubleFunction("add", parameters); } finally { parameters.close(); } }
@Test(expected = V8ResultUndefined.class) public void testDoubleFunctionNoReturn() { v8.executeVoidScript("function add(x, y) {;}"); V8Array parameters = new V8Array(v8); parameters.push(7); parameters.push(8); try { v8.executeDoubleFunction("add", parameters); } finally { parameters.close(); } }
@Test public void testDoubleFunctionCall() { v8.executeVoidScript("function add(x, y) {return x+y;}"); V8Array parameters = new V8Array(v8); parameters.push(1.1); parameters.push(2.2); double result = v8.executeDoubleFunction("add", parameters); assertEquals(3.3, result, 0.000001); parameters.close(); }
@Test(expected = Error.class) public void testSharingObjectsAsFunctionCallParameters_DoubleFunction() { V8 engine = null; V8 engine2 = null; try { engine = V8.createV8Runtime(); engine2 = V8.createV8Runtime(); engine.executeScript("b = function(param){return param;}"); engine2.executeScript("a = [3.14];"); V8Array a = (V8Array) engine2.get("a"); engine.executeDoubleFunction("b", a); } finally { engine.release(false); engine2.release(false); } }
/** * Invoke a JavaScript function and return the result as a double. If the * result is not a double, 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 double representing the result of the function call or V8ResultUndefined * if the result is not a double. */ public double executeDoubleFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeDoubleFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a double. If the * result is not a double, 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 double representing the result of the function call or V8ResultUndefined * if the result is not a double. */ public double executeDoubleFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeDoubleFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }