Search in sources :

Example 16 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project kernel by cristal-ise.

the class Script method setScriptEngine.

/**
 * Sets the language
 *
 * @param requestedLang the language
 */
public void setScriptEngine(String requestedLang) throws ScriptingEngineException {
    String lang = Gateway.getProperties().getString("OverrideScriptLang." + requestedLang, requestedLang);
    ScriptEngineManager sem = (ScriptEngineManager) Gateway.getProperties().getObject("Script.EngineManager");
    if (sem == null)
        sem = new ScriptEngineManager(getClass().getClassLoader());
    engine = sem.getEngineByName(lang);
    if (engine == null)
        throw new ScriptingEngineException("No script engine for '" + lang + "' found.");
    mLanguage = requestedLang;
    context = new SimpleScriptContext();
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    engine.setContext(context);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptEngineManager(javax.script.ScriptEngineManager)

Example 17 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project revapi by revapi.

the class ConfigurationValidator method getJsEngine.

private ScriptEngine getJsEngine(Writer output) throws IOException, ScriptException {
    ScriptEngine ret = null;
    if (jsEngine != null) {
        ret = jsEngine.get();
    }
    if (ret == null) {
        ret = new ScriptEngineManager().getEngineByName("javascript");
        ScriptContext ctx = new SimpleScriptContext();
        Bindings globalScope = ret.createBindings();
        ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
        initTv4(ret, globalScope);
        ret.setContext(ctx);
        jsEngine = new WeakReference<>(ret);
    }
    ret.getContext().setWriter(output);
    ret.getContext().setErrorWriter(output);
    return ret;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptEngineManager(javax.script.ScriptEngineManager) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 18 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test7.

private static void test7() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var foo = 23;");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context1 = new SimpleScriptContext();
    context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context2 = new SimpleScriptContext();
    context2.getBindings(ScriptContext.ENGINE_SCOPE).put("foo", defaultBindings.get("foo"));
    engine.eval("foo = 44;", context1);
    engine.eval("print(foo);", context1);
    engine.eval("print(foo);", context2);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 19 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test8.

private static void test8() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("var obj = { foo: 23 };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context1 = new SimpleScriptContext();
    context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context2 = new SimpleScriptContext();
    context2.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", defaultBindings.get("obj"));
    engine.eval("obj.foo = 44;", context1);
    engine.eval("print(obj.foo);", context1);
    engine.eval("print(obj.foo);", context2);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Example 20 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test4.

private static void test4() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("function foo() { print('bar') };");
    ScriptContext defaultContext = engine.getContext();
    Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SimpleScriptContext context = new SimpleScriptContext();
    context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
    engine.eval("foo();", context);
    System.out.println(context.getAttribute("foo"));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)74 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2