Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
吕明琪
java8-tutorial
提交
1c3eaf14
J
java8-tutorial
项目概览
吕明琪
/
java8-tutorial
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java8-tutorial
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1c3eaf14
编写于
7月 25, 2015
作者:
B
Benjamin Winterberg
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Nashorn scope samples
上级
4fa2b2b6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
134 addition
and
0 deletion
+134
-0
src/com/winterbe/java8/samples/nashorn/Nashorn11.java
src/com/winterbe/java8/samples/nashorn/Nashorn11.java
+134
-0
未找到文件。
src/com/winterbe/java8/samples/nashorn/Nashorn11.java
0 → 100644
浏览文件 @
1c3eaf14
package
com.winterbe.java8.samples.nashorn
;
import
jdk.nashorn.api.scripting.NashornScriptEngine
;
import
javax.script.Bindings
;
import
javax.script.ScriptContext
;
import
javax.script.ScriptEngineManager
;
import
javax.script.ScriptException
;
import
javax.script.SimpleBindings
;
import
javax.script.SimpleScriptContext
;
/**
* @author Benjamin Winterberg
*/
public
class
Nashorn11
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// test1();
// test2();
// test3();
// test4();
// test5();
// test6();
test7
();
}
private
static
void
test7
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
engine
.
eval
(
"var foo = 23;"
);
ScriptContext
defaultContext
=
engine
.
getContext
();
Bindings
defaultBindings
=
defaultContext
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
);
SimpleScriptContext
context1
=
new
SimpleScriptContext
();
context1
.
setBindings
(
defaultBindings
,
ScriptContext
.
ENGINE_SCOPE
);
SimpleScriptContext
context2
=
new
SimpleScriptContext
();
context2
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
).
put
(
"foo"
,
defaultBindings
.
get
(
"foo"
));
engine
.
eval
(
"foo = 44;"
,
context1
);
engine
.
eval
(
"print(foo);"
,
context1
);
engine
.
eval
(
"print(foo);"
,
context2
);
}
private
static
void
test6
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
ScriptContext
defaultContext
=
engine
.
getContext
();
defaultContext
.
getBindings
(
ScriptContext
.
GLOBAL_SCOPE
).
put
(
"foo"
,
"hello"
);
ScriptContext
customContext
=
new
SimpleScriptContext
();
customContext
.
setBindings
(
defaultContext
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
),
ScriptContext
.
ENGINE_SCOPE
);
Bindings
bindings
=
new
SimpleBindings
();
bindings
.
put
(
"foo"
,
"world"
);
customContext
.
setBindings
(
bindings
,
ScriptContext
.
GLOBAL_SCOPE
);
// engine.eval("foo = 23;"); // overrides foo in all contexts, why???
engine
.
eval
(
"print(foo)"
);
// hello
engine
.
eval
(
"print(foo)"
,
customContext
);
// world
engine
.
eval
(
"print(foo)"
,
defaultContext
);
// hello
}
private
static
void
test5
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
engine
.
eval
(
"var obj = { foo: 'foo' };"
);
engine
.
eval
(
"function printFoo() { print(obj.foo) };"
);
ScriptContext
defaultContext
=
engine
.
getContext
();
Bindings
defaultBindings
=
defaultContext
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
);
SimpleScriptContext
context1
=
new
SimpleScriptContext
();
context1
.
setBindings
(
defaultBindings
,
ScriptContext
.
ENGINE_SCOPE
);
SimpleScriptContext
context2
=
new
SimpleScriptContext
();
context2
.
setBindings
(
defaultBindings
,
ScriptContext
.
ENGINE_SCOPE
);
engine
.
eval
(
"obj.foo = 'bar';"
,
context1
);
engine
.
eval
(
"printFoo();"
,
context1
);
engine
.
eval
(
"printFoo();"
,
context2
);
}
private
static
void
test4
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
engine
.
eval
(
"function foo() { print('bar') };"
);
ScriptContext
defaultContext
=
engine
.
getContext
();
Bindings
defaultBindings
=
defaultContext
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
);
SimpleScriptContext
context
=
new
SimpleScriptContext
();
context
.
setBindings
(
defaultBindings
,
ScriptContext
.
ENGINE_SCOPE
);
engine
.
eval
(
"foo();"
,
context
);
System
.
out
.
println
(
context
.
getAttribute
(
"foo"
));
}
private
static
void
test3
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
ScriptContext
defaultContext
=
engine
.
getContext
();
Bindings
defaultBindings
=
defaultContext
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
);
SimpleScriptContext
context
=
new
SimpleScriptContext
();
context
.
setBindings
(
defaultBindings
,
ScriptContext
.
ENGINE_SCOPE
);
engine
.
eval
(
"function foo() { print('bar') };"
,
context
);
engine
.
eval
(
"foo();"
,
context
);
Bindings
bindings
=
context
.
getBindings
(
ScriptContext
.
ENGINE_SCOPE
);
System
.
out
.
println
(
bindings
.
get
(
"foo"
));
System
.
out
.
println
(
context
.
getAttribute
(
"foo"
));
}
private
static
void
test2
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
engine
.
eval
(
"function foo() { print('bar') };"
);
engine
.
eval
(
"foo();"
,
new
SimpleScriptContext
());
}
private
static
void
test1
()
throws
ScriptException
{
NashornScriptEngine
engine
=
createEngine
();
engine
.
eval
(
"function foo() { print('bar') };"
);
engine
.
eval
(
"foo();"
);
}
private
static
NashornScriptEngine
createEngine
()
{
return
(
NashornScriptEngine
)
new
ScriptEngineManager
().
getEngineByName
(
"nashorn"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录