Search in sources :

Example 66 with ScriptContext

use of javax.script.ScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method globalScopeAccessWithOverrideNewContextSimpleBindings.

@Test
public void globalScopeAccessWithOverrideNewContextSimpleBindings() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Becrux");
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
    context.getBindings(ScriptContext.ENGINE_SCOPE).put("globalVar", "Antares");
    assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Antares")));
    engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("globalVar");
    assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Becrux")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 67 with ScriptContext

use of javax.script.ScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method isolatedBindingsWithContextAndSimpleBindings.

@Test
public void isolatedBindingsWithContextAndSimpleBindings() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    Bindings binding = new SimpleBindings();
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    engine.eval("var value = 'Zeta Scorpii'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Zeta Scorpii")));
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Zeta Scorpii")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 68 with ScriptContext

use of javax.script.ScriptContext in project es6draft by anba.

the class ScriptEngineScopeTest method scopeInteractionDefaultContextVarAssignment.

@Test
public void scopeInteractionDefaultContextVarAssignment() throws ScriptException {
    ScriptContext context = engine.getContext();
    context.setAttribute("value", "Phecda", ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Scheat", ScriptContext.GLOBAL_SCOPE);
    engine.eval("var value = 'Aludra'", context);
    assertThat(engine.eval("this.value", context), instanceOfWith(String.class, is("Aludra")));
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Aludra")));
    assertThat(context.getAttribute("value", ScriptContext.ENGINE_SCOPE), instanceOfWith(String.class, is("Aludra")));
    assertThat(context.getAttribute("value", ScriptContext.GLOBAL_SCOPE), instanceOfWith(String.class, is("Scheat")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 69 with ScriptContext

use of javax.script.ScriptContext in project hazelcast by hazelcast.

the class HazelcastOSGiScriptEngineTest method verifyThatPutAndGetContextSuccessfully.

private void verifyThatPutAndGetContextSuccessfully(ScriptEngine scriptEngine) {
    assertNotNull(scriptEngine.getContext());
    ScriptContext mockScriptContext = mock(ScriptContext.class);
    scriptEngine.setContext(mockScriptContext);
    assertEquals(mockScriptContext, scriptEngine.getContext());
}
Also used : ScriptContext(javax.script.ScriptContext)

Example 70 with ScriptContext

use of javax.script.ScriptContext in project jmeter by apache.

the class JavaScript method executeWithNashorn.

/**
 * @param previousResult {@link SampleResult}
 * @param currentSampler {@link Sampler}
 * @param jmctx {@link JMeterContext}
 * @param vars {@link JMeterVariables}
 * @param script Javascript code
 * @param varName variable name
 * @return result as String
 * @throws InvalidVariableException
 */
private String executeWithNashorn(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
    String resultStr = null;
    try {
        ScriptContext newContext = new SimpleScriptContext();
        ScriptEngine engine = getInstance().getEngineByName(JavaScript.NASHORN_ENGINE_NAME);
        Bindings bindings = engine.createBindings();
        // Set up some objects for the script to play with
        // $NON-NLS-1$
        bindings.put("log", log);
        // $NON-NLS-1$
        bindings.put("ctx", jmctx);
        // $NON-NLS-1$
        bindings.put("vars", vars);
        // $NON-NLS-1$
        bindings.put("props", JMeterUtils.getJMeterProperties());
        // $NON-NLS-1$
        bindings.put("threadName", Thread.currentThread().getName());
        // $NON-NLS-1$
        bindings.put("sampler", currentSampler);
        // $NON-NLS-1$
        bindings.put("sampleResult", previousResult);
        newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        Object result = engine.eval(script, newContext);
        resultStr = result.toString();
        if (varName != null && vars != null) {
            // vars can be null if run from TestPlan
            vars.put(varName, resultStr);
        }
    } catch (Exception e) {
        log.error("Error processing Javascript: [{}]", script, e);
        throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
    }
    return resultStr;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) RhinoException(org.mozilla.javascript.RhinoException)

Aggregations

ScriptContext (javax.script.ScriptContext)126 SimpleScriptContext (javax.script.SimpleScriptContext)83 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptEngine (javax.script.ScriptEngine)18 ScriptException (javax.script.ScriptException)18 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)9 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5