当前位置: 首页>>代码示例>>Java>>正文


Java ScriptEngine.eval方法代码示例

本文整理汇总了Java中javax.script.ScriptEngine.eval方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngine.eval方法的具体用法?Java ScriptEngine.eval怎么用?Java ScriptEngine.eval使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.script.ScriptEngine的用法示例。


在下文中一共展示了ScriptEngine.eval方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: globalPerEngineTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void globalPerEngineTest() throws ScriptException {
    final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    final String[] options = new String[] { "--global-per-engine" };
    final ScriptEngine e = fac.getScriptEngine(options);

    e.eval("function foo() {}");

    final ScriptContext newCtx = new SimpleScriptContext();
    newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);

    // all global definitions shared and so 'foo' should be
    // visible in new Bindings as well.
    assertTrue(e.eval("typeof foo", newCtx).equals("function"));

    e.eval("function bar() {}", newCtx);

    // bar should be visible in default context
    assertTrue(e.eval("typeof bar").equals("function"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:TrustedScriptEngineTest.java

示例2: printManyTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// check that print prints all arguments (more than one)
public void printManyTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final StringWriter sw = new StringWriter();
    e.getContext().setWriter(sw);
    try {
        e.eval("print(34, true, 'hello')");
    } catch (final Throwable t) {
        t.printStackTrace();
        fail(t.getMessage());
    }

    assertEquals(sw.toString(), println("34 true hello"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ScriptEngineTest.java

示例3: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName("nashorn");

    engine.put("x", "hello");
    // print global variable "x"
    engine.eval("print(x);");
    // the above line prints "hello"

    // Now, pass a different script context
    final ScriptContext newContext = new SimpleScriptContext();
    newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    final Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);

    // add new variable "x" to the new engineScope
    engineScope.put("x", "world");

    // execute the same script - but this time pass a different script context
    engine.eval("print(x);", newContext);
    // the above line prints "world"
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:MultiScopes.java

示例4: indexedAccessTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// array-like indexed access for a JSObject
public void indexedAccessTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    try {
        final BufferObject buf = new BufferObject(2);
        e.put("buf", buf);

        // array-like access on BufferObject objects
        assertEquals(e.eval("buf.length"), buf.getBuffer().capacity());
        e.eval("buf[0] = 23");
        assertEquals(buf.getBuffer().get(0), 23);
        assertEquals(e.eval("buf[0]"), 23);
        assertEquals(e.eval("buf[1]"), 0);
        buf.getBuffer().put(1, 42);
        assertEquals(e.eval("buf[1]"), 42);
        assertEquals(e.eval("Array.isArray(buf)"), Boolean.TRUE);
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:PluggableJSObjectTest.java

示例5: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("\nTest8\n");
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine e  = Helper.getJsEngine(m);
    if (e == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    e.eval(new FileReader(
        new File(System.getProperty("test.src", "."), "Test8.js")));
    Invocable inv = (Invocable)e;
    inv.invokeFunction("main", "Mustang");
    // use method of a specific script object
    Object scriptObj = e.get("scriptObj");
    inv.invokeMethod(scriptObj, "main", "Mustang");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:Test8.java

示例6: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("\nTest3\n");
    final Reader reader = new FileReader(
        new File(System.getProperty("test.src", "."), "Test3.js"));
    ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine engine = Helper.getJsEngine(m);
    if (engine == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    Bindings en = new SimpleBindings();
    engine.setBindings(en, ScriptContext.ENGINE_SCOPE);
    en.put("key", "engine value");
    Bindings gn = new SimpleBindings();
    engine.setBindings(gn, ScriptContext.GLOBAL_SCOPE);
    gn.put("key", "global value");
    engine.eval(reader);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:Test3.java

示例7: iteratingJSObjectTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// iteration tests
public void iteratingJSObjectTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    try {
        final MapWrapperObject obj = new MapWrapperObject();
        obj.setMember("foo", "hello");
        obj.setMember("bar", "world");
        e.put("obj", obj);

        // check for..in
        Object val = e.eval("var str = ''; for (i in obj) str += i; str");
        assertEquals(val.toString(), "foobar");

        // check for..each..in
        val = e.eval("var str = ''; for each (i in obj) str += i; str");
        assertEquals(val.toString(), "helloworld");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:PluggableJSObjectTest.java

示例8: userEngineScopeBindingsRetentionTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void userEngineScopeBindingsRetentionTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext newContext = new SimpleScriptContext();
    newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    e.eval("function foo() {}", newContext);

    // definition retained with user's ENGINE_SCOPE Binding
    assertTrue(e.eval("typeof foo", newContext).equals("function"));

    final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
    // but not in another ENGINE_SCOPE binding
    newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    assertTrue(e.eval("typeof foo", newContext).equals("undefined"));

    // restore ENGINE_SCOPE and check again
    newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE);
    assertTrue(e.eval("typeof foo", newContext).equals("function"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:ScopeTest.java

示例9: userEngineScopeBindingsNoLeakTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void userEngineScopeBindingsNoLeakTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext newContext = new SimpleScriptContext();
    newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    e.eval("function foo() {}", newContext);

    // in the default context's ENGINE_SCOPE, 'foo' shouldn't exist
    assertTrue(e.eval("typeof foo").equals("undefined"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:ScopeTest.java

示例10: redefineEchoTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void redefineEchoTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:ScriptEngineTest.java

示例11: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable
{
	// instantiate a new ScriptEngineManager
	ScriptEngineManager myEngineManager = new ScriptEngineManager();

	// instantiate a new Nashorn ScriptEngine
	ScriptEngine myEngine = myEngineManager.getEngineByName("nashorn");
	
	// create the JavaScript function
	myEngine.eval("function addTest(x, y) { return x + y; }");
	
	// generate output including a call to the addTest function via the engine
	System.out.println("The addition results are: " + myEngine.eval("addTest(300, 19);"));
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Java-9,代码行数:15,代码来源:EmbeddedAddTest.java

示例12: setWriterTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void setWriterTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final StringWriter sw = new StringWriter();
    e.getContext().setWriter(sw);

    try {
        e.eval("print('hello world')");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
    assertEquals(sw.toString(), println("hello world"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:ScriptEngineTest.java

示例13: defaultMethodTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void defaultMethodTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Invocable inv = (Invocable) e;

    final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})");
    @SuppressWarnings("unchecked")
    final Function<String, String> func = inv.getInterface(obj, Function.class);
    assertEquals(func.apply("hello"), "HELLO");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:InvocableTest.java

示例14: consStringTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
// ConsString attribute access on a JSObject
public void consStringTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    try {
        final MapWrapperObject obj = new MapWrapperObject();
        e.put("obj", obj);
        e.put("f", "f");
        e.eval("obj[f + 'oo'] = 'bar';");

        assertEquals(obj.getMap().get("foo"), "bar");
        assertEquals(e.eval("obj[f + 'oo']"), "bar");
        assertEquals(e.eval("obj['foo']"), "bar");
        assertEquals(e.eval("f + 'oo' in obj"), Boolean.TRUE);
        assertEquals(e.eval("'foo' in obj"), Boolean.TRUE);
        e.eval("delete obj[f + 'oo']");
        assertFalse(obj.getMap().containsKey("foo"));
        assertEquals(e.eval("obj[f + 'oo']"), null);
        assertEquals(e.eval("obj['foo']"), null);
        assertEquals(e.eval("f + 'oo' in obj"), Boolean.FALSE);
        assertEquals(e.eval("'foo' in obj"), Boolean.FALSE);
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:PluggableJSObjectTest.java

示例15: testWrapObjectWithArray

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Wrap an embedded array as a list.
 */
@Test
public void testWrapObjectWithArray() throws ScriptException {
    final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
    final Object val = engine.eval("Java.asJSONCompatible({x: [1, 2, 3]})");
    assertEquals(asList(asMap(val).get("x")), Arrays.asList(1, 2, 3));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:JSONCompatibleTest.java


注:本文中的javax.script.ScriptEngine.eval方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。