Search in sources :

Example 31 with SimpleScriptContext

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

the class ScriptEngineScopeTest method scopeInteractionNewContextSimpleBindingsGlobalAssignment.

@Test
public void scopeInteractionNewContextSimpleBindingsGlobalAssignment() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Phecda", ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Scheat", ScriptContext.GLOBAL_SCOPE);
    engine.eval("value = 'Aludra'", context);
    assertThat(engine.eval("this.value", context), nullValue());
    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) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 32 with SimpleScriptContext

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

the class ScriptEngineScopeTest method bindingValueWithThisExplicitContext.

@Test
public void bindingValueWithThisExplicitContext() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    context.setAttribute("value", "Markab", ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("this.value", context), instanceOfWith(String.class, is("Markab")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 33 with SimpleScriptContext

use of javax.script.SimpleScriptContext 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 34 with SimpleScriptContext

use of javax.script.SimpleScriptContext 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 35 with SimpleScriptContext

use of javax.script.SimpleScriptContext 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

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