/** * Invoke a JavaScript function and return the result as a String. If the * result is not a String, 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 String representing the result of the function call or V8ResultUndefined * if the result is not a String. */ public String executeStringFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeStringFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForNoReturnInStringFunction() { v8.executeVoidScript("function foo() {};"); v8.executeStringFunction("foo", null); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForWrongReturnTypeOfStringFunction() { v8.executeVoidScript("function foo() {return 42;}"); v8.executeStringFunction("foo", null); }
@Test(expected = V8ScriptExecutionException.class) public void testV8ScriptExceptionStringCall() { v8.executeVoidScript(errorFunction); v8.executeStringFunction("myFunction", null); }
@Test public void testObjectMethodReturnsString() { ICallback callback = mock(ICallback.class); doReturn("foobar").when(callback).objectMethodNoParameter(); v8.registerJavaMethod(callback, "objectMethodNoParameter", "foo", new Class<?>[] {}); String result = v8.executeStringFunction("foo", null); assertEquals("foobar", result); }
@Test public void testExecuteUnicodeFunction() { v8.executeVoidScript("var ಠ_ಠ = function() { return '🌞' + '💐'; }; "); assertEquals("🌞💐", v8.executeStringFunction("ಠ_ಠ", null)); }
@Test public void testStringFunctionCallNullParameters() { v8.executeVoidScript("function foo() {return 'hello';}"); String result = v8.executeStringFunction("foo", null); assertEquals("hello", result); }
/*** String Function ***/ @Test public void testSimpleStringFunction() { v8.executeVoidScript("function foo() {return 'hello';}"); String result = v8.executeStringFunction("foo", null); assertEquals("hello", result); }
/** * Invoke a JavaScript function and return the result as a String. If the * result is not a String, 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 String representing the result of the function call or V8ResultUndefined * if the result is not a String. */ public String executeStringFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); v8.checkRuntime(parameters); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeStringFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
@Test public void testStringFunctionCallNoParameters() { v8.executeVoidScript("function foo() {return 'hello';}"); V8Array parameters = new V8Array(v8); String result = v8.executeStringFunction("foo", parameters); assertEquals("hello", result); parameters.close(); }
@Test(expected = V8ResultUndefined.class) public void testStringFunctionNoReturn() { v8.executeVoidScript("function add(x, y) {;}"); V8Array parameters = new V8Array(v8); parameters.push(7); parameters.push(8); try { v8.executeStringFunction("add", parameters); } finally { parameters.close(); } }
@Test(expected = V8ResultUndefined.class) public void testStringFunctionNotString() { v8.executeVoidScript("function add(x, y) {return 7;}"); V8Array parameters = new V8Array(v8); parameters.push(7); parameters.push(8); try { v8.executeStringFunction("add", parameters); } finally { parameters.close(); } }
@Test public void testStringFunctionCall() { v8.executeVoidScript("function add(x, y) {return x+y;}"); V8Array parameters = new V8Array(v8); parameters.push("hello, "); parameters.push("world!"); String result = v8.executeStringFunction("add", parameters); assertEquals("hello, world!", result); parameters.close(); }
@Test(expected = Error.class) public void testSharingObjectsAsFunctionCallParameters_StringFunction() { V8 engine = null; V8 engine2 = null; try { engine = V8.createV8Runtime(); engine2 = V8.createV8Runtime(); engine.executeScript("b = function(param){return param;}"); engine2.executeScript("a = ['foo'];"); V8Array a = (V8Array) engine2.get("a"); engine.executeStringFunction("b", a); } finally { engine.release(false); engine2.release(false); } }
static Optional<String> executeV8Function(V8 v8, String userAgent) { V8Array v8FunctionParams = null; try { v8.getLocker().acquire(); v8FunctionParams = new V8Array(v8).push(userAgent); return Optional.ofNullable(v8.executeStringFunction(WRAPPER_FUNCTION, v8FunctionParams)); } finally { // Release V8 engine function parameters array Optional.ofNullable(v8FunctionParams) .ifPresent(V8Array::release); // Release V8 engine lock Optional.of(v8.getLocker()) .filter(V8Locker::hasLock) .ifPresent(V8Locker::release); } } }
@Override @SuppressWarnings("unchecked") public <T> T callMethod(String name, Class<T> resultType, Object... args) throws ScriptRunnerException { Set<V8Value> newValues = new HashSet<>(); V8Array parameters = convertArguments(args, newValues); try { if (String.class.isAssignableFrom(resultType)) { return (T)runtime.executeStringFunction(name, parameters); } return convert(runtime.executeObjectFunction(name, parameters), resultType); } catch (V8RuntimeException e) { throw new ScriptRunnerException("Could not call method", e); } finally { newValues.forEach(V8Value::release); } }
@Override @SuppressWarnings("unchecked") public <T> T callMethod(String name, Class<T> resultType, Object... args) throws ScriptRunnerException { Set<V8Value> newValues = new HashSet<>(); V8Array parameters = convertArguments(args, newValues); try { if (String.class.isAssignableFrom(resultType)) { return (T)runtime.executeStringFunction(name, parameters); } return convert(runtime.executeObjectFunction(name, parameters), resultType); } catch (V8RuntimeException e) { throw new ScriptRunnerException("Could not call method", e); } finally { newValues.forEach(V8Value::release); } }
/** * Invoke a JavaScript function and return the result as a String. If the * result is not a String, 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 String representing the result of the function call or V8ResultUndefined * if the result is not a String. */ public String executeStringFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeStringFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
/** * Invoke a JavaScript function and return the result as a String. If the * result is not a String, 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 String representing the result of the function call or V8ResultUndefined * if the result is not a String. */ public String executeStringFunction(final String name, final V8Array parameters) { v8.checkThread(); checkReleased(); long parametersHandle = parameters == null ? 0 : parameters.getHandle(); return v8.executeStringFunction(v8.getV8RuntimePtr(), getHandle(), name, parametersHandle); }
return runtime.executeStringFunction("compileTypescript", args); } finally { args.release();