V8 v8 = V8.createV8Runtime(GLOBAL); final NodeJS node = new NodeJS(v8); v8.registerJavaMethod(new JavaVoidCallback() { File startupScript = createTemporaryScriptFile(STARTUP_SCRIPT, STARTUP_SCRIPT_NAME); try { v8.createNodeRuntime(startupScript.getAbsolutePath()); } finally { startupScript.delete();
/** * Adds a key value pair to the receiver where the value is a String. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final String value) { v8.checkThread(); checkReleased(); if (value == null) { v8.addNull(v8.getV8RuntimePtr(), objectHandle, key); } else if (value.equals(V8.getUndefined())) { v8.addUndefined(v8.getV8RuntimePtr(), objectHandle, key); } else { v8.add(v8.getV8RuntimePtr(), objectHandle, key, value); } return this; }
/** * 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); }
@Override public void close() { v8.checkThread(); if (!released) { try { v8.releaseObjRef(this); } finally { released = true; v8.release(v8.getV8RuntimePtr(), objectHandle); } } }
void registerCallback(final JavaCallback callback, final long objectHandle, final String jsFunctionName) { long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, false); createAndRegisterMethodDescriptor(callback, methodID); }
/** * 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); }
@Test(expected = V8ResultUndefined.class) public void testResultUndefinedForNoReturnInStringFunction() { v8.executeVoidScript("function foo() {};"); v8.executeStringFunction("foo", null); }
v8.checkThread(); checkReleased(); if (value instanceof V8Value) { v8.checkRuntime((V8Value) value); v8.addArrayNullItem(v8.getV8RuntimePtr(), getHandle()); } else if (value.equals(V8.getUndefined())) { v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle()); } else { if (value instanceof Double) { v8.addArrayDoubleItem(v8.getV8RuntimePtr(), getHandle(), (Double) value); } else if (value instanceof Integer) { v8.addArrayIntItem(v8.getV8RuntimePtr(), getHandle(), (Integer) value); } else if (value instanceof Float) { v8.addArrayDoubleItem(v8.getV8RuntimePtr(), getHandle(), ((Float) value).doubleValue()); } else if (value instanceof Number) { v8.addArrayDoubleItem(v8.getV8RuntimePtr(), getHandle(), ((Number) value).doubleValue()); } else if (value instanceof Boolean) { v8.addArrayBooleanItem(v8.getV8RuntimePtr(), getHandle(), (Boolean) value); } else if (value instanceof String) { v8.addArrayStringItem(v8.getV8RuntimePtr(), getHandle(), (String) value); } else if (value instanceof V8Value) { v8.addArrayObjectItem(v8.getV8RuntimePtr(), getHandle(), ((V8Value) value).getHandle()); } else { throw new IllegalArgumentException();
@Override public void run() { synchronized (this) { runtime = V8.createV8Runtime(); runtime.registerJavaMethod(new ExecutorTermination(), "__j2v8__checkThreadTerminate"); setup(runtime); Object scriptResult = runtime.executeScript("__j2v8__checkThreadTerminate();\n" + script, getName(), -1); if (scriptResult != null) { result = scriptResult.toString(); runtime.executeVoidFunction(messageHandler, parameters); } finally { strings.close(); } finally { synchronized (this) { if (runtime.getLocker().hasLock()) { runtime.close(); runtime = null;
@SuppressWarnings("resource") @Test public void testISENotThrownOnShutdown() { V8 v8_ = V8.createV8Runtime(); new V8Object(v8_); v8_.release(false); }
@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)"); }
/** * Adds a key value pair to the receiver where the value is a V8Value. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final V8Value value) { v8.checkThread(); checkReleased(); v8.checkRuntime(value); if (value == null) { v8.addNull(v8.getV8RuntimePtr(), objectHandle, key); } else if (value.equals(V8.getUndefined())) { v8.addUndefined(v8.getV8RuntimePtr(), objectHandle, key); } else { v8.addObject(v8.getV8RuntimePtr(), objectHandle, key, value.getHandle()); } return this; }
/*** Void Function ***/ @Test public void testSimpleVoidFunction() { v8.executeVoidScript("function foo() {x=1}"); v8.executeVoidFunction("foo", null); assertEquals(1, v8.getInteger("x")); }
@Test public void testReleaseAttachedObjects() { V8 runtime = V8.createV8Runtime(); V8Object v8Object = new V8Object(v8); runtime.registerResource(v8Object); runtime.release(true); }
/** * Pushes a V8Value to the next available spot in the Array. In * particular, this[length] = value; * * @param value The value to push to the array. * * @return The receiver. */ public V8Array push(final V8Value value) { v8.checkThread(); checkReleased(); v8.checkRuntime(value); if (value == null) { v8.addArrayNullItem(v8.getV8RuntimePtr(), getHandle()); } else if (value.equals(V8.getUndefined())) { v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle()); } else { v8.addArrayObjectItem(v8.getV8RuntimePtr(), getHandle(), value.getHandle()); } return this; }
/*** Global Object Prototype Manipulation ***/ private void setupWindowAlias() { v8.close(); v8 = V8.createV8Runtime("window"); v8.executeVoidScript("function Window(){};"); V8Object prototype = v8.executeObjectScript("Window.prototype"); v8.setPrototype(prototype); prototype.close(); }
/** * Pushes a String value to the next available spot in the Array. In * particular, this[length] = value; * * @param value The value to push to the array. * * @return The receiver. */ public V8Array push(final String value) { v8.checkThread(); checkReleased(); if (value == null) { v8.addArrayNullItem(v8.getV8RuntimePtr(), getHandle()); } else if (value.equals(V8.getUndefined())) { v8.addArrayUndefinedItem(v8.getV8RuntimePtr(), getHandle()); } else { v8.addArrayStringItem(v8.getV8RuntimePtr(), getHandle(), value); } return this; }
/** * Adds a key value pair to the receiver where the value is an integer. * * @param key The key to associate the value with. * @param value The value to add. * * @return The receiver. */ public V8Object add(final String key, final int value) { v8.checkThread(); checkReleased(); v8.add(v8.getV8RuntimePtr(), objectHandle, key, value); return this; }
/** * 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); }
@Test public void testChangeToWindowPrototypeAppearsInGlobalScope() { setupWindowAlias(); V8Object prototype = v8.executeObjectScript("Window.prototype"); prototype.add("foo", "bar"); v8.executeVoidScript("delete window.foo"); assertEquals("bar", v8.getString("foo")); assertEquals("bar", v8.executeStringScript("window.foo;")); prototype.close(); }