/** * Executes a JS Script on this runtime and returns the result as a Java Object. * Primitives will be boxed. * * @param script The script to execute. * * @return The result of the script as a Java Object. */ public Object executeScript(final String script) { return executeScript(script, null, 0); }
private static Object load(Class<?> loader, V8 v8, final String path) throws Exception { return v8.executeScript(V8Engine.readFile(loader, path), path, 0); } }
private static Object load(Class<?> loader, V8 v8, final String path) throws Exception { return register(v8, v8.executeScript(readFile(loader, path), path, 0)); }
/** * Executes a JS Script on this runtime and returns the result as a V8Array. * If the result is not a V8Array, 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 V8Array, or V8ResultUndefinedException if * the result is not a V8Array. */ public V8Array executeArrayScript(final String script, final String scriptName, final int lineNumber) { checkThread(); Object result = this.executeScript(script, scriptName, lineNumber); if (result instanceof V8Array) { return (V8Array) result; } throw new V8ResultUndefined(); }
/** * Executes a JS Script on this runtime and returns the result as a V8Object. * If the result is not a V8Object, 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 V8Object, or V8ResultUndefinedException if * the result is not a V8Object. */ public V8Object executeObjectScript(final String script, final String scriptName, final int lineNumber) { checkThread(); Object result = this.executeScript(script, scriptName, lineNumber); if (result instanceof V8Object) { return (V8Object) result; } throw new V8ResultUndefined(); }
/** * Executes a JS Script on this runtime and returns the result as a Java Object. * Primitives will be boxed. * * @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 Java Object. */ public Object executeScript(final String script, final String scriptName, final int lineNumber) { checkThread(); checkScript(script); return executeScript(getV8RuntimePtr(), UNKNOWN, script, scriptName, lineNumber); }
Object scriptResult = runtime.executeScript("__j2v8__checkThreadTerminate();\n" + script, getName(), -1); if (scriptResult != null) { result = scriptResult.toString();
@Test public void testAnyScriptReturnString() { Object result = v8.executeScript("'foo';"); assertEquals("foo", result); }
@Test public void testAnyScriptReturnBoolean() { Object result = v8.executeScript("false;"); assertFalse((Boolean) result); }
@Test public void testV8HandleCreated_AccessedArray() { ReferenceHandler referenceHandler = mock(ReferenceHandler.class); v8.addReferenceHandler(referenceHandler); V8Array object = (V8Array) v8.executeScript("[1,2,3];"); verify(referenceHandler, times(1)).v8HandleCreated(object); object.close(); }
@Test(expected = V8RuntimeException.class) public void testCannotPushStringToTypedArray() { V8Array array = (V8Array) v8.executeScript("var buf = new ArrayBuffer(8); var ints = new Int32Array(buf); ints"); try { array.push("foo"); } finally { array.close(); } }
@Test public void testAnyScriptReturnsV8Object() { V8Object result = (V8Object) v8.executeScript("foo = {hello:'world'}; foo;"); assertEquals("world", result.getString("hello")); result.close(); }
@Test public void testAnyScriptWithName() { V8Object result = (V8Object) v8.executeScript("foo = {hello:'world'}; foo;", "name", 6); assertEquals("world", result.getString("hello")); result.close(); }
@Test public void testEmptyArrayBufferReturned() { V8ArrayBuffer arrayBuffer = (V8ArrayBuffer) v8.executeScript("new ArrayBuffer(0);"); assertEquals(arrayBuffer.capacity(), 0); arrayBuffer.close(); }
@Test public void testGetTypedArrayIntType() { V8Array result = (V8Array) v8.executeScript("var buf = new ArrayBuffer(4); var ints = new Int16Array(buf); ints[0] = 7; ints"); assertEquals(V8Value.INTEGER, result.getType(0)); result.close(); }
@Test public void testGetTypedArrayType16BitValue() { V8Array result = (V8Array) v8.executeScript("var buf = new ArrayBuffer(4); var ints = new Int16Array(buf); ints[0] = 255; ints"); assertEquals((short) 255, result.get(0)); result.close(); }
@Test public void testGetTypedArrayType32BitFloatValue() { V8Array result = (V8Array) v8.executeScript("var buf = new ArrayBuffer(4); var floats = new Float32Array(buf); floats[0] = 255.5; floats"); assertEquals(255.5, result.getDouble(0), 0.00001); result.close(); }
@Test public void testAccessArrayBuffer_Int8ArrayView() { V8ArrayBuffer buffer = (V8ArrayBuffer) v8.executeScript("var buf = new ArrayBuffer(4); var ints = new Int8Array(buf); ints[0] = 7; buf"); assertEquals(4, buffer.limit()); assertEquals(7, buffer.get(0)); buffer.close(); }
@Test public void testAccessArrayBuffer_Float32rrayView() { V8ArrayBuffer buffer = (V8ArrayBuffer) v8.executeScript("var buf = new ArrayBuffer(4); var floats = new Float32Array(buf); floats[0] = 7.7; buf"); assertEquals(1, buffer.floatLimit()); assertEquals(7.7, buffer.getFloat(0), 0.00001); buffer.close(); }
@Test public void testGetBytesFromTypedArray() { V8Array result = (V8Array) v8.executeScript("var buf = new ArrayBuffer(8); var bytes = new Int8Array(buf); bytes[0] = 1; bytes[1] = 256; bytes"); assertEquals(V8Value.BYTE, result.getType()); assertEquals(1, result.getByte(0)); assertEquals(0, result.getByte(1)); result.close(); }