Nashorn	
  in	
  the	
  Future	
  
Oracle  Corporation  Japan
Fusion  Middleware  Business  Unit
NISHIKAWA,  Akihiro
2015年年4⽉月8⽇日  
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	
  direcIon.	
  It	
  is	
  intended	
  for	
  
informaIon	
  purposes	
  only,	
  and	
  may	
  not	
  be	
  incorporated	
  into	
  any	
  contract.	
  It	
  is	
  not	
  a	
  
commitment	
  to	
  deliver	
  any	
  material,	
  code,	
  or	
  funcIonality,	
  and	
  should	
  not	
  be	
  relied	
  upon	
  
in	
  making	
  purchasing	
  decisions.	
  The	
  development,	
  release,	
  and	
  Iming	
  of	
  any	
  features	
  or	
  
funcIonality	
  described	
  for	
  Oracle’s	
  products	
  remains	
  at	
  the	
  sole	
  discreIon	
  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
•  Rhinoの置き換え	
  
– セキュリティ、パフォーマンスの向上
•  InvokeDynamic	
  (JSR-­‐292)	
  のProof	
  of	
  Concept	
  
5	
  
Java	
  8から導⼊入されたJavaScript	
  Engine	
  (JEP	
  174)	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
Nashorn
•  ECMAScript-­‐262	
  EdiIon	
  5.1
•  javax.script	
  (JSR	
  223)	
  API
•  JavaóJavaScript間での相互呼び出し	
  
•  コマンドラインツールは  jjs
Java	
  8から導⼊入されたJavaScript	
  Engine	
  (JEP	
  174)	
  
6	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
使ってみよう
•  jjs	
  
– Hello	
  World	
  
– Lambda	
  Expression,	
  Stream	
  
•  jjs	
  –scripting	
  
– Web	
  APIを呼び出してみる	
  
•  jjs	
  –fx	
  
– WebView
7	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
Lambda	
  ExpressionはFuncIonで	
  
//	
  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.	
  	
  |	
  
Javaから呼び出す(1)
ScriptEngineManager	
  manager	
  =	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  ScriptEngineManager();	
  
ScriptEngine	
  engine	
  =	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  manager.getEngineByName("nashorn");	
  
//	
  評価
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.	
  	
  |	
  
Javaから呼び出す(2)
engine.eval("function	
  hello(name){	
  
    	
  	
  	
  	
  	
  	
  	
    print('Hello,	
  '	
  +	
  name);	
  }");	
  
	
  
Invocable	
  inv=(Invocable)engine;	
  
	
  
//	
  Hello,	
  Taro	
  
Object	
  obj=	
  inv.invokeFunction("hello","Taro");	
  
10	
  
JavaScriptのfunc:onを呼び出す
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
Javaから呼び出す(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	
  
Script	
  func:onでInterfaceを実装する
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
8u20
12	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
8u20
•  セキュリティ⾯面の強化	
  
•  基礎部分のJIT/JDKの強化
13	
  
2014年年8⽉月リリース
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
constを  varで置き換え
•  デフォルトはfalse	
  
•  8u40でconstをサポートしたので、今後使えなくなる予定	
  
14	
  
-­‐-­‐const-­‐as-­‐var=true|false	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
ScriptからJava	
  PackageやClassへのアクセスを禁⽌止	
  
•  デフォルトはfalse	
  
15	
  
-­‐-­‐no-­‐java=true|false	
  (-­‐nj)	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
ECMAScript標準に反する構⽂文の使⽤用を禁⽌止	
  
•  Java.typeなどのExtensionは利利⽤用可	
  
•  -­‐scripting  併⽤用時には⾃自動的に無効化
16	
  
-­‐-­‐no-­‐syntax-­‐extensions	
  (-­‐nse)	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
#sourceURLと@sourceURL
•  Evalソースに名前を付ける
//#	
  sourceURL=myScript.js	
  
@sourceURL=myScript.js	
  
17	
  
JDK-­‐8032068	
  :	
  implement	
  @sourceURL	
  and	
  #sourceURL	
  direc:ves	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
クラスキャッシュ
•  当初は内部にキャッシュを保持せず、逐⼀一コンパイル	
  
•  8u20から、内部にキャッシュを持ち、コードを再利利⽤用するように	
  
•  例例:Javaから	
  
– engine.eval(new	
  URLReader(myScriptURL));	
  
•  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
•  パフォーマンス改善	
  
– OpImisIc	
  typing	
  (JEP	
  196)
– Code	
  Persistence	
  (JEP	
  194)	
  
•  セキュリティ	
  
– Class	
  Filter	
  (JEP	
  202)	
  
•  ECMAScript	
  6仕様の段階的なサポート	
  
– Lexical-­‐scoped	
  variables	
  and	
  constant	
  definiIon	
  (JEP	
  203)	
  
20	
  
実装されたJEP	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  196:	
  OpImisIc	
  Typing	
  
•  できるだけ実⾏行行時に型推論論せずに	
  
パフォーマンス向上をはかる	
  
– 演算および配列列のインデックス操作	
  
で使⽤用する特定の型(type)を仮定	
  
– 仮定した型推論論が正しくない場合、	
  
仮定を取り消してフォールバック
•  -­‐-­‐optimistic-­‐types=true|false	
  (-­‐ot)	
  
– デフォルトはfalse
21	
  
楽観的型推論論	
  –	
  Javaのようなバイトコードを⽣生成するために
int	
   long	
  
double	
  
Object	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  196:	
  OpImisIc	
  Typing	
  
•  nashorn.typeInfo.maxFiles	
  
– 型情報をキャッシュするためのファイルの最⼤大個数	
  
– 0でキャッシュを保持しない設定	
  
•  nashorn.typeInfo.cacheDir	
  
– 型情報をキャッシュするファイルが存在するディレクトリ	
  
• Windows:	
  ${java.io.tmpdir}com.oracle.java.NashornTypeInfo	
  
• LinuxとSolaris:	
  ~/.cache/com.oracle.java.NashornTypeInfo	
  
• Mac	
  OS	
  X:	
  ~/Library/Caches/com.oracle.java.NashornTypeInfo
22	
  
型推論論結果のキャッシュ	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  194:	
  Code	
  Persistence	
  
•  同じプロセス内で再利利⽤用できるようコードをキャッシュ	
  
•  メモリ使⽤用量量の削減ならびに起動時間短縮に寄与	
  
23	
  
コードキャッシュによる⾼高速化
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  194:	
  Code	
  Persistence	
  
•  -­‐-­‐class-­‐cache-­‐size=50	
  (-­‐ccs)	
  
– グローバル・スコープ毎のクラス・キャッシュサイズ
– デフォルトサイズは50	
  (個)
•  -­‐-­‐persistent-­‐code-­‐cache=true|false	
  (-­‐pcc)	
  
– 楽観的型推論論情報(OpImisIc	
  type	
  informaIon)も含め、ディスクにコンパイ
ル済みスクリプトを永続化	
  
– デフォルトはfalse	
  
24	
  
コードキャッシュによる⾼高速化
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  194:	
  Code	
  Persistence
•  -­‐pccを指定した場合の情報の永続先	
  
– デフォルトは実⾏行行ディレクトリのnashorn_code_cache	
  
– フォルダ名は変更更可能	
  
• nashorn.persistent.code.cache	
  システムプロパティ	
  
•  クラスのバイトコードだけでなく様々なメタデータを保持	
  
25	
  
コードキャッシュによる⾼高速化
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
遅延コンパイル
•  On	
  Demandでメソッドをコンパイル	
  
•  デフォルトは  true	
  
– 8u31までは試験的オプションのためfalse	
  
26	
  
-­‐-­‐lazy-­‐compilation=true|false	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  202:	
  Class	
  Filter	
  
•  JEP	
  202:	
  Nashorn	
  Class	
  Filter	
  
hjps://bugs.openjdk.java.net/browse/JDK-­‐8043717	
  
•  JavaScriptを使うJavaアプリケーション側で実装	
  
– jdk.nashorn.api.scripting.ClassFilter	
  
27	
  
JavaScriptからJavaクラスへのアクセスを抑⽌止
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
ClassFilter実装例例
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	
  
java.io.Fileを制限したい場合
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
ECMAScript	
  6の段階的なサポート
29	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  203:	
  Lexically-­‐scoped	
  variable	
  and	
  constant	
  declaraIons	
  
•  -­‐-­‐language=es5|es6	
  
– デフォルトはes5	
  
– const、letを使う場合は-­‐-­‐language=es6が必須	
  
•  let	
  
– ブロックに含まれるスコープ内変数の宣⾔言に利利⽤用	
  
•  const
– letと同様、constを使って宣⾔言した定数はブロック内に⽣生存範囲を限定	
  
30	
  
スコープ内変数(let)や定数(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は2のまま
}	
  
//	
  var	
  
var	
  a=2;	
  
	
  
function	
  f(x)	
  {	
  
	
  	
  //	
  ここではaは未定義
	
  	
  if(x)	
  {	
  
	
  	
  	
  	
  var	
  a=42;	
  
	
  	
  }	
  
	
  	
  //	
  x次第でaは42もしくは未定義
}	
  
31	
  
letとvar
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
//	
  1)	
  構⽂文エラー
function	
  f(x)	
  {	
  
	
  	
  const	
  b=1;	
  
	
  	
  //	
  代⼊入不不可	
  
	
  	
  b	
  =	
  99;	
  
}	
  
	
  
//	
  2)	
  スコープその1
function	
  f(x)	
  {	
  
	
  	
  const	
  b=1;	
  
	
  	
  var	
  z=b+1;	
  //z=2	
  
}	
  
//bは未定義
var	
  y=b+1;	
  
32	
  
//	
  3)	
  スコープその2
function	
  f(x)	
  {	
  
	
  	
  const	
  b=1;	
  
	
  	
  var	
  z	
  =b+1;	
  //z=2	
  
}	
  
//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.	
  	
  |	
  
Server	
  Side	
  JavaScript…
•  JVMで動作するNode.js互換フレームワーク	
  
– Nodyn	
  
• Vert.x	
  +	
  Dyn.JS	
  +	
  Nejy	
  
– Trieme	
  
• apigeeによるNode.js互換フレームワーク	
  
•  最近はやりのReacIve	
  Programming	
  
– RxJS	
  
– React.JS	
  
などなど	
  
34	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
   35	
  
hVps://blogs.oracle.com/theaquarium/entry/project_avatar_update
そんな時代もあったね...
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
•  地道な改善	
  
– ウォームアップ時間短縮のための改善	
  
– OpImisIc	
  Typing、Code	
  Persistenceの改善	
  
•  Java	
  9	
  
– ECMAScript	
  6の完全サポート	
  
– Parser	
  API	
  for	
  Nashorn	
  (JEP	
  236)	
  
– Java	
  Flight	
  Recorder	
  
• JavaScript	
  Profiler	
  
• Nashorn⽤用のタグ	
  
	
   	
   	
  	
  	
   	
   	
   	
  ...などなど	
  
37	
  
Java	
  8u60、9、その先
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
地道な改善	
  
•  JSONパース時の性能向上のために	
  
– プロパティ情報などの⼩小さなJSONオブジェクトを取り扱う場合、  
PropertyHashMap#findElement	
  を使うと遅かった。	
  
	
  
jdk.nashorn.internal.parser.JSONParserを書き換え
38	
  
例例えば8u60では
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
Java	
  Flight	
  Recorder
39	
  
JavaScript	
  Profilerの可能性
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  236:	
  Parser	
  API	
  for	
  Nashorn	
  
•  ⽬目的	
  
– ECMAScriptのコードをASTとして表現するためのParser	
  API	
  
– Visitorパターン	
  
– 内部実装パッケージ  (jdk.nashorn.internal.ir)	
  を使わせない	
  
•  注意	
  
– Java	
  API(スクリプトレベルのAPIではない)	
  
– 可能な限りECMAScriptの仕様に基づいてASTノードを表現	
  
40	
  
ECMAScript	
  ASTのための公開API
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
JEP	
  236:	
  Parser	
  API	
  for	
  Nashorn	
  
•  JDK9	
  b55以後で試すことが
できる	
  
•  JavaDoc
– hjps://bugs.openjdk.java.net/
browse/JDK-­‐8048176
41	
  
jdk.nashorn.api.tree	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
まとめ
42	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
まとめ
•  8u20、8u40でパフォーマンス向上の
ための機能が追加されました	
  
•  今後もNashornの開発は続⾏行行します	
  
•  是⾮非フィードバックをお願いします	
  
43	
  
Copyright	
  ©	
  2015,	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.	
  	
  |	
  
Nashorn	
  Project
•  Nashorn	
  Mailing	
  List	
  
– nashorn-­‐dev@openjdk.java.net	
  
•  Nashorn	
  Wiki	
  
– hjps://wiki.openjdk.java.net/display/Nashorn/Main	
  
•  DEVELOPER_README	
  
– hjp://hg.openjdk.java.net/jdk8u/jdk8u-­‐dev/nashorn/file/Ip/docs/
DEVELOPER_README	
  
•  Nashorn	
  -­‐	
  JavaScript	
  for	
  the	
  JVM	
  
– hjp://blogs.oracle.com/nashorn/	
  
44	
  
hjp://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	
  direcIon.	
  It	
  is	
  intended	
  for	
  
informaIon	
  purposes	
  only,	
  and	
  may	
  not	
  be	
  incorporated	
  into	
  any	
  contract.	
  It	
  is	
  not	
  a	
  
commitment	
  to	
  deliver	
  any	
  material,	
  code,	
  or	
  funcIonality,	
  and	
  should	
  not	
  be	
  relied	
  upon	
  
in	
  making	
  purchasing	
  decisions.	
  The	
  development,	
  release,	
  and	
  Iming	
  of	
  any	
  features	
  or	
  
funcIonality	
  described	
  for	
  Oracle’s	
  products	
  remains	
  at	
  the	
  sole	
  discreIon	
  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 (Japanese)

Nashorn in the future (Japanese)

  • 1.
    Nashorn  in  the  Future   Oracle  Corporation  Japan Fusion  Middleware  Business  Unit NISHIKAWA,  Akihiro 2015年年4⽉月8⽇日   Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  
  • 2.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Safe  Harbor  Statement   The  following  is  intended  to  outline  our  general  product  direcIon.  It  is  intended  for   informaIon  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a   commitment  to  deliver  any  material,  code,  or  funcIonality,  and  should  not  be  relied  upon   in  making  purchasing  decisions.  The  development,  release,  and  Iming  of  any  features  or   funcIonality  described  for  Oracle’s  products  remains  at  the  sole  discreIon  of  Oracle.   2  
  • 3.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Agenda   What's  Nashorn?   8u20   8u40   In  the  future...   1   2   3   4   3  
  • 4.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   What’s  Nashorn? 4  
  • 5.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Nashorn •  Rhinoの置き換え   – セキュリティ、パフォーマンスの向上 •  InvokeDynamic  (JSR-­‐292)  のProof  of  Concept   5   Java  8から導⼊入されたJavaScript  Engine  (JEP  174)  
  • 6.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Nashorn •  ECMAScript-­‐262  EdiIon  5.1 •  javax.script  (JSR  223)  API •  JavaóJavaScript間での相互呼び出し   •  コマンドラインツールは  jjs Java  8から導⼊入されたJavaScript  Engine  (JEP  174)   6  
  • 7.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   使ってみよう •  jjs   – Hello  World   – Lambda  Expression,  Stream   •  jjs  –scripting   – Web  APIを呼び出してみる   •  jjs  –fx   – WebView 7  
  • 8.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Lambda  ExpressionはFuncIonで   //  Java   array.stream().sorted(Comparator.naturalOrder())   .forEach(  t  -­‐>  sortedArray.add(t)  );     //  Nashorn   array.stream().sorted(Comparator.naturalOrder())   .forEach(function(t)  sortedArray.add(t));     8  
  • 9.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Javaから呼び出す(1) ScriptEngineManager  manager  =                            new  ScriptEngineManager();   ScriptEngine  engine  =                              manager.getEngineByName("nashorn");   //  評価 engine.eval("print('hello  world')");  //  hello  world   engine.eval(new  FileReader(“hello.js”));  //  hello.js   9  
  • 10.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Javaから呼び出す(2) engine.eval("function  hello(name){                       print('Hello,  '  +  name);  }");     Invocable  inv=(Invocable)engine;     //  Hello,  Taro   Object  obj=  inv.invokeFunction("hello","Taro");   10   JavaScriptのfunc:onを呼び出す
  • 11.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Javaから呼び出す(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   Script  func:onでInterfaceを実装する
  • 12.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   8u20 12  
  • 13.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   8u20 •  セキュリティ⾯面の強化   •  基礎部分のJIT/JDKの強化 13   2014年年8⽉月リリース
  • 14.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   constを  varで置き換え •  デフォルトはfalse   •  8u40でconstをサポートしたので、今後使えなくなる予定   14   -­‐-­‐const-­‐as-­‐var=true|false  
  • 15.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   ScriptからJava  PackageやClassへのアクセスを禁⽌止   •  デフォルトはfalse   15   -­‐-­‐no-­‐java=true|false  (-­‐nj)  
  • 16.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   ECMAScript標準に反する構⽂文の使⽤用を禁⽌止   •  Java.typeなどのExtensionは利利⽤用可   •  -­‐scripting  併⽤用時には⾃自動的に無効化 16   -­‐-­‐no-­‐syntax-­‐extensions  (-­‐nse)  
  • 17.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   #sourceURLと@sourceURL •  Evalソースに名前を付ける //#  sourceURL=myScript.js   @sourceURL=myScript.js   17   JDK-­‐8032068  :  implement  @sourceURL  and  #sourceURL  direc:ves  
  • 18.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   クラスキャッシュ •  当初は内部にキャッシュを保持せず、逐⼀一コンパイル   •  8u20から、内部にキャッシュを持ち、コードを再利利⽤用するように   •  例例:Javaから   – engine.eval(new  URLReader(myScriptURL));   •  JavaScriptから   – load(url);   18   JDK-­‐8021350  :  Share  script  classes  between  threads/globals  within  context  
  • 19.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   8u40 19  
  • 20.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   8u40 •  パフォーマンス改善   – OpImisIc  typing  (JEP  196) – Code  Persistence  (JEP  194)   •  セキュリティ   – Class  Filter  (JEP  202)   •  ECMAScript  6仕様の段階的なサポート   – Lexical-­‐scoped  variables  and  constant  definiIon  (JEP  203)   20   実装されたJEP  
  • 21.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  196:  OpImisIc  Typing   •  できるだけ実⾏行行時に型推論論せずに   パフォーマンス向上をはかる   – 演算および配列列のインデックス操作   で使⽤用する特定の型(type)を仮定   – 仮定した型推論論が正しくない場合、   仮定を取り消してフォールバック •  -­‐-­‐optimistic-­‐types=true|false  (-­‐ot)   – デフォルトはfalse 21   楽観的型推論論  –  Javaのようなバイトコードを⽣生成するために int   long   double   Object  
  • 22.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  196:  OpImisIc  Typing   •  nashorn.typeInfo.maxFiles   – 型情報をキャッシュするためのファイルの最⼤大個数   – 0でキャッシュを保持しない設定   •  nashorn.typeInfo.cacheDir   – 型情報をキャッシュするファイルが存在するディレクトリ   • Windows:  ${java.io.tmpdir}com.oracle.java.NashornTypeInfo   • LinuxとSolaris:  ~/.cache/com.oracle.java.NashornTypeInfo   • Mac  OS  X:  ~/Library/Caches/com.oracle.java.NashornTypeInfo 22   型推論論結果のキャッシュ  
  • 23.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  194:  Code  Persistence   •  同じプロセス内で再利利⽤用できるようコードをキャッシュ   •  メモリ使⽤用量量の削減ならびに起動時間短縮に寄与   23   コードキャッシュによる⾼高速化
  • 24.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  194:  Code  Persistence   •  -­‐-­‐class-­‐cache-­‐size=50  (-­‐ccs)   – グローバル・スコープ毎のクラス・キャッシュサイズ – デフォルトサイズは50  (個) •  -­‐-­‐persistent-­‐code-­‐cache=true|false  (-­‐pcc)   – 楽観的型推論論情報(OpImisIc  type  informaIon)も含め、ディスクにコンパイ ル済みスクリプトを永続化   – デフォルトはfalse   24   コードキャッシュによる⾼高速化
  • 25.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  194:  Code  Persistence •  -­‐pccを指定した場合の情報の永続先   – デフォルトは実⾏行行ディレクトリのnashorn_code_cache   – フォルダ名は変更更可能   • nashorn.persistent.code.cache  システムプロパティ   •  クラスのバイトコードだけでなく様々なメタデータを保持   25   コードキャッシュによる⾼高速化
  • 26.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   遅延コンパイル •  On  Demandでメソッドをコンパイル   •  デフォルトは  true   – 8u31までは試験的オプションのためfalse   26   -­‐-­‐lazy-­‐compilation=true|false  
  • 27.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  202:  Class  Filter   •  JEP  202:  Nashorn  Class  Filter   hjps://bugs.openjdk.java.net/browse/JDK-­‐8043717   •  JavaScriptを使うJavaアプリケーション側で実装   – jdk.nashorn.api.scripting.ClassFilter   27   JavaScriptからJavaクラスへのアクセスを抑⽌止
  • 28.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   ClassFilter実装例例 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   java.io.Fileを制限したい場合
  • 29.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   ECMAScript  6の段階的なサポート 29  
  • 30.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  203:  Lexically-­‐scoped  variable  and  constant  declaraIons   •  -­‐-­‐language=es5|es6   – デフォルトはes5   – const、letを使う場合は-­‐-­‐language=es6が必須   •  let   – ブロックに含まれるスコープ内変数の宣⾔言に利利⽤用   •  const – letと同様、constを使って宣⾔言した定数はブロック内に⽣生存範囲を限定   30   スコープ内変数(let)や定数(const)の宣⾔言
  • 31.
    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は2のまま }   //  var   var  a=2;     function  f(x)  {      //  ここではaは未定義    if(x)  {          var  a=42;      }      //  x次第でaは42もしくは未定義 }   31   letとvar
  • 32.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   //  1)  構⽂文エラー function  f(x)  {      const  b=1;      //  代⼊入不不可      b  =  99;   }     //  2)  スコープその1 function  f(x)  {      const  b=1;      var  z=b+1;  //z=2   }   //bは未定義 var  y=b+1;   32   //  3)  スコープその2 function  f(x)  {      const  b=1;      var  z  =b+1;  //z=2   }   //bは定義可能 const  b  =  10;   const
  • 33.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Server  Side  JavaScript 33  
  • 34.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Server  Side  JavaScript… •  JVMで動作するNode.js互換フレームワーク   – Nodyn   • Vert.x  +  Dyn.JS  +  Nejy   – Trieme   • apigeeによるNode.js互換フレームワーク   •  最近はやりのReacIve  Programming   – RxJS   – React.JS   などなど   34  
  • 35.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   35   hVps://blogs.oracle.com/theaquarium/entry/project_avatar_update そんな時代もあったね...
  • 36.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   In  the  future... 36  
  • 37.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   In  the  Future •  地道な改善   – ウォームアップ時間短縮のための改善   – OpImisIc  Typing、Code  Persistenceの改善   •  Java  9   – ECMAScript  6の完全サポート   – Parser  API  for  Nashorn  (JEP  236)   – Java  Flight  Recorder   • JavaScript  Profiler   • Nashorn⽤用のタグ                  ...などなど   37   Java  8u60、9、その先
  • 38.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   地道な改善   •  JSONパース時の性能向上のために   – プロパティ情報などの⼩小さなJSONオブジェクトを取り扱う場合、   PropertyHashMap#findElement  を使うと遅かった。     jdk.nashorn.internal.parser.JSONParserを書き換え 38   例例えば8u60では
  • 39.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Java  Flight  Recorder 39   JavaScript  Profilerの可能性
  • 40.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  236:  Parser  API  for  Nashorn   •  ⽬目的   – ECMAScriptのコードをASTとして表現するためのParser  API   – Visitorパターン   – 内部実装パッケージ  (jdk.nashorn.internal.ir)  を使わせない   •  注意   – Java  API(スクリプトレベルのAPIではない)   – 可能な限りECMAScriptの仕様に基づいてASTノードを表現   40   ECMAScript  ASTのための公開API
  • 41.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JEP  236:  Parser  API  for  Nashorn   •  JDK9  b55以後で試すことが できる   •  JavaDoc – hjps://bugs.openjdk.java.net/ browse/JDK-­‐8048176 41   jdk.nashorn.api.tree  
  • 42.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   まとめ 42  
  • 43.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   まとめ •  8u20、8u40でパフォーマンス向上の ための機能が追加されました   •  今後もNashornの開発は続⾏行行します   •  是⾮非フィードバックをお願いします   43  
  • 44.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Nashorn  Project •  Nashorn  Mailing  List   – nashorn-­‐dev@openjdk.java.net   •  Nashorn  Wiki   – hjps://wiki.openjdk.java.net/display/Nashorn/Main   •  DEVELOPER_README   – hjp://hg.openjdk.java.net/jdk8u/jdk8u-­‐dev/nashorn/file/Ip/docs/ DEVELOPER_README   •  Nashorn  -­‐  JavaScript  for  the  JVM   – hjp://blogs.oracle.com/nashorn/   44   hjp://openjdk.java.net/projects/nashorn/  
  • 45.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Safe  Harbor  Statement   The  preceding  is  intended  to  outline  our  general  product  direcIon.  It  is  intended  for   informaIon  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a   commitment  to  deliver  any  material,  code,  or  funcIonality,  and  should  not  be  relied  upon   in  making  purchasing  decisions.  The  development,  release,  and  Iming  of  any  features  or   funcIonality  described  for  Oracle’s  products  remains  at  the  sole  discreIon  of  Oracle.   45  
  • 46.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |           46  
  • 48.
    Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |