Java Code Examples for javax.script.ScriptEngine#createBindings()
The following examples show how to use
javax.script.ScriptEngine#createBindings() .
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: SoapUIScriptEngineBinder.java From microcks with Apache License 2.0 | 6 votes |
/** * Create and bind a SoapUI environment for a ScriptEngine. * @param engine The engine to enrich with binding environment. * @param requestContent The content of request to use as data * @param request The wrapped incoming servlet request. */ public static void bindSoapUIEnvironment(ScriptEngine engine, String requestContent, HttpServletRequest request){ // Build a map of header values. StringToStringsMap headers = new StringToStringsMap(); for (String headerName : Collections.list(request.getHeaderNames())) { headers.put(headerName, Collections.list(request.getHeaders(headerName))); } // Build a fake request container. FakeSoapUIMockRequest mockRequest = new FakeSoapUIMockRequest(requestContent, headers); mockRequest.setRequest(request); // Create bindings and put content according to SoapUI binding environment. Bindings bindings = engine.createBindings(); bindings.put("mockRequest", mockRequest); bindings.put("log", log); engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); }
Example 2
Source File: ScopeTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
@Test public void createBindingsTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); Bindings b = e.createBindings(); b.put("foo", 42.0); Object res = null; try { res = e.eval("foo == 42.0", b); } catch (final ScriptException | NullPointerException se) { se.printStackTrace(); fail(se.getMessage()); } assertEquals(res, Boolean.TRUE); }
Example 3
Source File: ScriptObjectMirrorTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
@Test public void mapScriptObjectMirrorCallsiteTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine engine = m.getEngineByName("nashorn"); final String TEST_SCRIPT = "typeof obj.foo"; final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE); engine.eval("var obj = java.util.Collections.emptyMap()"); // this will drive callsite "obj.foo" of TEST_SCRIPT // to use "obj instanceof Map" as it's guard engine.eval(TEST_SCRIPT, global); // redefine 'obj' to be a script object engine.eval("obj = {}"); final Bindings newGlobal = engine.createBindings(); // transfer 'obj' from default global to new global // new global will get a ScriptObjectMirror wrapping 'obj' newGlobal.put("obj", global.get("obj")); // Every ScriptObjectMirror is a Map! If callsite "obj.foo" // does not see the new 'obj' is a ScriptObjectMirror, it'll // continue to use Map's get("obj.foo") instead of ScriptObjectMirror's // getMember("obj.foo") - thereby getting null instead of undefined assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal)); }
Example 4
Source File: GremlinGroovyScriptEngineOverGraphTest.java From tinkerpop with Apache License 2.0 | 6 votes |
@Test public void shouldClearBindingsBetweenEvals() throws Exception { final Graph graph = TinkerFactory.createModern(); final GraphTraversalSource g = graph.traversal(); final ScriptEngine engine = new GremlinGroovyScriptEngine(); engine.put("g", g); engine.put("marko", convertToVertexId(graph, "marko")); assertEquals(g.V(convertToVertexId(graph, "marko")).next(), engine.eval("g.V(marko).next()")); final Bindings bindings = engine.createBindings(); bindings.put("g", g); bindings.put("s", "marko"); assertEquals(engine.eval("g.V().has('name',s).next()", bindings), g.V(convertToVertexId(graph, "marko")).next()); try { engine.eval("g.V().has('name',s).next()"); fail("This should have failed because s is no longer bound"); } catch (Exception ex) { final Throwable t = ExceptionUtils.getRootCause(ex); assertEquals(MissingPropertyException.class, t.getClass()); assertTrue(t.getMessage().startsWith("No such property: s for class")); } }
Example 5
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 6
Source File: ScopeTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to prototype user accessor properties for shared script classes with multiple globals. */ @Test public static void multiThreadedAccessorTest() 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(Object.prototype, 'foo', { get: function() 'original context' })", origContext); e.eval("Object.defineProperty(Object.prototype, 'foo', { get: function() 'new context', configurable: true })", 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 Object.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(); }
Example 7
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals. */ @Test public static void multiThreadedPrimitiveTest() 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 Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); assertEquals(obj1, "original context"); assertEquals(obj2, "new context"); 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(); Assert.assertEquals(e.eval(sharedScript), "original context"); Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); }
Example 8
Source File: Titan1Graph.java From incubator-atlas with Apache License 2.0 | 5 votes |
@Override public Object executeGremlinScript(ScriptEngine scriptEngine, Map<? extends String, ? extends Object> userBindings, String query, boolean isPath) throws ScriptException { Bindings bindings = scriptEngine.createBindings(); bindings.putAll(userBindings); bindings.put("g", getGraph()); Object result = scriptEngine.eval(query, bindings); return convertGremlinValue(result); }
Example 9
Source File: ScopeTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded scope function invocation for shared script classes with multiple globals. */ @Test public static void multiThreadedFunctionTest() 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(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); assertEquals(origContext.getAttribute("scopeVar"), 1); assertEquals(e.eval("scopeTest()"), 1); e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); assertEquals(newCtxt.getAttribute("scopeVar"), 1); assertEquals(e.eval("scopeTest();", newCtxt), 1); assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); assertEquals(newCtxt.getAttribute("scopeVar"), 3); final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); t1.start(); t2.start(); t1.join(); t2.join(); }
Example 10
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded scope function invocation for shared script classes with multiple globals. */ @Test public static void multiThreadedFunctionTest() 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(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); assertEquals(origContext.getAttribute("scopeVar"), 1); assertEquals(e.eval("scopeTest()"), 1); e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); assertEquals(newCtxt.getAttribute("scopeVar"), 1); assertEquals(e.eval("scopeTest();", newCtxt), 1); assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); assertEquals(newCtxt.getAttribute("scopeVar"), 3); final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); t1.start(); t2.start(); t1.join(); t2.join(); }
Example 11
Source File: ScopeTest.java From hottub 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 getterSetter2Test() 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 = "accessor2"; e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); assertEquals(e.eval("accessor2 = 1;"), 1); assertEquals(e.eval(sharedScript), 1); e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); assertEquals(e.eval("accessor2 = 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("x"), 1); assertEquals(e.eval("x", newCtxt), 2); }
Example 12
Source File: ScopeTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to defined global variables for shared script classes with multiple globals. */ @Test public static void multiThreadedVarTest() 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 = "foo"; assertEquals(e.eval("var foo = 'original context';", origContext), null); assertEquals(e.eval("var foo = 'new context';", newCtxt), null); 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(); assertEquals(e.eval("var foo = 'newer context';", newCtxt), null); 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(); assertEquals(e.eval(sharedScript), "original context"); assertEquals(e.eval(sharedScript, newCtxt), "newer context"); }
Example 13
Source File: ScopeTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Test multi-threaded access to defined global variables for shared script classes with multiple globals. */ @Test public static void multiThreadedVarTest() 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 = "foo"; assertEquals(e.eval("var foo = 'original context';", origContext), null); assertEquals(e.eval("var foo = 'new context';", newCtxt), null); 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(); assertEquals(e.eval("var foo = 'newer context';", newCtxt), null); 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(); assertEquals(e.eval(sharedScript), "original context"); assertEquals(e.eval(sharedScript, newCtxt), "newer context"); }
Example 14
Source File: ScopeTest.java From openjdk-jdk9 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(); }
Example 15
Source File: ScriptEngineSample.java From luaj with MIT License | 5 votes |
public static void testUserClasses(ScriptEngine e) throws ScriptException { CompiledScript cs = ((Compilable)e).compile( "test = test or luajava.newInstance(\"java.lang.String\", \"test\")\n" + "print( 'test', type(test), test, tostring(test) )\n" + "return tostring(test)"); Bindings b = e.createBindings(); Object resultstring = cs.eval(b); b.put("test", new SomeUserClass()); Object resultuserclass = cs.eval(b); System.out.println( "eval(string): "+resultstring.getClass()+" "+resultstring ); System.out.println( "eval(userclass): "+resultuserclass.getClass()+" "+resultuserclass ); }
Example 16
Source File: ScopeTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
@Test // check that engine.js definitions are visible in all new global instances public void checkBuiltinsInNewBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); // check default global instance has engine.js definitions final Bindings g = (Bindings) e.eval("this"); Object value = g.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = g.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // check new global instance created has engine.js definitions final Bindings b = e.createBindings(); value = b.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = b.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // put a mapping into GLOBAL_SCOPE final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); globalScope.put("x", "hello"); // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval assertTrue(e.eval("x").equals("hello")); final ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // try some arbitray Bindings for ENGINE_SCOPE final Bindings sb = new SimpleBindings(); ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // engine.js builtins are still defined even with arbitrary Bindings assertTrue(e.eval("typeof print", ctx).equals("function")); assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition sb.put("x", "newX"); assertTrue(e.eval("x", ctx).equals("newX")); }
Example 17
Source File: ScopeTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test public void multiGlobalTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); try { Object obj1 = e.eval("Object"); Object obj2 = e.eval("Object", newCtxt); Assert.assertNotEquals(obj1, obj2); Assert.assertNotNull(obj1); Assert.assertNotNull(obj2); Assert.assertEquals(obj1.toString(), obj2.toString()); e.eval("x = 'hello'"); e.eval("x = 'world'", newCtxt); Object x1 = e.getContext().getAttribute("x"); Object x2 = newCtxt.getAttribute("x"); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); x1 = e.eval("x"); x2 = e.eval("x", newCtxt); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); final ScriptContext origCtxt = e.getContext(); e.setContext(newCtxt); e.eval("y = new Object()"); e.eval("y = new Object()", origCtxt); Object y1 = origCtxt.getAttribute("y"); Object y2 = newCtxt.getAttribute("y"); Assert.assertNotEquals(y1, y2); Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt)); Assert.assertEquals("[object Object]", y1.toString()); Assert.assertEquals("[object Object]", y2.toString()); } catch (final ScriptException se) { se.printStackTrace(); fail(se.getMessage()); } }
Example 18
Source File: ScopeTest.java From jdk8u_nashorn with GNU General Public License v2.0 | 4 votes |
@Test // check that engine.js definitions are visible in all new global instances public void checkBuiltinsInNewBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); // check default global instance has engine.js definitions final Bindings g = (Bindings) e.eval("this"); Object value = g.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = g.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // check new global instance created has engine.js definitions final Bindings b = e.createBindings(); value = b.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = b.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // put a mapping into GLOBAL_SCOPE final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); globalScope.put("x", "hello"); // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval assertTrue(e.eval("x").equals("hello")); final ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // try some arbitray Bindings for ENGINE_SCOPE final Bindings sb = new SimpleBindings(); ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // engine.js builtins are still defined even with arbitrary Bindings assertTrue(e.eval("typeof print", ctx).equals("function")); assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition sb.put("x", "newX"); assertTrue(e.eval("x", ctx).equals("newX")); }
Example 19
Source File: ScopeTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test // check that engine.js definitions are visible in all new global instances public void checkBuiltinsInNewBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); // check default global instance has engine.js definitions final Bindings g = (Bindings) e.eval("this"); Object value = g.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = g.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // check new global instance created has engine.js definitions Bindings b = e.createBindings(); value = b.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = b.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // put a mapping into GLOBAL_SCOPE final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); globalScope.put("x", "hello"); // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval assertTrue(e.eval("x").equals("hello")); final ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // try some arbitray Bindings for ENGINE_SCOPE Bindings sb = new SimpleBindings(); ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // engine.js builtins are still defined even with arbitrary Bindings assertTrue(e.eval("typeof print", ctx).equals("function")); assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition sb.put("x", "newX"); assertTrue(e.eval("x", ctx).equals("newX")); }
Example 20
Source File: ScopeTest.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
@Test // check that engine.js definitions are visible in all new global instances public void checkBuiltinsInNewBindingsTest() throws ScriptException { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); // check default global instance has engine.js definitions final Bindings g = (Bindings) e.eval("this"); Object value = g.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = g.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // check new global instance created has engine.js definitions Bindings b = e.createBindings(); value = b.get("__noSuchProperty__"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); value = b.get("print"); assertTrue(value instanceof ScriptObjectMirror && ((ScriptObjectMirror)value).isFunction()); // put a mapping into GLOBAL_SCOPE final Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE); globalScope.put("x", "hello"); // GLOBAL_SCOPE mapping should be visible from default ScriptContext eval assertTrue(e.eval("x").equals("hello")); final ScriptContext ctx = new SimpleScriptContext(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // try some arbitray Bindings for ENGINE_SCOPE Bindings sb = new SimpleBindings(); ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval assertTrue(e.eval("x", ctx).equals("hello")); // engine.js builtins are still defined even with arbitrary Bindings assertTrue(e.eval("typeof print", ctx).equals("function")); assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition sb.put("x", "newX"); assertTrue(e.eval("x", ctx).equals("newX")); }