/** * Executes a JS Script on this runtime and returns the result as a boolean. * If the result is not a boolean, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as a boolean, or V8ResultUndefinedException if * the result is not a boolean. */ public boolean executeBooleanScript(final String script) { return executeBooleanScript(script, null, 0); }
/** * Executes a JS Script on this runtime and returns the result as a boolean. * If the result is not a boolean, then a V8ResultUndefinedException is thrown. * * @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. * * @return The result of the script as a boolean, or V8ResultUndefinedException if * the result is not a boolean. */ public boolean executeBooleanScript(final String script, final String scriptName, final int lineNumber) { checkThread(); checkScript(script); return executeBooleanScript(v8RuntimePtr, script, scriptName, lineNumber); }
/** * Executes a JS Script on this runtime and returns the result as a boolean. * If the result is not a boolean, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as a boolean, or V8ResultUndefinedException if * the result is not a boolean. */ public boolean executeBooleanScript(final String script) { return executeBooleanScript(script, null, 0); }
@Test public void testCreatev8ObjectWithNulls() { Map<String, Boolean> map = new HashMap<String, Boolean>(); map.put("a", true); map.put("b", null); int size = registerAndRelease("result", map); assertTrue(v8.executeBooleanScript("result.a")); assertTrue(v8.executeBooleanScript("typeof result.b === 'undefined'")); assertEquals(2, size); }
@Test public void testBooleanScriptWithName() { boolean result = v8.executeBooleanScript("true", "name", 4); assertTrue(result); }
@Test(expected = Error.class) public void testCannotAccessDisposedIsolateBoolean() { v8.close(); v8.executeBooleanScript("true"); }
/*** Boolean Script ***/ @Test public void testSimpleBooleanScript() { boolean result = v8.executeBooleanScript("true"); assertTrue(result); }
@Test public void testStringMethodCalledFromScriptWithNull() { ICallback callback = mock(ICallback.class); doReturn(null).when(callback).stringMethodNoParameters(); v8.registerJavaMethod(callback, "stringMethodNoParameters", "foo", new Class<?>[0]); boolean result = v8.executeBooleanScript("foo() === null"); assertTrue(result); }
@Test public void testV8ObjectMethodReturnsNull() { ICallback callback = mock(ICallback.class); doReturn(null).when(callback).v8ObjectMethodNoParameters(); v8.registerJavaMethod(callback, "v8ObjectMethodNoParameters", "foo", new Class<?>[0]); boolean result = v8.executeBooleanScript("foo() === null"); assertTrue(result); }
@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 public void testWindowWindowWindowWindow() { setupWindowAlias(); assertTrue(v8.executeBooleanScript("window.window.window === window")); }
@Test public void testAccessGlobalViaWindow() { setupWindowAlias(); String script = "var global = {data: 0};\n" + "global === window.global"; assertTrue(v8.executeBooleanScript(script)); }
/*** Add Boolean ***/ @Test public void testAddBoolean() { v8.add("foo", true); boolean result = v8.executeBooleanScript("foo"); assertTrue(result); }
@Test public void testwindowIsInstanceOfWindow() { setupWindowAlias(); assertTrue(v8.executeBooleanScript("window instanceof Window")); }
@Test public void testWindowIsGlobal2() { setupWindowAlias(); v8.executeVoidScript("var global = Function('return this')();"); assertTrue(v8.executeBooleanScript("window === global")); }
@Test public void testAccessWindowObjectInStrictMode() { setupWindowAlias(); String script = "'use strict';\n" + "window.foo = 7;\n" + "true\n"; boolean result = v8.executeBooleanScript(script); assertTrue(result); assertEquals(7, v8.executeIntegerScript("window.foo")); }
@Test public void testAddObjectWithBoolean() { V8Object v8Object = new V8Object(v8); v8Object.add("boolean", false); v8.add("foo", v8Object); boolean result = v8.executeBooleanScript("foo.boolean"); assertFalse(result); v8Object.close(); }
@SuppressWarnings("unchecked") @Test public void testCloneV8Array_GetValue() { V8Array list = v8.executeArrayScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Array v8Object = V8ObjectUtils.toV8Array(v8, (List<? extends Object>) V8ObjectUtils.getValue(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }
@Test public void testCloneV8Array() { V8Array list = v8.executeArrayScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Array v8Object = V8ObjectUtils.toV8Array(v8, V8ObjectUtils.toList(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }
@Test public void testCloneV8Object() { V8Object list = v8.executeObjectScript("var l = [{first:'ian', last:'bull'}, {first:'sadie', last:'bull'}]; l;"); V8Object v8Object = V8ObjectUtils.toV8Object(v8, V8ObjectUtils.toMap(list)); v8.add("l2", v8Object); v8.executeBooleanScript("JSON.stringify(l) === JSON.stringify(l2);"); list.close(); v8Object.close(); }