Nashorn in	the	Future
Oracle Corporation Japan
Fusion Middleware Business Unit
NISHIKAWA, Akihiro
8, April, 2015
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Safe	Harbor	Statement
The	following	is	intended	to	outline	our	general	product	direction.	It	is	intended	for	
information	purposes	only,	and	may	not	be	incorporated	into	any	contract.	It	is	not	a	
commitment	to	deliver	any	material,	code,	or	functionality,	and	should	not	be	relied	upon	
in	making	purchasing	decisions.	The	development,	release,	and	timing	of	any	features	or	
functionality	described	for	Oracle’s	products	remains	at	the	sole	discretion	of	Oracle.
2
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Agenda
What's	Nashorn?
8u20
8u40
In	the	future...
1
2
3
4
3
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
What’s	Nashorn?
4
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Nashorn
• Replacement	for	Rhino
– For	improving	security	and	performance
• Proof	of	Concept	for	InvokeDynamic (JSR-292)
5
JavaScript	Engine	introduced	in	Java	8 (JEP	174)
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Nashorn
• ECMAScript-262	Edition	5.1
• javax.script (JSR	223)	API
• Interaction	between	Java	and	JavaScript
• Command	line	tool	:	jjs
JavaScript	Engine	introduced	in	Java	8 (JEP	174)
6
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Get	started!
• jjs
– Hello	World
– Lambda	Expression,	Stream
• Scripting
– User	the	option	of	“-scripting”
– Call	Web	API
• JavaFX
– User	the	option	of	“–fx”
– WebView
7
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Lambda Expression	in	Nashorn
// Java
array.stream().sorted(Comparator.naturalOrder())
.forEach( t -> sortedArray.add(t) );
// Nashorn
array.stream().sorted(Comparator.naturalOrder())
.forEach(function(t) sortedArray.add(t));
8
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Call	from	Java	(1/3)
ScriptEngineManager manager =
new ScriptEngineManager();
ScriptEngine engine =
manager.getEngineByName("nashorn");
// Evaluation
engine.eval("print('hello world')"); // hello world
engine.eval(new FileReader(“hello.js”)); // hello.js
9
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Call	from	Java	(2/3)
engine.eval("function hello(name){
print('Hello, ' + name); }");
Invocable inv=(Invocable)engine;
// Hello, Taro
Object obj= inv.invokeFunction("hello","Taro");
10
Invoke	JavaScript	function
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Call	from	Java	(3/3)
engine.eval("function run(){
print('run() called');
}");
Invocable inv =(Invocable)engine;
Runnable r=inv.getInterface(Runnable.class);
Thread th=new Threads(r);
th.start();
th.join();
11
Implement	interface	with	Script	function
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
8u20
12
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
8u20
• Focused	on	security	improvement	and	enhancement	of	underlying	
platform
– JIT
– JDK
13
Released	in	August,	2014
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Replace	“const”	with	“var”
• Default:	false
• It	might	retire	in	the	future	since	8u40	supports	const.
14
--const-as-var=true|false
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Forbid	access	Java	Packages	and	Classes	from	JavaScript
• Default	:	false
15
--no-java=true|false (-nj)
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Forbid	using	syntax	against	ECMAScript	standard
• Able	to	use	API	Extension	such	as	Java.type
• Disabled	when	using	-scripting
16
--no-syntax-extensions (-nse)
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
#sourceURL and @sourceURL
• For	debuging use
– //# sourceURL=myScript.js
– @sourceURL=myScript.js
17
JDK-8032068	:	implement	@sourceURL and	#sourceURL directives
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Class	Cache
• Originally	compilation	is	required	even	if	invoking	previous	used	script.
• Since	8u20,	previous	evaluated	script	is	stored	in	cache	for	reuse.
• Example
– from	Java
engine.eval(new URLReader(myScriptURL));
– from	JavaScript
load(url);
18
JDK-8021350	:	Share	script	classes	between	threads/globals within	context
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
8u40
19
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
8u40
• Performance	improvement
– Optimistic	typing	(JEP	196)
– Code	Persistence	(JEP	194)
• Security
– Class	Filter	(JEP	202)
• Partial	support	for	ECMAScript	6
– Lexical-scoped	variables	and	constant	definition	(JEP	203)
20
Implemented	JEP
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	196:	Optimistic	Typing
• Type	inference	at	compilation	(not	at	runtime)
– Assume	specific	type	used	at	operation	
and	manipulation	of	array	index
– If	type	inference	is	wrong,	fall	back	and	
recompile	with	more	pessimistic	assumption
• --optimistic-types=true|false (-ot)
– Default:	false
21
For	generating	bytecode	like	Java
int long
double
Object
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	196:	Optimistic	Typing
• nashorn.typeInfo.maxFiles
– Maximum	#	of	files	to	cache	type	information
– if	0 is	set	to	this	property,	caching	is	disabled.
• nashorn.typeInfo.cacheDir
– Directory	where	files	storing	cached	type	information	exist.
• Windows: ${java.io.tmpdir}¥com.oracle.java.NashornTypeInfo
• Linux	and	Solaris: ~/.cache/com.oracle.java.NashornTypeInfo
• Mac	OS	X: ~/Library/Caches/com.oracle.java.NashornTypeInfo
22
Caching	result	of	type	inference
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	194:	Code	Persistence
• Cache	code	for	re-use	within	the	same	process.
• This	helps	smaller	memory	usage	and	shorter	startup	time.
23
Performance	improvement	with	caching	code
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	194:	Code	Persistence
• --class-cache-size=50 (-ccs)
– Class	cache	size	per	global	scope
– Default	:		50
• --persistent-code-cache=true|false (-pcc)
– Specifies	whether	or	not	optimistic	type	information	as	well	as	compiled	scripts	are	
persisted	to	disk
– Default	:	false
24
Performance	improvement	with	caching	code
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	194:	Code	Persistence
• Where	is	code	cache	stored?
– Default	:	When	invoking	JavaScript,	directory	named	nashorn_code_cache is	
created	and	cached	data	are	stored.
– Directory	name	is	configurable.
• nashorn.persistent.code.cache
• File	stored	cached	data	contains	not	only	class	byte	code	but	also	various	
metadata.
25
Optimistic	type	information	is	also	cached	to	disk
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Lazy	compilation
• Compile	methods	on	demand
• Default	:	true
– Before	8u40,	default	setting	was	false due	to	experimental	option.
26
--lazy-compilation=true|false
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	202:	Class	Filter
• JEP	202:	Nashorn Class	Filter
https://bugs.openjdk.java.net/browse/JDK-8043717
• Impementation is	required	in	Java	application	which	uses	JavaScript.
– jdk.nashorn.api.scripting.ClassFilter
27
Forbid	accessing	Java	classes	from	JavaScript
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
ClassFilter
// restrcting access to java.io.File
import jdk.nashorn.api.scripting.ClassFilter;
class MyFilter implements ClassFilter {
@Override
public boolean exposeToScripts(String s) {
if (s.compareTo("java.io.File") == 0) return false;
return true;
}
}
28
Implementation	example
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Partial	support	for	ECMAScript	6
29
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	203:	Lexically-scoped	variable	and	constant	declarations
• --language=es5|es6
– Default	:	es5
– --language=es6 is	mandatory	when	using	const and	let
30
Support	for	keyword	“let”	and	“const”
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
// let
let a=2;
function f(x) {
// a=2
if(x) {
let a=42;
}
// a is still equal to 2
}
// var
var a=2;
function f(x) {
// a is not undefined
if(x) {
var a=42;
}
// depending upon x, a is
equal to 42 or undefined
}
31
let and	var
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
// 1) Syntax error
function f(x) {
const b=1;
// Unable to assign
b = 99;
}
// 2) Scope #1
function f(x) {
const b=1;
var z=b+1; //z=2
}
//b is undefined
var y=b+1;
32
// 3) Scope #2
function f(x) {
const b=1;
var z =b+1; //z=2
}
//Able to declare b
const b = 10;
const
Copyright	©	2015,	Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Server	Side	JavaScript
33
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Serverside	JavaScript...
• Node.js	compatible	framework	running	on	JVM
– Nodyn
• Vert.x	+	Dyn.JS	+	Netty
– Trieme
• by	Apigee
• Reactive	Programming
– RxJS
– React.JS
34
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 35
https://blogs.oracle.com/theaquarium/entry/project_avatar_update
Avatar...
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
In	the	future...
36
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
In	the	Future
• Improve	with	steady	work
– Shorter	warmup	time
– Enhancement/improvement	of	Optimistic	Typing	and	Code	Persistence
• Java	9
– Fully	suport for	ECMAScript	6		(Java	9)
– Parser	API	for	Nashorn (JEP	236)
– Java	Flight	Recorder
• JavaScript	Profiler
• Tag	for	Nashorn
...etc
37
Java	8u60,	9,	and	...
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Improve	with	steady	work
• Improve	performance	when	parsing	JSON
– Poor	performance	when	handling	small	JSON	objects	using	
PropertyHashMap#findElement
• Re-write	jdk.nashorn.internal.parser.JSONParser
38
For	example...
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Java	Flight	Recorder
39
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	236:	Parser	API	for	Nashorn
• Objective
– Parser	API	for	representing	ECMAScript	code	as	AST
– Visitor	pattern
– Introduced	to	hide	internal	implementation	package (jdk.nashorn.internal.ir)
• Notice
– Not	script	level	API	but	Java	API
40
Public	API	for	ECMAScript AST
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
JEP	236:	Parser	API	for	Nashorn
• Available	after	JDK9	b55
• JavaDoc
– https://bugs.openjdk.java.net/br
owse/JDK-8048176
41
jdk.nashorn.api.tree
Copyright	©	2015,	Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Takeaway
42
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Key	takeaway
• Many	features	and	improvement	
have	been	introduced	to	8u20	
and	8u40.
• Nashorn is	being	improved	day	
by	day,	of	course.
• Please	give	your	feedback	to	
development	team!
43
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Nashorn Project
• Nashorn Mailing	List
– nashorn-dev@openjdk.java.net
• Nashorn Wiki
– https://wiki.openjdk.java.net/display/Nashorn/Main
• DEVELOPER_README
– http://hg.openjdk.java.net/jdk8u/jdk8u-
dev/nashorn/file/tip/docs/DEVELOPER_README
• Nashorn - JavaScript	for	the	JVM
– http://blogs.oracle.com/nashorn/
44
http://openjdk.java.net/projects/nashorn/
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Safe	Harbor	Statement
The	preceding	is	intended	to	outline	our	general	product	direction.	It	is	intended	for	
information	purposes	only,	and	may	not	be	incorporated	into	any	contract.	It	is	not	a	
commitment	to	deliver	any	material,	code,	or	functionality,	and	should	not	be	relied	upon	
in	making	purchasing	decisions.	The	development,	release,	and	timing	of	any	features	or	
functionality	described	for	Oracle’s	products	remains	at	the	sole	discretion	of	Oracle.
45
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		| 46
Copyright	©	2015, Oracle	and/or	its	affiliates.	All	rights	reserved.		|
Nashorn in the future (English)

Nashorn in the future (English)