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