Skip to content

Commit 0c6e52c

Browse files
committed
Nashorn scope samples
1 parent 1c3eaf1 commit 0c6e52c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/com/winterbe/java8/samples/nashorn/Nashorn11.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,27 @@ public static void main(String[] args) throws Exception {
2121
// test4();
2222
// test5();
2323
// test6();
24-
test7();
24+
// test7();
25+
test8();
26+
}
27+
28+
private static void test8() throws ScriptException {
29+
NashornScriptEngine engine = createEngine();
30+
31+
engine.eval("var obj = { foo: 23 };");
32+
33+
ScriptContext defaultContext = engine.getContext();
34+
Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
35+
36+
SimpleScriptContext context1 = new SimpleScriptContext();
37+
context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
38+
39+
SimpleScriptContext context2 = new SimpleScriptContext();
40+
context2.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", defaultBindings.get("obj"));
41+
42+
engine.eval("obj.foo = 44;", context1);
43+
engine.eval("print(obj.foo);", context1);
44+
engine.eval("print(obj.foo);", context2);
2545
}
2646

2747
private static void test7() throws ScriptException {
@@ -118,7 +138,10 @@ private static void test3() throws ScriptException {
118138
private static void test2() throws ScriptException {
119139
NashornScriptEngine engine = createEngine();
120140
engine.eval("function foo() { print('bar') };");
121-
engine.eval("foo();", new SimpleScriptContext());
141+
142+
SimpleScriptContext context = new SimpleScriptContext();
143+
engine.eval("print(Function);", context);
144+
engine.eval("foo();", context);
122145
}
123146

124147
private static void test1() throws ScriptException {

0 commit comments

Comments
 (0)