test/src/jdk/nashorn/api/scripting/ScopeTest.java

Print this page




 493 
 494         e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
 495         assertEquals(e.eval("accessor2 = 2;", newCtxt), 2);
 496         assertEquals(e.eval(sharedScript, newCtxt), 2);
 497 
 498 
 499         final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
 500         final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));
 501 
 502         t1.start();
 503         t2.start();
 504         t1.join();
 505         t2.join();
 506 
 507         assertEquals(e.eval(sharedScript), 1);
 508         assertEquals(e.eval(sharedScript, newCtxt), 2);
 509         assertEquals(e.eval("x"), 1);
 510         assertEquals(e.eval("x", newCtxt), 2);
 511     }
 512 

















 513     /**
 514      * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals.
 515      */
 516     @Test
 517     public static void testSlowScope() throws ScriptException, InterruptedException {
 518         final ScriptEngineManager m = new ScriptEngineManager();
 519         final ScriptEngine e = m.getEngineByName("nashorn");
 520 
 521         for (int i = 0; i < 100; i++) {
 522             final Bindings b = e.createBindings();
 523             final ScriptContext ctxt = new SimpleScriptContext();
 524             ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
 525 
 526             e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt);
 527             assertEquals(e.eval("a", ctxt), 1);
 528             assertEquals(b.get("a"), 1);
 529             assertEquals(e.eval("b", ctxt), 3);
 530             assertEquals(b.get("b"), 3);
 531             assertEquals(e.eval("c", ctxt), 10);
 532             assertEquals(b.get("c"), 10);




 493 
 494         e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt);
 495         assertEquals(e.eval("accessor2 = 2;", newCtxt), 2);
 496         assertEquals(e.eval(sharedScript, newCtxt), 2);
 497 
 498 
 499         final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000));
 500         final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000));
 501 
 502         t1.start();
 503         t2.start();
 504         t1.join();
 505         t2.join();
 506 
 507         assertEquals(e.eval(sharedScript), 1);
 508         assertEquals(e.eval(sharedScript, newCtxt), 2);
 509         assertEquals(e.eval("x"), 1);
 510         assertEquals(e.eval("x", newCtxt), 2);
 511     }
 512 
 513     // @bug 8044750: megamorphic getter for scope objects does not call __noSuchProperty__ hook
 514     @Test
 515     public static void testMegamorphicGetInGlobal() throws Exception {
 516         final ScriptEngineManager m = new ScriptEngineManager();
 517         final ScriptEngine engine = m.getEngineByName("nashorn");
 518         final String script = "foo";
 519         // "foo" is megamorphic because of different global scopes.
 520         // Make sure ScriptContext variable search works even after
 521         // it becomes megamorphic.
 522         for (int index = 0; index < 25; index++) {
 523             final Bindings bindings = new SimpleBindings();
 524             bindings.put("foo", index);
 525             final Number value = (Number)engine.eval(script, bindings);
 526             assertEquals(index, value.intValue());
 527         }
 528     }
 529 
 530     /**
 531      * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals.
 532      */
 533     @Test
 534     public static void testSlowScope() throws ScriptException, InterruptedException {
 535         final ScriptEngineManager m = new ScriptEngineManager();
 536         final ScriptEngine e = m.getEngineByName("nashorn");
 537 
 538         for (int i = 0; i < 100; i++) {
 539             final Bindings b = e.createBindings();
 540             final ScriptContext ctxt = new SimpleScriptContext();
 541             ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
 542 
 543             e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt);
 544             assertEquals(e.eval("a", ctxt), 1);
 545             assertEquals(b.get("a"), 1);
 546             assertEquals(e.eval("b", ctxt), 3);
 547             assertEquals(b.get("b"), 3);
 548             assertEquals(e.eval("c", ctxt), 10);
 549             assertEquals(b.get("c"), 10);