| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,134 @@ | |||
| 1 | + package com.winterbe.java8.samples.nashorn; | ||
| 2 | + | ||
| 3 | + import jdk.nashorn.api.scripting.NashornScriptEngine; | ||
| 4 | + | ||
| 5 | + import javax.script.Bindings; | ||
| 6 | + import javax.script.ScriptContext; | ||
| 7 | + import javax.script.ScriptEngineManager; | ||
| 8 | + import javax.script.ScriptException; | ||
| 9 | + import javax.script.SimpleBindings; | ||
| 10 | + import javax.script.SimpleScriptContext; | ||
| 11 | + | ||
| 12 | + /** | ||
| 13 | + * @author Benjamin Winterberg | ||
| 14 | + */ | ||
| 15 | + public class Nashorn11 { | ||
| 16 | + | ||
| 17 | + public static void main(String[] args) throws Exception { | ||
| 18 | + // test1(); | ||
| 19 | + // test2(); | ||
| 20 | + // test3(); | ||
| 21 | + // test4(); | ||
| 22 | + // test5(); | ||
| 23 | + // test6(); | ||
| 24 | + test7(); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + private static void test7() throws ScriptException { | ||
| 28 | + NashornScriptEngine engine = createEngine(); | ||
| 29 | + | ||
| 30 | + engine.eval("var foo = 23;"); | ||
| 31 | + | ||
| 32 | + ScriptContext defaultContext = engine.getContext(); | ||
| 33 | + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 34 | + | ||
| 35 | + SimpleScriptContext context1 = new SimpleScriptContext(); | ||
| 36 | + context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); | ||
| 37 | + | ||
| 38 | + SimpleScriptContext context2 = new SimpleScriptContext(); | ||
| 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 | + private static void test6() throws ScriptException { | ||
| 47 | + NashornScriptEngine engine = createEngine(); | ||
| 48 | + | ||
| 49 | + ScriptContext defaultContext = engine.getContext(); | ||
| 50 | + defaultContext.getBindings(ScriptContext.GLOBAL_SCOPE).put("foo", "hello"); | ||
| 51 | + | ||
| 52 | + ScriptContext customContext = new SimpleScriptContext(); | ||
| 53 | + customContext.setBindings(defaultContext.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.ENGINE_SCOPE); | ||
| 54 | + | ||
| 55 | + Bindings bindings = new SimpleBindings(); | ||
| 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 | + private static void test5() throws ScriptException { | ||
| 67 | + NashornScriptEngine engine = createEngine(); | ||
| 68 | + | ||
| 69 | + engine.eval("var obj = { foo: 'foo' };"); | ||
| 70 | + engine.eval("function printFoo() { print(obj.foo) };"); | ||
| 71 | + | ||
| 72 | + ScriptContext defaultContext = engine.getContext(); | ||
| 73 | + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 74 | + | ||
| 75 | + SimpleScriptContext context1 = new SimpleScriptContext(); | ||
| 76 | + context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); | ||
| 77 | + | ||
| 78 | + SimpleScriptContext context2 = new SimpleScriptContext(); | ||
| 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 | + private static void test4() throws ScriptException { | ||
| 87 | + NashornScriptEngine engine = createEngine(); | ||
| 88 | + | ||
| 89 | + engine.eval("function foo() { print('bar') };"); | ||
| 90 | + | ||
| 91 | + ScriptContext defaultContext = engine.getContext(); | ||
| 92 | + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 93 | + | ||
| 94 | + SimpleScriptContext context = new SimpleScriptContext(); | ||
| 95 | + context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); | ||
| 96 | + | ||
| 97 | + engine.eval("foo();", context); | ||
| 98 | + System.out.println(context.getAttribute("foo")); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + private static void test3() throws ScriptException { | ||
| 102 | + NashornScriptEngine engine = createEngine(); | ||
| 103 | + | ||
| 104 | + ScriptContext defaultContext = engine.getContext(); | ||
| 105 | + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 106 | + | ||
| 107 | + SimpleScriptContext context = new SimpleScriptContext(); | ||
| 108 | + context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); | ||
| 109 | + | ||
| 110 | + engine.eval("function foo() { print('bar') };", context); | ||
| 111 | + engine.eval("foo();", context); | ||
| 112 | + | ||
| 113 | + Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 114 | + System.out.println(bindings.get("foo")); | ||
| 115 | + System.out.println(context.getAttribute("foo")); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + private static void test2() throws ScriptException { | ||
| 119 | + NashornScriptEngine engine = createEngine(); | ||
| 120 | + engine.eval("function foo() { print('bar') };"); | ||
| 121 | + engine.eval("foo();", new SimpleScriptContext()); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + private static void test1() throws ScriptException { | ||
| 125 | + NashornScriptEngine engine = createEngine(); | ||
| 126 | + engine.eval("function foo() { print('bar') };"); | ||
| 127 | + engine.eval("foo();"); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + private static NashornScriptEngine createEngine() { | ||
| 131 | + return (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments