javax.script.SimpleScriptContext Java Examples
The following examples show how to use
javax.script.SimpleScriptContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InvocableTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that invokeFunction calls functions only from current context's * Bindings. */ public void invokeFunctionDifferentContextTest() { ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it Object obj = e.eval("function hello() { return 'Hello World!'; }"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); // change engine's current context e.setContext(ctxt); ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! fail("should have thrown NoSuchMethodException"); } catch (final Exception exp) { if (!(exp instanceof NoSuchMethodException)) { exp.printStackTrace(); fail(exp.getMessage()); } } }
Example #2
Source File: ScopeTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@Test public void userEngineScopeBindingsRetentionTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); e.eval("function foo() {}", newContext); // definition retained with user's ENGINE_SCOPE Binding assertTrue(e.eval("typeof foo", newContext).equals("function")); final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // but not in another ENGINE_SCOPE binding newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("undefined")); // restore ENGINE_SCOPE and check again newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("function")); }
Example #3
Source File: InvocableTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can get interface out of a script object even after * switching to use different ScriptContext. */ public void getInterfaceDifferentContext() { ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine e = m.getEngineByName("nashorn"); try { Object obj = e.eval("({ run: function() { } })"); // change script context ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); r.run(); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #4
Source File: InvocableTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can get interface out of a script object even after * switching to use different ScriptContext. */ public void getInterfaceDifferentContext() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { final Object obj = e.eval("({ run: function() { } })"); // change script context final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); r.run(); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #5
Source File: ScriptEngineScopeTest.java From es6draft with MIT License | 6 votes |
@Test public void scopeInteractionNewContextSimpleBindingsVarAssignment() 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("var 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"))); }
Example #6
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals. * @throws ScriptException * @throws InterruptedException */ @Test public static void testSlowScope() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); for (int i = 0; i < 100; i++) { final Bindings b = e.createBindings(); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt); assertEquals(e.eval("a", ctxt), 1); assertEquals(b.get("a"), 1); assertEquals(e.eval("b", ctxt), 3); assertEquals(b.get("b"), 3); assertEquals(e.eval("c", ctxt), 10); assertEquals(b.get("c"), 10); } }
Example #7
Source File: TrustedScriptEngineTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 6 votes |
@Test public void evalFileNameWithSpecialCharsTest() throws ScriptException { final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); final ScriptContext ctxt = new SimpleScriptContext(); // use file name with "dangerous" chars. ctxt.setAttribute(ScriptEngine.FILENAME, "<myscript>", ScriptContext.ENGINE_SCOPE); engine.eval("var a = 3;"); ctxt.setAttribute(ScriptEngine.FILENAME, "[myscript]", ScriptContext.ENGINE_SCOPE); engine.eval("var h = 'hello';"); ctxt.setAttribute(ScriptEngine.FILENAME, ";/\\$.", ScriptContext.ENGINE_SCOPE); engine.eval("var foo = 'world';"); // name used by jjs shell tool for the interactive mode ctxt.setAttribute(ScriptEngine.FILENAME, "<shell>", ScriptContext.ENGINE_SCOPE); engine.eval("var foo = 'world';"); }
Example #8
Source File: JavaScriptUtil.java From framework with Apache License 2.0 | 6 votes |
/** * Description: 执行脚本<br> * * @author 王伟 <br> * @param script script * @param params params * @return 脚本返回值 * @throws UtilException <br> */ public static Object eval(final String script, final Map<String, Object> params) throws UtilException { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("javascript"); try { ScriptContext context = new SimpleScriptContext(); if (params != null) { for (Entry<String, Object> entry : params.entrySet()) { context.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE); } } return engine.eval(script, context); } catch (ScriptException e) { throw new UtilException(ErrorCodeDef.EVAL_JAVASCRIPT_ERROR_10042, e); } }
Example #9
Source File: InvocableTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can call invokeMethod on an object that we got by * evaluating script with different Context set. */ public void invokeMethodDifferentContextTest() { ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); // invoke 'func' on obj - but with current script context changed final Object res = ((Invocable) e).invokeMethod(obj, "hello"); assertEquals(res, "Hello World!"); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #10
Source File: ScriptEventHandler.java From neoscada with Eclipse Public License 1.0 | 6 votes |
@Override public Event handleEvent ( final Event event, final InjectionContext context ) { final ScriptContext scriptContext = new SimpleScriptContext (); try { scriptContext.setAttribute ( "event", event, ScriptContext.GLOBAL_SCOPE ); scriptContext.setAttribute ( "logger", logger, ScriptContext.GLOBAL_SCOPE ); scriptContext.setAttribute ( "injector", this.injector, ScriptContext.GLOBAL_SCOPE ); final Object result = this.script.execute ( scriptContext ); final Event resultEvent = convert ( result, event ); logger.debug ( "Result: {}", resultEvent ); return resultEvent; } catch ( final Exception e ) { return event; } }
Example #11
Source File: MultiScopes.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(final String[] args) throws Exception { final ScriptEngineManager manager = new ScriptEngineManager(); final ScriptEngine engine = manager.getEngineByName("nashorn"); engine.put("x", "hello"); // print global variable "x" engine.eval("print(x);"); // the above line prints "hello" // Now, pass a different script context final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); final Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // add new variable "x" to the new engineScope engineScope.put("x", "world"); // execute the same script - but this time pass a different script context engine.eval("print(x);", newContext); // the above line prints "world" }
Example #12
Source File: InvocableTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that invokeFunction calls functions only from current context's * Bindings. */ public void invokeFunctionDifferentContextTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it e.eval("function hello() { return 'Hello World!'; }"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); // change engine's current context e.setContext(ctxt); ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! fail("should have thrown NoSuchMethodException"); } catch (final Exception exp) { if (!(exp instanceof NoSuchMethodException)) { exp.printStackTrace(); fail(exp.getMessage()); } } }
Example #13
Source File: ScriptEngineScopeTest.java From es6draft with MIT License | 6 votes |
@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"))); }
Example #14
Source File: InvocableTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that invokeFunction calls functions only from current context's * Bindings. */ public void invokeFunctionDifferentContextTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it e.eval("function hello() { return 'Hello World!'; }"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); // change engine's current context e.setContext(ctxt); ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context! fail("should have thrown NoSuchMethodException"); } catch (final Exception exp) { if (!(exp instanceof NoSuchMethodException)) { exp.printStackTrace(); fail(exp.getMessage()); } } }
Example #15
Source File: InvocableTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can get interface out of a script object even after * switching to use different ScriptContext. */ public void getInterfaceDifferentContext() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { final Object obj = e.eval("({ run: function() { } })"); // change script context final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); r.run(); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #16
Source File: InvocableTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can call invokeMethod on an object that we got by * evaluating script with different Context set. */ public void invokeMethodDifferentContextTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); // invoke 'func' on obj - but with current script context changed final Object res = ((Invocable) e).invokeMethod(obj, "hello"); assertEquals(res, "Hello World!"); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #17
Source File: InvocableTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can get interface out of a script object even after * switching to use different ScriptContext. */ public void getInterfaceDifferentContext() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { final Object obj = e.eval("({ run: function() { } })"); // change script context final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); r.run(); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #18
Source File: TrustedScriptEngineTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Test public void evalFileNameWithSpecialCharsTest() throws ScriptException { final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); final ScriptContext ctxt = new SimpleScriptContext(); // use file name with "dangerous" chars. ctxt.setAttribute(ScriptEngine.FILENAME, "<myscript>", ScriptContext.ENGINE_SCOPE); engine.eval("var a = 3;"); ctxt.setAttribute(ScriptEngine.FILENAME, "[myscript]", ScriptContext.ENGINE_SCOPE); engine.eval("var h = 'hello';"); ctxt.setAttribute(ScriptEngine.FILENAME, ";/\\$.", ScriptContext.ENGINE_SCOPE); engine.eval("var foo = 'world';"); // name used by jjs shell tool for the interactive mode ctxt.setAttribute(ScriptEngine.FILENAME, "<shell>", ScriptContext.ENGINE_SCOPE); engine.eval("var foo = 'world';"); }
Example #19
Source File: InvocableTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can get interface out of a script object even after * switching to use different ScriptContext. */ public void getInterfaceDifferentContext() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { final Object obj = e.eval("({ run: function() { } })"); // change script context final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class); r.run(); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #20
Source File: MultiScopes.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
public static void main(final String[] args) throws Exception { final ScriptEngineManager manager = new ScriptEngineManager(); final ScriptEngine engine = manager.getEngineByName("nashorn"); engine.put("x", "hello"); // print global variable "x" engine.eval("print(x);"); // the above line prints "hello" // Now, pass a different script context final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); final Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // add new variable "x" to the new engineScope engineScope.put("x", "world"); // execute the same script - but this time pass a different script context engine.eval("print(x);", newContext); // the above line prints "world" }
Example #21
Source File: TrustedScriptEngineTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@Test public void evalFileNameWithSpecialCharsTest() throws ScriptException { final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" }); final ScriptContext ctxt = new SimpleScriptContext(); // use file name with "dangerous" chars. ctxt.setAttribute(ScriptEngine.FILENAME, "<myscript>", ScriptContext.ENGINE_SCOPE); engine.eval("var a = 3;"); ctxt.setAttribute(ScriptEngine.FILENAME, "[myscript]", ScriptContext.ENGINE_SCOPE); engine.eval("var h = 'hello';"); ctxt.setAttribute(ScriptEngine.FILENAME, ";/\\$.", ScriptContext.ENGINE_SCOPE); engine.eval("var foo = 'world';"); // name used by jjs shell tool for the interactive mode ctxt.setAttribute(ScriptEngine.FILENAME, "<shell>", ScriptContext.ENGINE_SCOPE); engine.eval("var foo = 'world';"); }
Example #22
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 6 votes |
@Test public void userEngineScopeBindingsRetentionTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); e.eval("function foo() {}", newContext); // definition retained with user's ENGINE_SCOPE Binding assertTrue(e.eval("typeof foo", newContext).equals("function")); final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE); // but not in another ENGINE_SCOPE binding newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("undefined")); // restore ENGINE_SCOPE and check again newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE); assertTrue(e.eval("typeof foo", newContext).equals("function")); }
Example #23
Source File: InvocableTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test /** * Check that we can call invokeMethod on an object that we got by * evaluating script with different Context set. */ public void invokeMethodDifferentContextTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); try { // define an object with method on it final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); e.setContext(ctxt); // invoke 'func' on obj - but with current script context changed final Object res = ((Invocable) e).invokeMethod(obj, "hello"); assertEquals(res, "Hello World!"); } catch (final Exception exp) { exp.printStackTrace(); fail(exp.getMessage()); } }
Example #24
Source File: ScopeTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to global getters and setters for shared script classes with multiple globals. */ @Test public static void getterSetterTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); final String sharedScript = "accessor1"; e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); assertEquals(e.eval("accessor1 = 1;"), 1); assertEquals(e.eval(sharedScript), 1); e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); assertEquals(e.eval("accessor1 = 2;", newCtxt), 2); assertEquals(e.eval(sharedScript, newCtxt), 2); final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); t1.start(); t2.start(); t1.join(); t2.join(); assertEquals(e.eval(sharedScript), 1); assertEquals(e.eval(sharedScript, newCtxt), 2); assertEquals(e.eval("v"), 1); assertEquals(e.eval("v", newCtxt), 2); }
Example #25
Source File: ScriptEngineTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test public void compileAndEvalInDiffContextTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine engine = m.getEngineByName("js"); final Compilable compilable = (Compilable) engine; final CompiledScript compiledScript = compilable.compile("foo"); final ScriptContext ctxt = new SimpleScriptContext(); ctxt.setAttribute("foo", "hello", ScriptContext.ENGINE_SCOPE); assertEquals(compiledScript.eval(ctxt), "hello"); }
Example #26
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Test public static void contextOverwriteTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = new SimpleBindings(); b.put("context", "hello"); b.put("foo", 32); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.setContext(newCtxt); assertEquals(e.eval("context"), "hello"); assertEquals(((Number)e.eval("foo")).intValue(), 32); }
Example #27
Source File: CompilableTest.java From es6draft with MIT License | 5 votes |
@Test public void compileStringWithContextAndSimpleBindings() throws ScriptException { CompiledScript script = compilable.compile("numberVal * 2"); ScriptContext context = new SimpleScriptContext(); Bindings bindings = new SimpleBindings(); bindings.put("numberVal", 9); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); assertThat(script.eval(context), instanceOfWith(Number.class, is(numberCloseTo(18)))); }
Example #28
Source File: ScriptEngineScopeTest.java From es6draft with MIT License | 5 votes |
@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"))); }
Example #29
Source File: ScriptEngineScopeTest.java From es6draft with MIT License | 5 votes |
@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))); }
Example #30
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to primitive prototype user accessor properties for shared script classes with multiple globals. */ @Test public static void multiThreadedPrimitiveAccessorTest() throws ScriptException, InterruptedException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext origContext = e.getContext(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'original context' })", origContext); e.eval("Object.defineProperty(String.prototype, 'foo', { get: function() 'new context' })", newCtxt); final String sharedScript = "''.foo"; final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); t1.start(); t2.start(); t1.join(); t2.join(); final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); assertEquals(obj3, "newer context"); final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); t3.start(); t4.start(); t3.join(); t4.join(); }