Nashorn in the Future Oracle Corporation Japan Fusion Middleware Business Unit NISHIKAWA, Akihiro 2015 年 4 月 8 日
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
Agenda 1 2 3 4 What's Nashorn? 8u20 8u40 In the future... 3
What s Nashorn? 4
Nashorn Java 8 から導入された JavaScript Engine (JEP 174) Rhino の置き換え セキュリティ パフォーマンスの向上 InvokeDynamic (JSR-292) の Proof of Concept 5
Nashorn Java 8 から導入された JavaScript Engine (JEP 174) ECMAScript-262 Edition 5.1 javax.script (JSR 223) API Java JavaScript 間での相互呼び出し コマンドラインツールは jjs 6
使ってみよう jjs Hello World Lambda Expression, Stream jjs scripting Web API を呼び出してみる jjs fx WebView 7
Lambda Expression は Function で // Java array.stream().sorted(comparator.naturalorder()).foreach( t -> sortedarray.add(t) ); // Nashorn array.stream().sorted(comparator.naturalorder()).foreach(function(t) sortedarray.add(t)); 8
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
Java から呼び出す (2) JavaScript の function を呼び出す engine.eval("function hello(name){ print('hello, ' + name); }"); Invocable inv=(invocable)engine; // Hello, Taro Object obj= inv.invokefunction("hello","taro"); 10
Java から呼び出す (3) Script function で Interface を実装する 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
8u20 12
8u20 2014 年 8 月リリース セキュリティ面の強化 基礎部分のJIT/JDKの強化 13
const を var で置き換え --const-as-var=true false デフォルトは false 8u40 で const をサポートしたので 今後使えなくなる予定 14
Script から Java Package や Class へのアクセスを禁止 --no-java=true false (-nj) デフォルトは false 15
ECMAScript 標準に反する構文の使用を禁止 --no-syntax-extensions (-nse) Java.typeなどのExtensionは利用可 -scripting 併用時には自動的に無効化 16
#sourceurl と @sourceurl JDK-8032068 : implement @sourceurl and #sourceurl directives Eval ソースに名前を付ける //# sourceurl=myscript.js @sourceurl=myscript.js 17
クラスキャッシュ JDK-8021350 : Share script classes between threads/globals within context 当初は内部にキャッシュを保持せず 逐一コンパイル 8u20から 内部にキャッシュを持ち コードを再利用するように 例 :Java から engine.eval(new URLReader(myScriptURL)); JavaScript から load(url); 18
8u40 19
8u40 実装された JEP パフォーマンス改善 Optimistic typing (JEP 196) Code Persistence (JEP 194) セキュリティ Class Filter (JEP 202) ECMAScript 6 仕様の段階的なサポート Lexical-scoped variables and constant definition (JEP 203) 20
JEP 196: Optimistic Typing 楽観的型推論 Java のようなバイトコードを生成するために できるだけ実行時に型推論せずにパフォーマンス向上をはかる 演算および配列のインデックス操作で使用する特定の型 (type) を仮定 仮定した型推論が正しくない場合 仮定を取り消してフォールバック int long double --optimistic-types=true false (-ot) デフォルトは false Object 21
JEP 196: Optimistic 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
JEP 194: Code Persistence コードキャッシュによる高速化 同じプロセス内で再利用できるようコードをキャッシュ メモリ使用量の削減ならびに起動時間短縮に寄与 23
JEP 194: Code Persistence コードキャッシュによる高速化 --class-cache-size=50 (-ccs) グローバル スコープ毎のクラス キャッシュサイズ デフォルトサイズは 50 ( 個 ) --persistent-code-cache=true false (-pcc) 楽観的型推論情報 (Optimistic type information) も含め ディスクにコンパイル済みスクリプトを永続化 デフォルトは false 24
JEP 194: Code Persistence コードキャッシュによる高速化 -pcc を指定した場合の型情報の永続先 デフォルトは実行ディレクトリの nashorn_code_cache フォルダ名は変更可能 nashorn.persistent.code.cache システムプロパティ クラスのバイトコードだけでなく様々なメタデータを保持 25
遅延コンパイル --lazy-compilation=true false On Demandでメソッドをコンパイル デフォルトは true 8u31 までは試験的オプションのため false 26
JEP 202: Class Filter JavaScript から Java クラスへのアクセスを抑止 JEP 202: Nashorn Class Filter https://bugs.openjdk.java.net/browse/jdk-8043717 JavaScript を使う Java アプリケーション側で実装 jdk.nashorn.api.scripting.classfilter 27
ClassFilter 実装例 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
ECMAScript 6 の段階的なサポート 29
JEP 203: Lexically-scoped variable and constant declarations スコープ内変数 (let) や定数 (const) の宣言 --language=es5 es6 デフォルトは es5 const let を使う場合は --language=es6 が必須 let ブロックに含まれるスコープ内変数の宣言に利用 const let と同様 const を使って宣言した定数はブロック内に生存範囲を限定 30
let と var // 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
const // 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; // 3) スコープその 2 function f(x) { const b=1; var z =b+1; //z=2 } //b は定義可能 const b = 10; 32
Server Side JavaScript 33
Server Side JavaScript JVM で動作する Node.js 互換フレームワーク Nodyn Vert.x + Dyn.JS + Netty Trieme apigee による Node.js 互換フレームワーク 最近はやりの Reactive Programming RxJS React.JS などなど 34
そんな時代もあったね... https://blogs.oracle.com/theaquarium/entry/project_avatar_update 35
In the future... 36
In the Future Java 8u60 9 その先 地道な改善 ウォームアップ時間短縮のための改善 Optimistic Typing Code Persistence の改善 Java 9 ECMAScript 6 のサポート Parser API for Nashorn (JEP 236) Java Flight Recorder JavaScript Profiler Nashorn 用のタグ... などなど 37
地道な改善 例えば 8u60 では JSON パース時の性能向上のために プロパティ情報などの小さな JSON オブジェクトを取り扱う場合 PropertyHashMap#findElement を使うと遅かった jdk.nashorn.internal.parser.jsonparser を書き換え 38
Java Flight Recorder JavaScript Profiler の可能性 39
JEP 236: Parser API for Nashorn ECMAScript AST のための公開 API 目的 ECMAScript のコードを AST として表現するための Parser API Visitor パターン 内部実装パッケージ (jdk.nashorn.internal.ir) を使わせない 注意 Java API( スクリプトレベルの API ではない ) 可能な限り ECMAScript の仕様に基づいて AST ノードを表現 40
JEP 236: Parser API for Nashorn jdk.nashorn.api.tree JDK9 b55 以後で試すことができる JavaDoc https://bugs.openjdk.java.net/br owse/jdk-8048176 41
まとめ 42
まとめ 8u20 8u40 でパフォーマンス向上のための機能が追加されました 今後も Nashorn の開発は続行します 是非フィードバックをお願いします 43
Nashorn Project http://openjdk.java.net/projects/nashorn/ 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/jdk8udev/nashorn/file/tip/docs/developer_readme Nashorn - JavaScript for the JVM http://blogs.oracle.com/nashorn/ 44
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
46