|
| 1 | +packagecom.winterbe.java8.samples.nashorn; |
| 2 | + |
| 3 | +importjdk.nashorn.api.scripting.NashornScriptEngine; |
| 4 | + |
| 5 | +importjavax.script.Bindings; |
| 6 | +importjavax.script.ScriptContext; |
| 7 | +importjavax.script.ScriptEngineManager; |
| 8 | +importjavax.script.ScriptException; |
| 9 | +importjavax.script.SimpleBindings; |
| 10 | +importjavax.script.SimpleScriptContext; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Benjamin Winterberg |
| 14 | + */ |
| 15 | +publicclassNashorn11 { |
| 16 | + |
| 17 | +publicstaticvoidmain(String[]args)throwsException { |
| 18 | +// test1(); |
| 19 | +// test2(); |
| 20 | +// test3(); |
| 21 | +// test4(); |
| 22 | +// test5(); |
| 23 | +// test6(); |
| 24 | +test7(); |
| 25 | + } |
| 26 | + |
| 27 | +privatestaticvoidtest7()throwsScriptException { |
| 28 | +NashornScriptEngineengine =createEngine(); |
| 29 | + |
| 30 | +engine.eval("var foo = 23;"); |
| 31 | + |
| 32 | +ScriptContextdefaultContext =engine.getContext(); |
| 33 | +BindingsdefaultBindings =defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); |
| 34 | + |
| 35 | +SimpleScriptContextcontext1 =newSimpleScriptContext(); |
| 36 | +context1.setBindings(defaultBindings,ScriptContext.ENGINE_SCOPE); |
| 37 | + |
| 38 | +SimpleScriptContextcontext2 =newSimpleScriptContext(); |
| 39 | +context2.getBindings(ScriptContext.ENGINE_SCOPE).put("foo",defaultBindings.get("foo")); |
| 40 | + |
| 41 | +engine.eval("foo = 44;",context1); |
| 42 | +engine.eval("print(foo);",context1); |
| 43 | +engine.eval("print(foo);",context2); |
| 44 | + } |
| 45 | + |
| 46 | +privatestaticvoidtest6()throwsScriptException { |
| 47 | +NashornScriptEngineengine =createEngine(); |
| 48 | + |
| 49 | +ScriptContextdefaultContext =engine.getContext(); |
| 50 | +defaultContext.getBindings(ScriptContext.GLOBAL_SCOPE).put("foo","hello"); |
| 51 | + |
| 52 | +ScriptContextcustomContext =newSimpleScriptContext(); |
| 53 | +customContext.setBindings(defaultContext.getBindings(ScriptContext.ENGINE_SCOPE),ScriptContext.ENGINE_SCOPE); |
| 54 | + |
| 55 | +Bindingsbindings =newSimpleBindings(); |
| 56 | +bindings.put("foo","world"); |
| 57 | +customContext.setBindings(bindings,ScriptContext.GLOBAL_SCOPE); |
| 58 | + |
| 59 | +// engine.eval("foo = 23;"); // overrides foo in all contexts, why??? |
| 60 | + |
| 61 | +engine.eval("print(foo)");// hello |
| 62 | +engine.eval("print(foo)",customContext);// world |
| 63 | +engine.eval("print(foo)",defaultContext);// hello |
| 64 | + } |
| 65 | + |
| 66 | +privatestaticvoidtest5()throwsScriptException { |
| 67 | +NashornScriptEngineengine =createEngine(); |
| 68 | + |
| 69 | +engine.eval("var obj = { foo: 'foo' };"); |
| 70 | +engine.eval("function printFoo() { print(obj.foo) };"); |
| 71 | + |
| 72 | +ScriptContextdefaultContext =engine.getContext(); |
| 73 | +BindingsdefaultBindings =defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); |
| 74 | + |
| 75 | +SimpleScriptContextcontext1 =newSimpleScriptContext(); |
| 76 | +context1.setBindings(defaultBindings,ScriptContext.ENGINE_SCOPE); |
| 77 | + |
| 78 | +SimpleScriptContextcontext2 =newSimpleScriptContext(); |
| 79 | +context2.setBindings(defaultBindings,ScriptContext.ENGINE_SCOPE); |
| 80 | + |
| 81 | +engine.eval("obj.foo = 'bar';",context1); |
| 82 | +engine.eval("printFoo();",context1); |
| 83 | +engine.eval("printFoo();",context2); |
| 84 | + } |
| 85 | + |
| 86 | +privatestaticvoidtest4()throwsScriptException { |
| 87 | +NashornScriptEngineengine =createEngine(); |
| 88 | + |
| 89 | +engine.eval("function foo() { print('bar') };"); |
| 90 | + |
| 91 | +ScriptContextdefaultContext =engine.getContext(); |
| 92 | +BindingsdefaultBindings =defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); |
| 93 | + |
| 94 | +SimpleScriptContextcontext =newSimpleScriptContext(); |
| 95 | +context.setBindings(defaultBindings,ScriptContext.ENGINE_SCOPE); |
| 96 | + |
| 97 | +engine.eval("foo();",context); |
| 98 | +System.out.println(context.getAttribute("foo")); |
| 99 | + } |
| 100 | + |
| 101 | +privatestaticvoidtest3()throwsScriptException { |
| 102 | +NashornScriptEngineengine =createEngine(); |
| 103 | + |
| 104 | +ScriptContextdefaultContext =engine.getContext(); |
| 105 | +BindingsdefaultBindings =defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); |
| 106 | + |
| 107 | +SimpleScriptContextcontext =newSimpleScriptContext(); |
| 108 | +context.setBindings(defaultBindings,ScriptContext.ENGINE_SCOPE); |
| 109 | + |
| 110 | +engine.eval("function foo() { print('bar') };",context); |
| 111 | +engine.eval("foo();",context); |
| 112 | + |
| 113 | +Bindingsbindings =context.getBindings(ScriptContext.ENGINE_SCOPE); |
| 114 | +System.out.println(bindings.get("foo")); |
| 115 | +System.out.println(context.getAttribute("foo")); |
| 116 | + } |
| 117 | + |
| 118 | +privatestaticvoidtest2()throwsScriptException { |
| 119 | +NashornScriptEngineengine =createEngine(); |
| 120 | +engine.eval("function foo() { print('bar') };"); |
| 121 | +engine.eval("foo();",newSimpleScriptContext()); |
| 122 | + } |
| 123 | + |
| 124 | +privatestaticvoidtest1()throwsScriptException { |
| 125 | +NashornScriptEngineengine =createEngine(); |
| 126 | +engine.eval("function foo() { print('bar') };"); |
| 127 | +engine.eval("foo();"); |
| 128 | + } |
| 129 | + |
| 130 | +privatestaticNashornScriptEnginecreateEngine() { |
| 131 | +return (NashornScriptEngine)newScriptEngineManager().getEngineByName("nashorn"); |
| 132 | + } |
| 133 | + |
| 134 | +} |