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


Java ScriptEngine.getContext方法代码示例

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


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

示例1: multiThreadedPrimitiveTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext);
    final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt);
    assertEquals(obj1, "original context");
    assertEquals(obj2, "new context");
    final String sharedScript = "''.foo";

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
    assertEquals(obj3, "newer context");
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();

    Assert.assertEquals(e.eval(sharedScript), "original context");
    Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:ScopeTest.java

示例2: invokeFunctionInGlobalScopeTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void invokeFunctionInGlobalScopeTest() throws Exception {
     final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
     final ScriptContext ctxt = engine.getContext();

     // define a function called "func"
     engine.eval("func = function() { return 42 }");

     // move ENGINE_SCOPE Bindings to GLOBAL_SCOPE
     ctxt.setBindings(ctxt.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.GLOBAL_SCOPE);

     // create a new Bindings and set as ENGINE_SCOPE
     ctxt.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);

     // define new function that calls "func" now in GLOBAL_SCOPE
     engine.eval("newfunc = function() { return func() }");

     // call "newfunc" and check the return value
     final Object value = ((Invocable)engine).invokeFunction("newfunc");
     assertTrue(((Number)value).intValue() == 42);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:ScopeTest.java

示例3: EngineMap

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public EngineMap()
{
    this.map = Collections.synchronizedMap(new EnumMap<>(Engine.class));

    final ScriptEngine js = Engine.JAVASCRIPT.newScriptEngine();
    this.map.put(Engine.JAVASCRIPT, js);

    try
    {
        js.eval("engines = {}");
    }
    catch (final ScriptException e)
    {
        throw new RuntimeException(e); // should never happen
    }

    final Bindings engines = (Bindings) js.get("engines");
    engines.put("js", js);

    this.context = js.getContext();
    engines.put("context", this.context);

    for (final Engine engine : Engine.values())
        this.map.computeIfAbsent(engine, e ->
        {
            final ScriptEngine scriptEngine = e.newScriptEngine(this.context);
            ((Bindings) scriptEngine.get("engines")).put(e.getName(), scriptEngine);
            return scriptEngine;
        });
}
 
开发者ID:JDA-Applications,项目名称:GuildBot,代码行数:31,代码来源:EngineMap.java

示例4: multiThreadedVarTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to defined global variables for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedVarTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    final String sharedScript = "foo";

    assertEquals(e.eval("var foo = 'original context';", origContext), null);
    assertEquals(e.eval("var foo = 'new context';", newCtxt), null);

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    assertEquals(e.eval("var foo = 'newer context';", newCtxt), null);
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();

    assertEquals(e.eval(sharedScript), "original context");
    assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:ScopeTest.java

示例5: multiThreadedGlobalTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to undefined global variables for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedGlobalTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    assertEquals(e.eval("foo = 'original context';", origContext), "original context");
    assertEquals(e.eval("foo = 'new context';", newCtxt), "new context");
    final String sharedScript = "foo";

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    final Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt);
    assertEquals(obj3, "newer context");
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();

    Assert.assertEquals(e.eval(sharedScript), "original context");
    Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:37,代码来源:ScopeTest.java

示例6: multiThreadedAccessorTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedAccessorTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'original context' })", origContext);
    e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", newCtxt);
    final String sharedScript = "({}).foo";

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    final Object obj3 = e.eval("delete Object.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
    assertEquals(obj3, "newer context");
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ScopeTest.java

示例7: multiThreadedPrimitiveAccessorTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedPrimitiveAccessorTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext);
    e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt);
    final String sharedScript = "''.foo";

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
    assertEquals(obj3, "newer context");
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ScopeTest.java

示例8: multiThreadedFunctionTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded scope function invocation for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext);
    assertEquals(origContext.getAttribute("scopeVar"), 1);
    assertEquals(e.eval("scopeTest()"), 1);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt);
    assertEquals(newCtxt.getAttribute("scopeVar"), 1);
    assertEquals(e.eval("scopeTest();", newCtxt), 1);

    assertEquals(e.eval("scopeVar = 3;", newCtxt), 3);
    assertEquals(newCtxt.getAttribute("scopeVar"), 3);


    final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000));

    t1.start();
    t2.start();
    t1.join();
    t2.join();

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ScopeTest.java

示例9: getterSetterTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to global getters and setters for shared script classes with multiple globals.
 */
@Test
public static void getterSetterTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    final String sharedScript = "accessor1";

    e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext);
    assertEquals(e.eval("accessor1 = 1;"), 1);
    assertEquals(e.eval(sharedScript), 1);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
    assertEquals(e.eval("accessor1 = 2;", newCtxt), 2);
    assertEquals(e.eval(sharedScript, newCtxt), 2);


    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));

    t1.start();
    t2.start();
    t1.join();
    t2.join();

    assertEquals(e.eval(sharedScript), 1);
    assertEquals(e.eval(sharedScript, newCtxt), 2);
    assertEquals(e.eval("v"), 1);
    assertEquals(e.eval("v", newCtxt), 2);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:ScopeTest.java

示例10: getterSetter2Test

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to global getters and setters for shared script classes with multiple globals.
 */
@Test
public static void getterSetter2Test() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    final String sharedScript = "accessor2";

    e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext);
    assertEquals(e.eval("accessor2 = 1;"), 1);
    assertEquals(e.eval(sharedScript), 1);

    e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
    assertEquals(e.eval("accessor2 = 2;", newCtxt), 2);
    assertEquals(e.eval(sharedScript, newCtxt), 2);


    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));

    t1.start();
    t2.start();
    t1.join();
    t2.join();

    assertEquals(e.eval(sharedScript), 1);
    assertEquals(e.eval(sharedScript, newCtxt), 2);
    assertEquals(e.eval("x"), 1);
    assertEquals(e.eval("x", newCtxt), 2);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:ScopeTest.java

示例11: testGlobalScopeSearch

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void testGlobalScopeSearch() throws Exception {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext c = e.getContext();
    c.setAttribute("name1234", "value", ScriptContext.GLOBAL_SCOPE);
    assertEquals(c.getAttribute("name1234"), "value");
    assertEquals(c.getAttributesScope("name1234"),
        ScriptContext.GLOBAL_SCOPE);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:ScopeTest.java

示例12: testScriptContextGetRemoveUndefined

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void testScriptContextGetRemoveUndefined() throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine e = manager.getEngineByName("nashorn");
    final ScriptContext ctx = e.getContext();
    assertNull(ctx.getAttribute("undefinedname", ScriptContext.ENGINE_SCOPE));
    assertNull(ctx.removeAttribute("undefinedname", ScriptContext.ENGINE_SCOPE));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:ScriptEngineTest.java

示例13: multiThreadedLetTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Test multi-threaded access to global lexically declared variables for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedLetTest() throws ScriptException, InterruptedException {
    final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
    final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    final String sharedScript = "foo";

    assertEquals(e.eval("let foo = 'original context';", origContext), null);
    assertEquals(e.eval("let foo = 'new context';", newCtxt), null);

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    assertEquals(e.eval("foo = 'newer context';", newCtxt), "newer context");
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();

    assertEquals(e.eval(sharedScript), "original context");
    assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:LexicalBindingTest.java

示例14: getScriptContext

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public ScriptContext getScriptContext(ScriptEngine engine)
{
	return engine.getContext();
}
 
开发者ID:L2jBrasil,项目名称:L2jBrasil,代码行数:5,代码来源:L2ScriptEngineManager.java

示例15: multiGlobalTest

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
public void multiGlobalTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    try {
        final Object obj1 = e.eval("Object");
        final Object obj2 = e.eval("Object", newCtxt);
        Assert.assertNotEquals(obj1, obj2);
        Assert.assertNotNull(obj1);
        Assert.assertNotNull(obj2);
        Assert.assertEquals(obj1.toString(), obj2.toString());

        e.eval("x = 'hello'");
        e.eval("x = 'world'", newCtxt);
        Object x1 = e.getContext().getAttribute("x");
        Object x2 = newCtxt.getAttribute("x");
        Assert.assertNotEquals(x1, x2);
        Assert.assertEquals(x1, "hello");
        Assert.assertEquals(x2, "world");

        x1 = e.eval("x");
        x2 = e.eval("x", newCtxt);
        Assert.assertNotEquals(x1, x2);
        Assert.assertEquals(x1, "hello");
        Assert.assertEquals(x2, "world");

        final ScriptContext origCtxt = e.getContext();
        e.setContext(newCtxt);
        e.eval("y = new Object()");
        e.eval("y = new Object()", origCtxt);

        final Object y1 = origCtxt.getAttribute("y");
        final Object y2 = newCtxt.getAttribute("y");
        Assert.assertNotEquals(y1, y2);
        final Object yeval1 = e.eval("y");
        final Object yeval2 = e.eval("y", origCtxt);
        Assert.assertNotEquals(yeval1, yeval2);
        Assert.assertEquals("[object Object]", y1.toString());
        Assert.assertEquals("[object Object]", y2.toString());
    } catch (final ScriptException se) {
        se.printStackTrace();
        fail(se.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:49,代码来源:ScopeTest.java


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