當前位置: 首頁>>代碼示例>>Java>>正文


Java ScriptContext.setBindings方法代碼示例

本文整理匯總了Java中javax.script.ScriptContext.setBindings方法的典型用法代碼示例。如果您正苦於以下問題:Java ScriptContext.setBindings方法的具體用法?Java ScriptContext.setBindings怎麽用?Java ScriptContext.setBindings使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.script.ScriptContext的用法示例。


在下文中一共展示了ScriptContext.setBindings方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: globalPerEngineTest

import javax.script.ScriptContext; //導入方法依賴的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: getInterfaceDifferentContext

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
/**
 * Check that we can get interface out of a script object even after
 * switching to use different ScriptContext.
 */
public void getInterfaceDifferentContext() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    try {
        final Object obj = e.eval("({ run: function() { } })");

        // change script context
        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
        e.setContext(ctxt);

        final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class);
        r.run();
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:InvocableTest.java

示例3: multiThreadedPrimitiveTest

import javax.script.ScriptContext; //導入方法依賴的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

示例4: invokeMethodDifferentContextTest

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
/**
 * Check that we can call invokeMethod on an object that we got by
 * evaluating script with different Context set.
 */
public void invokeMethodDifferentContextTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        // define an object with method on it
        final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })");

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

        // invoke 'func' on obj - but with current script context changed
        final Object res = ((Invocable) e).invokeMethod(obj, "hello");
        assertEquals(res, "Hello World!");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:InvocableTest.java

示例5: invokeFunctionInGlobalScopeTest

import javax.script.ScriptContext; //導入方法依賴的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

示例6: main

import javax.script.ScriptContext; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
        System.out.println("\nTest5\n");
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine engine = Helper.getJsEngine(m);
        if (engine == null) {
            System.out.println("Warning: No js engine found; test vacuously passes.");
            return;
        }
        Bindings g = new SimpleBindings();
        Bindings e = new SimpleBindings();
        g.put("key", "value in global");
        e.put("key", "value in engine");
        ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e, ScriptContext.ENGINE_SCOPE);
        System.out.println("engine scope only");
        e.put("count", new Integer(1));

        try (Reader reader = new FileReader(
            new File(System.getProperty("test.src", "."), "Test5.js"))) {
            engine.eval(reader,ctxt);
        }

        System.out.println("both scopes");
        ctxt.setBindings(g, ScriptContext.GLOBAL_SCOPE);
        e.put("count", new Integer(2));
        try (Reader reader = new FileReader(
            new File(System.getProperty("test.src", "."), "Test5.js"))) {
            engine.eval(reader,ctxt);
        }
        System.out.println("only global");
        e.put("count", new Integer(3));
        ctxt.removeAttribute("key", ScriptContext.ENGINE_SCOPE);
        try (Reader reader = new FileReader(
            new File(System.getProperty("test.src", "."), "Test5.js"))) {
            engine.eval(reader,ctxt);
        }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:38,代碼來源:Test5.java

示例7: multiThreadedLetTest

import javax.script.ScriptContext; //導入方法依賴的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

示例8: invokeFunctionInGlobalScopeTest2

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
public void invokeFunctionInGlobalScopeTest2() throws Exception {
     final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

     // create a new ScriptContext instance
     final ScriptContext ctxt = new SimpleScriptContext();
     // set it as 'default' ScriptContext
     engine.setContext(ctxt);

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

     // 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,代碼行數:29,代碼來源:ScopeTest.java

示例9: userEngineScopeBindingsTest

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
public void userEngineScopeBindingsTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    e.eval("function func() {}");

    final ScriptContext newContext = new SimpleScriptContext();
    newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    // we are using a new bindings - so it should have 'func' defined
    final Object value = e.eval("typeof func", newContext);
    assertTrue(value.equals("undefined"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:ScopeTest.java

示例10: userEngineScopeBindingsNoLeakTest

import javax.script.ScriptContext; //導入方法依賴的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

示例11: createFrom

import javax.script.ScriptContext; //導入方法依賴的package包/類
public static RenderContext createFrom(Bindings bindings, SlingScriptHelper slingScriptHelper) {
    final ScriptContext scriptContext = new SimpleScriptContext();
    scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    final RenderContext renderContext = new RenderContextImpl(scriptContext, slingScriptHelper);
    renderContext.getBindings().putAll(bindings);
    return renderContext;
}
 
開發者ID:deepthinkit,項目名稱:patternlab-for-sling,代碼行數:8,代碼來源:RenderContextImpl.java

示例12: multiThreadedVarTest

import javax.script.ScriptContext; //導入方法依賴的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

示例13: multiThreadedGlobalTest

import javax.script.ScriptContext; //導入方法依賴的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

示例14: multiThreadedAccessorTest

import javax.script.ScriptContext; //導入方法依賴的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

示例15: multiThreadedPrimitiveAccessorTest

import javax.script.ScriptContext; //導入方法依賴的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


注:本文中的javax.script.ScriptContext.setBindings方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。