Search in sources :

Example 61 with SimpleScriptContext

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

the class ScriptEngineScopeTest method isolatedContextsEvalContextWithDefaults.

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

Example 62 with SimpleScriptContext

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

the class ScriptEngineScopeTest method accessToBuiltinsNonSharedContext.

@Test
public void accessToBuiltinsNonSharedContext() throws ScriptException {
    Object defaultObject = engine.eval("Object");
    Object contextObject = engine.eval("Object", new SimpleScriptContext());
    assertThat(defaultObject, notNullValue());
    assertThat(contextObject, notNullValue());
    assertThat(defaultObject, Matchers.not(sameInstance(contextObject)));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) Test(org.junit.Test)

Example 63 with SimpleScriptContext

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

the class ScriptEngineScopeTest method globalScopeAccessNewEmptyContext.

@Test
public void globalScopeAccessNewEmptyContext() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Gacrux");
    assertThat(engine.eval("typeof globalVar", new SimpleScriptContext()), instanceOfWith(String.class, is("undefined")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 64 with SimpleScriptContext

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

the class ScriptEngineScopeTest method globalScopeAccessNewContextWithGlobal.

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

Example 65 with SimpleScriptContext

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

the class ScriptEngineScopeTest method scopeInteractionNewContextPropertyAssignment.

@Test
public void scopeInteractionNewContextPropertyAssignment() 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("this.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