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‎

Lines changed: 25 additions & 2 deletions
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+
privatestaticvoidtest8() throwsScriptException{
29+
NashornScriptEngineengine = createEngine();
30+
31+
engine.eval("var obj ={foo: 23 };");
32+
33+
ScriptContextdefaultContext = engine.getContext();
34+
BindingsdefaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE);
35+
36+
SimpleScriptContextcontext1 = newSimpleScriptContext();
37+
context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE);
38+
39+
SimpleScriptContextcontext2 = newSimpleScriptContext();
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
privatestaticvoidtest7() throwsScriptException{
@@ -118,7 +138,10 @@ private static void test3() throws ScriptException{
118138
privatestaticvoidtest2() throwsScriptException{
119139
NashornScriptEngineengine = createEngine();
120140
engine.eval("function foo(){print('bar') };");
121-
engine.eval("foo();", newSimpleScriptContext());
141+
142+
SimpleScriptContextcontext = newSimpleScriptContext();
143+
engine.eval("print(Function);", context);
144+
engine.eval("foo();", context);
122145
}
123146

124147
privatestaticvoidtest1() throwsScriptException{

0 commit comments

Comments
(0)