/** * Executes a JS Script on this runtime and returns the result as an integer. * If the result is not an integer, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as an integer, or V8ResultUndefinedException if * the result is not an integer. */ public int executeIntegerScript(final String script) { return executeIntegerScript(script, null, 0); }
/** * Executes a JS Script on this runtime and returns the result as an integer. * If the result is not an integer, 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 excepton purposes. * * @return The result of the script as an integer, or V8ResultUndefinedException if * the result is not an integer. */ public int executeIntegerScript(final String script, final String scriptName, final int lineNumber) { checkThread(); checkScript(script); return executeIntegerScript(v8RuntimePtr, script, scriptName, lineNumber); }
/** * Executes a JS Script on this runtime and returns the result as an integer. * If the result is not an integer, then a V8ResultUndefinedException is thrown. * * @param script The script to execute. * * @return The result of the script as an integer, or V8ResultUndefinedException if * the result is not an integer. */ public int executeIntegerScript(final String script) { return executeIntegerScript(script, null, 0); }
@Override public void run(final V8 v8) { Assert.assertEquals(3344, v8.executeIntegerScript("i")); } });
@Test public void testCreateV8ObjectFromLongMap() { Map<String, Long> map = new HashMap<String, Long>(); map.put("a", 1L); map.put("b", 3L); int size = registerAndRelease("result", map); assertEquals(2, size); assertEquals(1, v8.executeIntegerScript("result.a")); assertEquals(3, v8.executeIntegerScript("result['b']")); }
@Test public void testCreateV8ObjectFromIntegerMap() { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 1); map.put("b", 3); int size = registerAndRelease("result", map); assertEquals(2, size); assertEquals(1, v8.executeIntegerScript("result.a")); assertEquals(3, v8.executeIntegerScript("result['b']")); }
@Test public void testVoidMethodCalledFromIntScript() { ICallback callback = mock(ICallback.class); v8.registerJavaMethod(callback, "voidMethodNoParameters", "foo", new Class<?>[0]); v8.executeIntegerScript("foo();1"); verify(callback).voidMethodNoParameters(); }
@Test(expected = Error.class) public void testCannotAccessDisposedIsolateInt() { v8.close(); v8.executeIntegerScript("7"); }
@Test public void testIntScriptWithDouble() { int result = v8.executeIntegerScript("1.9+2.9;"); assertEquals(4, result); }
@Test public void testGetTypedArrayValue() { int result = v8.executeIntegerScript("var buf = new ArrayBuffer(4); var ints = new Int16Array(buf); ints[0] = 7; ints[0]"); assertEquals(7, result); }
@Test public void testIntMethodCalledFromScriptWithResult() { ICallback callback = mock(ICallback.class); doReturn(7).when(callback).intMethodNoParameters(); v8.registerJavaMethod(callback, "intMethodNoParameters", "foo", new Class<?>[0]); int result = v8.executeIntegerScript("foo();"); assertEquals(7, result); }
@Test public void testCreateV8ArrayFromLongList() { List<Long> list = new ArrayList<Long>(); list.add((long) 1); int size = registerAndRelease("result", list); assertEquals(1, size); assertEquals(1, v8.executeIntegerScript("result[0]")); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForWrongReturnTypeOfIntFunction() { v8.executeIntegerScript("function foo() {return 'test';}; 42"); int result = v8.executeIntegerFunction("foo", null); assertEquals(3, result); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForNoReturnInIntFunction() { v8.executeIntegerScript("function foo() {}; 42"); int result = v8.executeIntegerFunction("foo", null); assertEquals(3, result); }
@Test public void testWindowAliasForGlobalScope() { setupWindowAlias(); v8.executeVoidScript("a = 1; window.b = 2;"); assertEquals(1, v8.executeIntegerScript("window.a;")); assertEquals(2, v8.executeIntegerScript("b;")); assertTrue(v8.executeBooleanScript("window.hasOwnProperty( \"Object\" )")); }
@Test public void testAddIntReplaceValue() { v8.add("foo", 42); v8.add("foo", 43); int result = v8.executeIntegerScript("foo"); assertEquals(43, result); }
@Test public void testAccessInt8TypedArrayInJavaScript() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 2); V8TypedArray v8Int8Array = new V8TypedArray(v8, buffer, V8Value.BYTE, 0, 2); v8.add("v8Int8Array", v8Int8Array); buffer.put((byte) 4); buffer.put((byte) 8); assertEquals(4, v8.executeIntegerScript("v8Int8Array[0];")); assertEquals(8, v8.executeIntegerScript("v8Int8Array[1];")); buffer.close(); v8Int8Array.close(); }
@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 testGetTypedArrayValue_WithArrayBuffer() { V8ArrayBuffer buffer = new V8ArrayBuffer(v8, 4); v8.add("buf", buffer); int result = v8.executeIntegerScript("var ints = new Int16Array(buf); ints[0] = 7; ints[0]"); assertEquals(7, result); buffer.close(); }
@Test public void testAddObjectWithInt() { V8Object v8Object = new V8Object(v8); v8Object.add("integer", 75); v8.add("foo", v8Object); int result = v8.executeIntegerScript("foo.integer"); assertEquals(75, result); v8Object.close(); }