228 ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); 229 230 // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 231 assertTrue(e.eval("x", ctx).equals("hello")); 232 233 // try some arbitray Bindings for ENGINE_SCOPE 234 Bindings sb = new SimpleBindings(); 235 ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); 236 237 // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 238 assertTrue(e.eval("x", ctx).equals("hello")); 239 240 // engine.js builtins are still defined even with arbitrary Bindings 241 assertTrue(e.eval("typeof print", ctx).equals("function")); 242 assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); 243 244 // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition 245 sb.put("x", "newX"); 246 assertTrue(e.eval("x", ctx).equals("newX")); 247 } 248 } | 228 ctx.setBindings(b, ScriptContext.ENGINE_SCOPE); 229 230 // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 231 assertTrue(e.eval("x", ctx).equals("hello")); 232 233 // try some arbitray Bindings for ENGINE_SCOPE 234 Bindings sb = new SimpleBindings(); 235 ctx.setBindings(sb, ScriptContext.ENGINE_SCOPE); 236 237 // GLOBAL_SCOPE mapping should be visible from non-default ScriptContext eval 238 assertTrue(e.eval("x", ctx).equals("hello")); 239 240 // engine.js builtins are still defined even with arbitrary Bindings 241 assertTrue(e.eval("typeof print", ctx).equals("function")); 242 assertTrue(e.eval("typeof __noSuchProperty__", ctx).equals("function")); 243 244 // ENGINE_SCOPE definition should 'hide' GLOBAL_SCOPE definition 245 sb.put("x", "newX"); 246 assertTrue(e.eval("x", ctx).equals("newX")); 247 } 248 249 @Test 250 public static void multiThreadedVarTest() throws ScriptException, InterruptedException { 251 final ScriptEngineManager m = new ScriptEngineManager(); 252 final ScriptEngine e = m.getEngineByName("nashorn"); 253 final Bindings b = e.createBindings(); 254 final ScriptContext origContext = e.getContext(); 255 final ScriptContext newCtxt = new SimpleScriptContext(); 256 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 257 final String sharedScript = "foo"; 258 259 assertEquals(e.eval("var foo = 'original context';", origContext), null); 260 assertEquals(e.eval("var foo = 'new context';", newCtxt), null); 261 262 final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 263 final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 264 t1.start(); 265 t2.start(); 266 t1.join(); 267 t2.join(); 268 269 assertEquals(e.eval("var foo = 'newer context';", newCtxt), null); 270 final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 271 final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 272 273 t3.start(); 274 t4.start(); 275 t3.join(); 276 t4.join(); 277 278 assertEquals(e.eval(sharedScript), "original context"); 279 assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 280 } 281 282 @Test 283 public static void multiThreadedGlobalTest() throws ScriptException, InterruptedException { 284 final ScriptEngineManager m = new ScriptEngineManager(); 285 final ScriptEngine e = m.getEngineByName("nashorn"); 286 final Bindings b = e.createBindings(); 287 final ScriptContext origContext = e.getContext(); 288 final ScriptContext newCtxt = new SimpleScriptContext(); 289 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 290 291 assertEquals(e.eval("foo = 'original context';", origContext), "original context"); 292 assertEquals(e.eval("foo = 'new context';", newCtxt), "new context"); 293 final String sharedScript = "foo"; 294 295 final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 296 final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 297 t1.start(); 298 t2.start(); 299 t1.join(); 300 t2.join(); 301 302 Object obj3 = e.eval("delete foo; foo = 'newer context';", newCtxt); 303 assertEquals(obj3, "newer context"); 304 final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 305 final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 306 307 t3.start(); 308 t4.start(); 309 t3.join(); 310 t4.join(); 311 312 Assert.assertEquals(e.eval(sharedScript), "original context"); 313 Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 314 } 315 316 @Test 317 public static void multiThreadedIncTest() throws ScriptException, InterruptedException { 318 final ScriptEngineManager m = new ScriptEngineManager(); 319 final ScriptEngine e = m.getEngineByName("nashorn"); 320 final Bindings b = e.createBindings(); 321 final ScriptContext origContext = e.getContext(); 322 final ScriptContext newCtxt = new SimpleScriptContext(); 323 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 324 325 assertEquals(e.eval("var x = 0;", origContext), null); 326 assertEquals(e.eval("var x = 2;", newCtxt), null); 327 final String sharedScript = "x++;"; 328 329 final Thread t1 = new Thread(new Runnable() { 330 @Override 331 public void run() { 332 try { 333 for (int i = 0; i < 1000; i++) { 334 assertEquals(e.eval(sharedScript, origContext), (double)i); 335 } 336 } catch (ScriptException se) { 337 fail(se.toString()); 338 } 339 } 340 }); 341 final Thread t2 = new Thread(new Runnable() { 342 @Override 343 public void run() { 344 try { 345 for (int i = 2; i < 1000; i++) { 346 assertEquals(e.eval(sharedScript, newCtxt), (double)i); 347 } 348 } catch (ScriptException se) { 349 fail(se.toString()); 350 } 351 } 352 }); 353 t1.start(); 354 t2.start(); 355 t1.join(); 356 t2.join(); 357 } 358 359 @Test 360 public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException { 361 final ScriptEngineManager m = new ScriptEngineManager(); 362 final ScriptEngine e = m.getEngineByName("nashorn"); 363 final Bindings b = e.createBindings(); 364 final ScriptContext origContext = e.getContext(); 365 final ScriptContext newCtxt = new SimpleScriptContext(); 366 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 367 368 Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext); 369 Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt); 370 assertEquals(obj1, "original context"); 371 assertEquals(obj2, "new context"); 372 final String sharedScript = "''.foo"; 373 374 final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 375 final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000)); 376 t1.start(); 377 t2.start(); 378 t1.join(); 379 t2.join(); 380 381 Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt); 382 assertEquals(obj3, "newer context"); 383 final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000)); 384 final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000)); 385 386 t3.start(); 387 t4.start(); 388 t3.join(); 389 t4.join(); 390 391 Assert.assertEquals(e.eval(sharedScript), "original context"); 392 Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context"); 393 } 394 395 @Test 396 public static void multiThreadedFunctionTest() throws ScriptException, InterruptedException { 397 final ScriptEngineManager m = new ScriptEngineManager(); 398 final ScriptEngine e = m.getEngineByName("nashorn"); 399 final Bindings b = e.createBindings(); 400 final ScriptContext origContext = e.getContext(); 401 final ScriptContext newCtxt = new SimpleScriptContext(); 402 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 403 404 e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), origContext); 405 assertEquals(origContext.getAttribute("scopeVar"), 1); 406 assertEquals(e.eval("scopeTest()"), 1); 407 408 e.eval(new URLReader(ScopeTest.class.getResource("resources/func.js")), newCtxt); 409 assertEquals(newCtxt.getAttribute("scopeVar"), 1); 410 assertEquals(e.eval("scopeTest();", newCtxt), 1); 411 412 assertEquals(e.eval("scopeVar = 3;", newCtxt), 3); 413 assertEquals(newCtxt.getAttribute("scopeVar"), 3); 414 415 416 final Thread t1 = new Thread(new ScriptRunner(e, origContext, "scopeTest()", 1, 1000)); 417 final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, "scopeTest()", 3, 1000)); 418 419 t1.start(); 420 t2.start(); 421 t1.join(); 422 t2.join(); 423 424 } 425 426 @Test 427 public static void getterSetterTest() throws ScriptException, InterruptedException { 428 final ScriptEngineManager m = new ScriptEngineManager(); 429 final ScriptEngine e = m.getEngineByName("nashorn"); 430 final Bindings b = e.createBindings(); 431 final ScriptContext origContext = e.getContext(); 432 final ScriptContext newCtxt = new SimpleScriptContext(); 433 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 434 final String sharedScript = "accessor1"; 435 436 e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); 437 assertEquals(e.eval("accessor1 = 1;"), 1); 438 assertEquals(e.eval(sharedScript), 1); 439 440 e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); 441 assertEquals(e.eval("accessor1 = 2;", newCtxt), 2); 442 assertEquals(e.eval(sharedScript, newCtxt), 2); 443 444 445 final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); 446 final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); 447 448 t1.start(); 449 t2.start(); 450 t1.join(); 451 t2.join(); 452 453 assertEquals(e.eval(sharedScript), 1); 454 assertEquals(e.eval(sharedScript, newCtxt), 2); 455 assertEquals(e.eval("v"), 1); 456 assertEquals(e.eval("v", newCtxt), 2); 457 } 458 459 @Test 460 public static void getterSetter2Test() throws ScriptException, InterruptedException { 461 final ScriptEngineManager m = new ScriptEngineManager(); 462 final ScriptEngine e = m.getEngineByName("nashorn"); 463 final Bindings b = e.createBindings(); 464 final ScriptContext origContext = e.getContext(); 465 final ScriptContext newCtxt = new SimpleScriptContext(); 466 newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 467 final String sharedScript = "accessor2"; 468 469 e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), origContext); 470 assertEquals(e.eval("accessor2 = 1;"), 1); 471 assertEquals(e.eval(sharedScript), 1); 472 473 e.eval(new URLReader(ScopeTest.class.getResource("resources/gettersetter.js")), newCtxt); 474 assertEquals(e.eval("accessor2 = 2;", newCtxt), 2); 475 assertEquals(e.eval(sharedScript, newCtxt), 2); 476 477 478 final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, 1, 1000)); 479 final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, 2, 1000)); 480 481 t1.start(); 482 t2.start(); 483 t1.join(); 484 t2.join(); 485 486 assertEquals(e.eval(sharedScript), 1); 487 assertEquals(e.eval(sharedScript, newCtxt), 2); 488 assertEquals(e.eval("x"), 1); 489 assertEquals(e.eval("x", newCtxt), 2); 490 } 491 492 @Test 493 public static void testSlowScope() throws ScriptException, InterruptedException { 494 final ScriptEngineManager m = new ScriptEngineManager(); 495 final ScriptEngine e = m.getEngineByName("nashorn"); 496 497 for (int i = 0; i < 100; i++) { 498 final Bindings b = e.createBindings(); 499 final ScriptContext ctxt = new SimpleScriptContext(); 500 ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE); 501 502 e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt); 503 assertEquals(e.eval("a", ctxt), 1); 504 assertEquals(b.get("a"), 1); 505 assertEquals(e.eval("b", ctxt), 3); 506 assertEquals(b.get("b"), 3); 507 assertEquals(e.eval("c", ctxt), 10); 508 assertEquals(b.get("c"), 10); 509 } 510 } 511 512 private static class ScriptRunner implements Runnable { 513 514 final ScriptEngine engine; 515 final ScriptContext context; 516 final String source; 517 final Object expected; 518 final int iterations; 519 520 ScriptRunner(final ScriptEngine engine, final ScriptContext context, final String source, final Object expected, final int iterations) { 521 this.engine = engine; 522 this.context = context; 523 this.source = source; 524 this.expected = expected; 525 this.iterations = iterations; 526 } 527 528 @Override 529 public void run() { 530 try { 531 for (int i = 0; i < iterations; i++) { 532 assertEquals(engine.eval(source, context), expected); 533 } 534 } catch (ScriptException se) { 535 throw new RuntimeException(se); 536 } 537 } 538 } 539 540 } |