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

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

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

V8.executeStringScript介绍

[英]Executes a JS Script on this runtime and returns the result as a String. If the result is not a String, then a V8ResultUndefinedException is thrown.
[中]在此运行时执行JS脚本,并将结果作为字符串返回。如果结果不是字符串,则抛出V8ResultUndefinedException。

代码示例

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

/**
 * Executes a JS Script on this runtime and returns the result as a String.
 * If the result is not a String, then a V8ResultUndefinedException is thrown.
 *
 * @param script The script to execute.
 *
 * @return The result of the script as a String, or V8ResultUndefinedException if
 * the result is not a String.
 */
public String executeStringScript(final String script) {
  return executeStringScript(script, null, 0);
}

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

/**
 * Executes a JS Script on this runtime and returns the result as a String.
 * If the result is not a String, 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 String, or V8ResultUndefinedException if
 * the result is not a String.
 */
public String executeStringScript(final String script, final String scriptName, final int lineNumber) {
  checkThread();
  checkScript(script);
  return executeStringScript(v8RuntimePtr, script, scriptName, lineNumber);
}

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

/**
 * Executes a JS Script on this runtime and returns the result as a String.
 * If the result is not a String, then a V8ResultUndefinedException is thrown.
 *
 * @param script The script to execute.
 *
 * @return The result of the script as a String, or V8ResultUndefinedException if
 * the result is not a String.
 */
public String executeStringScript(final String script) {
  return executeStringScript(script, null, 0);
}

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

@Test(expected = V8ScriptExecutionException.class)
public void testV8ScriptExecutionExceptionStringScript() {
  v8.executeStringScript(undefinedAccessScript + "'string';", "file", 0);
}

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

@Test(expected = V8ScriptCompilationException.class)
public void testSimpleSyntaxErrorStringScript() {
  v8.executeStringScript("'a");
}

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

@Test
public void testCreateV8ArrayFromStringList() {
  List<String> list = new ArrayList<String>();
  list.add("hello");
  list.add("world");
  int size = registerAndRelease("result", list);
  assertEquals(2, size);
  assertEquals("hello", v8.executeStringScript("result[0]"));
  assertEquals("world", v8.executeStringScript("result[1]"));
}

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

@Test
public void testCreateV8ObjectFromStringMap() {
  Map<String, String> map = new HashMap<String, String>();
  map.put("first", "john");
  map.put("last", "smith");
  int size = registerAndRelease("result", map);
  assertEquals(2, size);
  assertEquals("john", v8.executeStringScript("result.first"));
  assertEquals("smith", v8.executeStringScript("result['last']"));
}

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

@Test
public void testExecuteUnicodeScript() {
  String result = v8.executeStringScript("var ಠ_ಠ = function() { return '🌞' + '💐'; }; ಠ_ಠ();");
  assertEquals("🌞💐", result);
}

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

@Test
public void testVoidMethodCalledFromStringScript() {
  ICallback callback = mock(ICallback.class);
  v8.registerJavaMethod(callback, "voidMethodNoParameters", "foo", new Class<?>[0]);
  v8.executeStringScript("foo();'test'");
  verify(callback).voidMethodNoParameters();
}

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

@Test
public void testStringScriptWithName() {
  String result = v8.executeStringScript("'hello, world'", "name", 5);
  assertEquals("hello, world", result);
}

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

@Test(expected = Error.class)
public void testCannotAccessDisposedIsolateString() {
  v8.close();
  v8.executeStringScript("'foo'");
}

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

/*** String Script ***/
@Test
public void testSimpleStringScript() {
  String result = v8.executeStringScript("'hello, world'");
  assertEquals("hello, world", result);
}

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

@Test
public void testStringMethodCalledFromScriptWithResult() {
  ICallback callback = mock(ICallback.class);
  doReturn("bar").when(callback).stringMethodNoParameters();
  v8.registerJavaMethod(callback, "stringMethodNoParameters", "foo", new Class<?>[0]);
  String result = v8.executeStringScript("foo();");
  assertEquals("bar", result);
}

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

@Test
public void testCreateV8ArrayWithNullValues() {
  List<String> list = new ArrayList<String>();
  list.add("hello");
  list.add(null);
  list.add("world");
  int size = registerAndRelease("result", list);
  assertEquals(3, size);
  assertEquals("hello", v8.executeStringScript("result[0]"));
  assertTrue(v8.executeBooleanScript("typeof result[1] === 'undefined'"));
  assertEquals("world", v8.executeStringScript("result[2]"));
}

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

/*** Add String ***/
@Test
public void testAddString() {
  v8.add("foo", "hello, world!");
  String result = v8.executeStringScript("foo");
  assertEquals("hello, world!", result);
}

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

@Test
public void testAddReplaceValue() {
  v8.add("foo", true);
  v8.add("foo", "test");
  String result = v8.executeStringScript("foo");
  assertEquals("test", result);
}

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

@Test
public void testAddStringReplaceValue() {
  v8.add("foo", "hello");
  v8.add("foo", "world");
  String result = v8.executeStringScript("foo");
  assertEquals("world", result);
}

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

@Test
public void testAddObjectWithString() {
  V8Object v8Object = new V8Object(v8);
  v8Object.add("hello", "world");
  v8.add("foo", v8Object);
  String result = v8.executeStringScript("foo.hello");
  assertEquals("world", result);
  v8Object.close();
}

代码示例来源: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();
}

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

@Test
public void testAddObjectToObject() {
  V8Object nested = new V8Object(v8);
  nested.add("foo", "bar");
  V8Object v8Object = new V8Object(v8);
  v8Object.add("nested", nested);
  v8.add("foo", v8Object);
  String result = v8.executeStringScript("foo.nested.foo");
  assertEquals("bar", result);
  v8Object.close();
  nested.close();
}

相关文章

微信公众号

最新文章

更多

V8类方法