/** * Executes a JS Script on this runtime. * * @param script The script to execute. */ public void executeVoidScript(final String script) { executeVoidScript(script, null, 0); }
/** * Executes a JS Script on this runtime. * * @param script The script to execute. * @param scriptName The name of the script * @param lineNumber The line number that is considered to be the first line of * the script. Typically 0, but could be set to another value for exception stack trace purposes. */ public void executeVoidScript(final String script, final String scriptName, final int lineNumber) { checkThread(); checkScript(script); executeVoidScript(v8RuntimePtr, script, scriptName, lineNumber); }
runtime.executeVoidScript("(function() {\n" + " " + DEBUG_OBJECT_NAME + ".Debug. " + MAKE_BREAK_EVENT + " = function (break_id,breakpoints_hit) {\n" + " return new " + DEBUG_OBJECT_NAME + ".BreakEvent(break_id,breakpoints_hit);\n"
@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 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 testMissingParamtersWithObjectParameters() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodWithObjectParameters", "foo", new Class<?>[] { Integer.class }); v8.executeVoidScript("foo(1)"); verify(callback).voidMethodWithObjectParameters(1); }
@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 testFunctionCallWithNullReturn() { v8.executeVoidScript("function getFoo() {return null;}"); Object result = v8.executeFunction("getFoo", null); assertNull(result); }
@Test public void testExecuteJSFunction_Double() { v8.executeVoidScript("function add(p1, p2) {return p1 + p2;}"); double result = (Double) v8.executeJSFunction("add", 3.1d, 2.2d); assertEquals(5.3, result, 0.00001); }
@Test public void testExecuteJSFunction_null() { v8.executeVoidScript("function test(p1) { if (!p1) { return 'passed';} }"); String result = (String) v8.executeJSFunction("test", new Object[] { null }); assertEquals("passed", result); }
@Test public void testCallFunctionNullParameters() { v8.executeVoidScript("function call() {return true;}"); V8Function function = (V8Function) v8.getObject("call"); boolean result = (Boolean) function.call(v8, null); assertTrue(result); function.close(); }
@Test public void testIntFunctionCallNoParameters() { v8.executeVoidScript("function foo() {return 7;}"); V8Array parameters = new V8Array(v8); int result = v8.executeIntegerFunction("foo", parameters); assertEquals(7, result); parameters.close(); }
@Test public void testArrayFunctionCallNullParameters() { v8.executeVoidScript("function foo() {return [1,2];}"); V8Array result = v8.executeArrayFunction("foo", null); assertEquals(2, result.length()); result.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 public void testVoidFunctionCallNoParameters() { v8.executeVoidScript("function foo() {x=7;}"); V8Array parameters = new V8Array(v8); v8.executeVoidFunction("foo", parameters); assertEquals(7, v8.getInteger("x")); parameters.close(); }
@Test public void testStringParameter() { v8.executeVoidScript("function countLength(str) {return str.length;}"); V8Array parameters = new V8Array(v8); parameters.push("abcdefghijklmnopqrstuvwxyz"); assertEquals(26, v8.executeIntegerFunction("countLength", parameters)); 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 public void testObjectFunctionCallNoParameters() { v8.executeVoidScript("function foo() {return {bar:8};}"); V8Array parameters = new V8Array(v8); V8Object result = v8.executeObjectFunction("foo", parameters); assertEquals(8, result.getInteger("bar")); parameters.close(); result.close(); }