本文整理了Java中com.eclipsesource.v8.V8
类的一些代码示例,展示了V8
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。V8
类的具体详情如下:
包路径:com.eclipsesource.v8.V8
类名称:V8
[英]An isolated V8Runtime. All JavaScript execution must exist on a single runtime, and data is not shared between runtimes. A runtime must be created and released when finished. All access to a runtime must come from the same thread, unless the thread explicitly gives up control using the V8Locker. A public static factory method can be used to create the runtime. V8 runtime = V8.createV8Runtime();
[中]一个独立的V8Runtime。所有JavaScript执行都必须存在于一个运行时上,并且运行时之间不共享数据。运行时必须在完成时创建和发布。对运行时的所有访问必须来自同一线程,除非该线程使用V8Locker显式放弃控制。可以使用公共静态工厂方法创建运行时。V8运行时=V8。createV8Runtime();
代码示例来源:origin: eclipsesource/J2V8
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();
代码示例来源:origin: eclipsesource/J2V8
/**
* 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;
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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);
}
代码示例来源:origin: eclipsesource/J2V8
@Override
public void close() {
v8.checkThread();
if (!released) {
try {
v8.releaseObjRef(this);
} finally {
released = true;
v8.release(v8.getV8RuntimePtr(), objectHandle);
}
}
}
代码示例来源:origin: eclipsesource/J2V8
void registerCallback(final JavaCallback callback, final long objectHandle, final String jsFunctionName) {
long methodID = registerJavaMethod(getV8RuntimePtr(), objectHandle, jsFunctionName, false);
createAndRegisterMethodDescriptor(callback, methodID);
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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);
}
代码示例来源:origin: eclipsesource/J2V8
@Test(expected = V8ResultUndefined.class)
public void testResultUndefinedForNoReturnInStringFunction() {
v8.executeVoidScript("function foo() {};");
v8.executeStringFunction("foo", null);
}
代码示例来源:origin: eclipsesource/J2V8
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();
代码示例来源:origin: eclipsesource/J2V8
@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;
代码示例来源:origin: eclipsesource/J2V8
@SuppressWarnings("resource")
@Test
public void testISENotThrownOnShutdown() {
V8 v8_ = V8.createV8Runtime();
new V8Object(v8_);
v8_.release(false);
}
代码示例来源:origin: eclipsesource/J2V8
@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)");
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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;
}
代码示例来源:origin: eclipsesource/J2V8
/*** Void Function ***/
@Test
public void testSimpleVoidFunction() {
v8.executeVoidScript("function foo() {x=1}");
v8.executeVoidFunction("foo", null);
assertEquals(1, v8.getInteger("x"));
}
代码示例来源:origin: eclipsesource/J2V8
@Test
public void testReleaseAttachedObjects() {
V8 runtime = V8.createV8Runtime();
V8Object v8Object = new V8Object(v8);
runtime.registerResource(v8Object);
runtime.release(true);
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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;
}
代码示例来源:origin: eclipsesource/J2V8
/*** 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();
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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;
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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;
}
代码示例来源:origin: eclipsesource/J2V8
/**
* 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);
}
代码示例来源:origin: eclipsesource/J2V8
@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();
}
内容来源于网络,如有侵权,请联系作者删除!