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")));
}
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")));
}
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")));
}
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());
}
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;
}
Aggregations