com.eclipsesource.v8.V8.executeFunction()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中com.eclipsesource.v8.V8.executeFunction()方法的一些代码示例,展示了V8.executeFunction()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。V8.executeFunction()方法的具体详情如下:
包路径:com.eclipsesource.v8.V8
类名称:V8
方法名:executeFunction

V8.executeFunction介绍

暂无

代码示例

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke a JavaScript function and return the result as a Java Object.
 *
 * @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 Java Object representing the result of the function call.
 */
public Object executeFunction(final String name, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(parameters);
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  return v8.executeFunction(v8.getV8RuntimePtr(), UNKNOWN, objectHandle, name, parametersHandle);
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke a JavaScript function and return the result as a V8Array. If the
 * result is not a V8Array 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 V8Array representing the result of the function call or V8ResultUndefined
 * if the result is not a V8Array. The result must be released.
 */
public V8Array executeArrayFunction(final String name, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(parameters);
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  Object result = v8.executeFunction(v8.getV8RuntimePtr(), V8_ARRAY, objectHandle, name, parametersHandle);
  if (result instanceof V8Array) {
    return (V8Array) result;
  }
  throw new V8ResultUndefined();
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke a JavaScript function and return the result as a V8Object. If the
 * result is not a V8Object 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 V8Object representing the result of the function call or V8ResultUndefined
 * if the result is not a V8Object. The result must be released.
 */
public V8Object executeObjectFunction(final String name, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(parameters);
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  Object result = v8.executeFunction(v8.getV8RuntimePtr(), V8_OBJECT, objectHandle, name, parametersHandle);
  if (result instanceof V8Object) {
    return (V8Object) result;
  }
  throw new V8ResultUndefined();
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke the JavaScript function on the current runtime.
 *
 * @param receiver The object on which to call the function on. The
 * receiver will be mapped to 'this' in JavaScript. If receiver is null
 * or undefined, then the V8 runtime will be used instead.
 * @param parameters The parameters passed to the JS Function.
 *
 * @return The result of JavaScript function.
 */
@SuppressWarnings("resource")
public Object call(V8Object receiver, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(receiver);
  v8.checkRuntime(parameters);
  receiver = receiver != null ? receiver : v8;
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  long receiverHandle = receiver.isUndefined() ? v8.getHandle() : receiver.getHandle();
  return v8.executeFunction(v8.getV8RuntimePtr(), receiverHandle, objectHandle, parametersHandle);
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke a JavaScript function and return the result as a Java Object.
 *
 * @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 Java Object representing the result of the function call.
 */
public Object executeFunction(final String name, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(parameters);
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  return v8.executeFunction(v8.getV8RuntimePtr(), UNKNOWN, objectHandle, name, parametersHandle);
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithStringReturn() {
  v8.executeVoidScript("function getFoo() {return 'bar';}");
  Object result = v8.executeFunction("getFoo", null);
  assertEquals("bar", result);
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithBooleanReturn() {
  v8.executeVoidScript("function getFoo() {return true;}");
  Object result = v8.executeFunction("getFoo", null);
  assertTrue((Boolean) result);
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithNullReturn() {
  v8.executeVoidScript("function getFoo() {return null;}");
  Object result = v8.executeFunction("getFoo", null);
  assertNull(result);
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithDoubleReturn() {
  v8.executeVoidScript("function getFoo() {return 33.3;}");
  Object result = v8.executeFunction("getFoo", null);
  assertEquals(33.3, (Double) result, 0.000001);
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke a JavaScript function and return the result as a V8Array. If the
 * result is not a V8Array 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 V8Array representing the result of the function call or V8ResultUndefined
 * if the result is not a V8Array. The result must be released.
 */
public V8Array executeArrayFunction(final String name, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(parameters);
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  Object result = v8.executeFunction(v8.getV8RuntimePtr(), V8_ARRAY, objectHandle, name, parametersHandle);
  if (result instanceof V8Array) {
    return (V8Array) result;
  }
  throw new V8ResultUndefined();
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke a JavaScript function and return the result as a V8Object. If the
 * result is not a V8Object 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 V8Object representing the result of the function call or V8ResultUndefined
 * if the result is not a V8Object. The result must be released.
 */
public V8Object executeObjectFunction(final String name, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(parameters);
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  Object result = v8.executeFunction(v8.getV8RuntimePtr(), V8_OBJECT, objectHandle, name, parametersHandle);
  if (result instanceof V8Object) {
    return (V8Object) result;
  }
  throw new V8ResultUndefined();
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithUndefinedReturn() {
  v8.executeVoidScript("function getFoo() {return undefined;}");
  Object result = v8.executeFunction("getFoo", null);
  assertEquals(V8.getUndefined(), result);
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithNoReturn() {
  v8.executeVoidScript("function getAge(first, last, age) {}");
  Object result = v8.executeFunction("getAge", null);
  assertEquals(V8.getUndefined(), result);
}

代码示例来源:origin: eclipsesource/J2V8

@Test(expected = Exception.class)
public void testReleaseLockInCallback() {
  final V8 v8 = V8.createV8Runtime();
  try {
    v8.registerJavaMethod(new JavaCallback() {
      @Override
      public Object invoke(final V8Object receiver, final V8Array parameters) {
        v8.getLocker().release();
        v8.getLocker().acquire();
        return null;
      }
    }, "foo");
    v8.executeFunction("foo", null);
  } finally {
    v8.close();
  }
}

代码示例来源:origin: eclipsesource/J2V8

/**
 * Invoke the JavaScript function on the current runtime.
 *
 * @param receiver The object on which to call the function on. The
 * receiver will be mapped to 'this' in JavaScript. If receiver is null
 * or undefined, then the V8 runtime will be used instead.
 * @param parameters The parameters passed to the JS Function.
 *
 * @return The result of JavaScript function.
 */
@SuppressWarnings("resource")
public Object call(V8Object receiver, final V8Array parameters) {
  v8.checkThread();
  checkReleased();
  v8.checkRuntime(receiver);
  v8.checkRuntime(parameters);
  receiver = receiver != null ? receiver : v8;
  long parametersHandle = parameters == null ? 0 : parameters.getHandle();
  long receiverHandle = receiver.isUndefined() ? v8.getHandle() : receiver.getHandle();
  return v8.executeFunction(v8.getV8RuntimePtr(), receiverHandle, objectHandle, parametersHandle);
}

代码示例来源:origin: eclipsesource/J2V8

@Test(expected = Error.class)
public void testSharingObjectsAsFunctionCallParameters_ExecuteFunction() {
  V8 engine = null;
  V8 engine2 = null;
  try {
    engine = V8.createV8Runtime();
    engine2 = V8.createV8Runtime();
    engine.executeScript("b = function(param){return param;}");
    engine2.executeScript("a = [{name: 'joe'}];");
    V8Array a = (V8Array) engine2.get("a");
    engine.executeFunction("b", a);
  } finally {
    engine.release(false);
    engine2.release(false);
  }
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithIntegerReturn() {
  v8.executeVoidScript("function getAge(first, last, age) {return age;}");
  V8Array parameters = new V8Array(v8);
  parameters.push("John");
  parameters.push("Smith");
  parameters.push(7);
  Object result = v8.executeFunction("getAge", parameters);
  assertTrue(result instanceof Integer);
  assertEquals(7, result);
  parameters.close();
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testLosesCurrentIsolate() {
  final V8 v8 = V8.createV8Runtime();
  v8.registerJavaMethod(new JavaCallback() {
    @Override
    public Object invoke(final V8Object receiver, final V8Array parameters) {
      v8TempRuntime = V8.createV8Runtime();
      v8TempRuntime.getLocker().release();
      throw new RuntimeException();
    }
  }, "foo");
  try {
    v8.executeFunction("foo", null);
  } catch (RuntimeException e) {
    // doNothing
  }
  v8.release(false);
  v8TempRuntime.getLocker().acquire();
  v8TempRuntime.close();
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithArrayReturn() {
  v8.executeVoidScript("function getFoo() {return [1,2,3];}");
  Object result = v8.executeFunction("getFoo", null);
  assertTrue(result instanceof V8Array);
  V8Array v8Array = (V8Array) result;
  assertEquals(3, v8Array.length());
  assertEquals(1, v8Array.get(0));
  assertEquals(2, v8Array.get(1));
  assertEquals(3, v8Array.get(2));
  v8Array.close();
}

代码示例来源:origin: eclipsesource/J2V8

@Test
public void testFunctionCallWithObjectReturn() {
  v8.executeVoidScript("function getPerson(first, last, age) {return {'first':first, 'last':last, 'age':age};}");
  V8Array parameters = new V8Array(v8);
  parameters.push("John");
  parameters.push("Smith");
  parameters.push(7);
  Object result = v8.executeFunction("getPerson", parameters);
  assertTrue(result instanceof V8Object);
  V8Object v8Object = (V8Object) result;
  assertEquals("John", v8Object.getString("first"));
  assertEquals("Smith", v8Object.getString("last"));
  assertEquals(7, v8Object.getInteger("age"));
  parameters.close();
  v8Object.close();
}

相关文章

V8类方法