Search in sources :

Example 26 with SimpleScriptContext

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

the class ScriptEngineScopeTest method scopeInteractionNewContextSimpleBindingsPropertyAssignment.

@Test
public void scopeInteractionNewContextSimpleBindingsPropertyAssignment() 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("this.value = 'Aludra'", context);
    assertThat(engine.eval("this.value", context), nullValue());
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Phecda")));
    assertThat(context.getAttribute("value", ScriptContext.ENGINE_SCOPE), instanceOfWith(String.class, is("Phecda")));
    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 27 with SimpleScriptContext

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

the class ScriptEngineScopeTest method bindingValueWithThisExplicitContextSimpleBindings.

@Test
public void bindingValueWithThisExplicitContextSimpleBindings() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setAttribute("value", "Markeb", ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("this.value", context), nullValue());
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 28 with SimpleScriptContext

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

the class ScriptEngineScopeTest method isolatedContextsNoVarEvalDefault.

@Test
public void isolatedContextsNoVarEvalDefault() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    engine.eval("value = 'Rigel'");
    assertThat(engine.eval("value"), instanceOfWith(String.class, is("Rigel")));
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    assertThat(engine.getContext().getAttribute("value"), instanceOfWith(String.class, is("Rigel")));
    assertThat(context.getAttribute("value"), nullValue());
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 29 with SimpleScriptContext

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

the class ScriptEngineScopeTest method isolatedContextsEvalDefault.

@Test
public void isolatedContextsEvalDefault() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    engine.eval("var value = 'Sirius'");
    assertThat(engine.eval("value"), instanceOfWith(String.class, is("Sirius")));
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 30 with SimpleScriptContext

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

the class ScriptEngineScopeTest method scopeInteractionNewContextGlobalAssignment.

@Test
public void scopeInteractionNewContextGlobalAssignment() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
    context.setBindings(engine.createBindings(), 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), 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) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

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