Search in sources :

Example 56 with ScriptContext

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

the class ScriptEngineScopeTest method isolatedContextsEvalContext.

@Test
public void isolatedContextsEvalContext() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    engine.eval("var value = 'Vega'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Vega")));
    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 57 with ScriptContext

use of javax.script.ScriptContext 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 58 with ScriptContext

use of javax.script.ScriptContext 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 59 with ScriptContext

use of javax.script.ScriptContext 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 60 with ScriptContext

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

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