Nashorn : JavaScript Running on Java VM (Japanese)

69
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 1 JavaScript Running On JavaVM: Nashorn NISHIKAWA, Akihiro Oracle Corporation Japan

description

Presented at Java Day Tokyo 2014 (#JavaDayTokyo).

Transcript of Nashorn : JavaScript Running on Java VM (Japanese)

Page 1: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.1

JavaScript Running On JavaVM: Nashorn

NISHIKAWA, Akihiro

Oracle Corporation Japan

Page 2: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.2

以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するも

のです。また、情報提供を唯一の目的とするものであり、いかなる契約に

も組み込むことはできません。以下の事項は、マテリアルやコード、機能を

提供することをコミットメント(確約)するものではないため、購買決定を行う

際の判断材料になさらないで下さい。オラクル製品に関して記載されてい

る機能の開発、リリースおよび時期については、弊社の裁量により決定さ

れます。

OracleとJavaは、Oracle Corporation 及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。

Page 3: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Agenda Nashorn

Server Side JavaScript

Nashornの今後

Page 4: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4

Nashorn

Page 5: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5

NashornCompact1 Profileでも使えるJavaScript Engine

Page 6: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6

NashornCompact1 Profileでも使えるJavaScript Engine

Page 7: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7

登場の背景

Rhinoの置き換え

– セキュリティ

–パフォーマンス

InvokeDynamic (JSR-292) のProof of Concept

アトウッドの法則

Any application that can be written in JavaScript will

eventually be written in JavaScript.

Page 8: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8

Project Nashornの主なスコープJEP 174

ECMAScript-262 Edition 5.1

javax.script (JSR 223) API

JavaとJavaScript間での相互呼び出し

新しいコマンドラインツール(jjs)の導入

Page 9: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9

Java VM

Scripting Engine

(Nashorn)

Scripting API

(JSR-223)

JavaScript codeJava code

Other runtime

Other APIs jjs

Page 10: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10

$JAVA_HOME/bin/jjs

$JAVA_HOME/jre/lib/ext/nashorn.jar

Page 11: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11

ECMAScript-262 Edition 5.1に100%対応

Page 12: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12

未実装・未サポート

ECMAScript 6 (Harmony)

– Generators

–分割代入(Destructuring assignment)

– const, let, ...

DOM/CSSおよびDOM/CSS関連ライブラリ

– jQuery, Prototype, Dojo, …

ブラウザAPIやブラウザエミュレータ

– HTML5 canvas, HTML5 audio, WebGL, WebWorkers...

Page 13: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13

実行例

Page 14: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14

JavaからNashorn

ScriptEngineManager manager= new ScriptEngineManager();

ScriptEngine engine= manager.getEngineByName("nashorn");

engine.eval("print('hello world')");

engine.eval(new FileReader("hello.js"));

Page 15: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15

JavaからNashornJavaからScript functionを呼び出す

engine.eval("function hello(name) {print('Hello, ' + name)}");

Invocable inv=(Invocable)engine;

Object obj=inv.invokeFunction("hello","Taro");

Page 16: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16

JavaからNashornScript 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();

Page 17: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17

NashornからJava

print(java.lang.System.currentTimeMillis());

jjs -fx ...

Page 18: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18

Nashorn for ScriptingScripting用途で使えるように機能追加

-scriptingオプション

Here document

Back quote

String Interpolation

...

Page 19: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19

Nashorn Extensions

Page 20: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20

Nashorn Extensions今日ご紹介するのは

Java typeの参照を取得

Javaオブジェクトのプロパティアクセス

Lambda、SAM、Script関数の関係

スコープおよびコンテキスト

Page 21: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21

Java type

Page 22: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22

Java.typeRhinoでの表記(Nashornでも利用可)

var hashmap=new java.util.HashMap();

または

var HashMap=java.util.HashMap;var hashmap=new HashMap();

Page 23: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23

Java.typeNashornで推奨する表記

var HashMap=Java.type('java.util.HashMap');var hashmap=new HashMap();

Page 24: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.24

Java.typeClass or Package?

java.util.ArrayList

java.util.Arraylist

Page 25: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25

Java配列Rhinoでの表記

var intArray=java.lang.reflect.Array.newInstance(

java.lang.Integer.TYPE, 5);

var Array=java.lang.reflect.Array;

var intClass=java.lang.Integer.TYPE;

var array=Array.newInstance(intClass, 5);

Page 26: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26

Java配列Nashornでの表記

var intArray=new(Java.type("int[]"))(5);

var intArrayType=Java.type("int[]");var intArray=new intArrayType(5);

Page 27: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27

プロパティへのアクセス

Page 28: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.28

getter/setter

var HashMap=Java.type('java.util.HashMap');

var map=new HashMap();

map.put('size', 2);

print(map.get('size')); // 2

Page 29: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.29

プロパティ

var HashMap=Java.type('java.util.HashMap');

var map=new HashMap();

map.size=3;

print(map.size); // 3

print(map.size()); // 1

Page 30: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.30

連想配列

var HashMap=Java.type('java.util.HashMap');

var map=new HashMap();

map['size']=4;

print(map['size']); // 4

print(map['size']()); // 1

Page 31: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.31

Lambda, SAM, and Script function

Page 32: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.32

Lambda, SAM, and Script functionScript functionをLambdaオブジェクトやSAMインターフェースを実装するオブジェクトに自動変換

var timer=new java.util.Timer();

timer.schedule(

function() { print('Tick') }, 0, 1000);

java.lang.Thread.sleep(5000);

timer.cancel();

Page 33: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.33

Lambda, SAM, and Script functionLambda typeのインスタンスであるオブジェクトをScript functionのように取り扱う

var JFunction=Java.type('java.util.function.Function');

var obj=new JFunction() {

// x->print(x*x)

apply: function(x) { print(x*x) }

}

print(typeof obj); //function

obj(9); // 81 Script functionっぽく

Page 34: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.34

Scope and Context

Page 35: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.35

Scope and Contextload と loadWithNewGlobal

load

–同じグローバル・スコープにScriptをロード

– ロードしたScriptにロード元のScriptと同じ名称の変数が存在する場合、変数が衝突する可能性がある

loadWithNewGlobal

–グローバル・スコープを新規作成し、そのスコープにJavaScriptをロード

– ロード元に同じ名前の変数があっても衝突しない

Page 36: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.36

Scope and ContextScriptContextはBindingに紐付いた複数のスコープをサポート

ScriptContext ctx=new SimpleScriptContext();

ctx.setBindings(engine.createBindings(),ScriptContext.ENGINE_SCOPE);

Bindings engineScope=ctx.getBindings(ScriptContext.ENGINE_SCOPE);

engineScope.put("x", "world");

engine.eval("print(x)", ctx);

Page 37: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.37

Scope and Contextスコープを区切るためにJavaImporterをwithと共に利用

with(new JavaImporter(java.util, java.io)){

var map=new HashMap(); //java.util.HashMap

map.put("js", "javascript");

map.put("java", "java");

print(map);

....

}

Page 38: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.38

その他のNashorn Extensions

Java配列とJavaScript配列の変換

– Java.from

– Java.to

Javaクラスの拡張、スーパークラス・オブジェクトの取得

– Java.extend

– Java.super

など

Page 39: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.39

Server Side JavaScript

Page 40: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.40

Java EE for Next Generation ApplicationsHTML5に対応した、動的かつスケーラブルなアプリケーション提供のために

Web

So

ckets

Avata

r

Page 41: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.41

Webアプリケーションアーキテクチャの進化Request-Response and Multi-page application

Java EE/JVM

Presentation

(Servlet/JSP)

Business

Logic

Backend

ConnectivityBrowser

Page 42: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.42

Webアプリケーションアーキテクチャの進化Ajax (JavaScript)の利用

Java EE/JVM

Connectivity(REST, SSE)

Presentation(Servlet/JSP, JSF)

Business

Logic

Backend

ConnectivityBrowser

JavaScript

Page 43: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.43

今風のWebアプリケーションアーキテクチャPresentationよりはむしろ接続性を重視

Java EE/JVM

Connectivity(WebSocket,

REST, SSE)

Presentation(Servlet/JSP, JSF)

Business

Logic

Backend

ConnectivityBrowser

View

Controller

JavaScript

Page 44: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.44

How about Node.js?

Page 45: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.45

既存サービスのモバイル対応例

Node.js

JavaScript

REST

SSE

WebSocket

Browser

View

Controller

JavaScript

Page 46: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.46

How aboutNode.js on JVM?

Page 47: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.47

既存サービスのモバイル対応例NodeをJava VMで動作させよう

Java EE/JVM

Node

Server

Business

Logic

Backend

Connectivity

Client

JavaScriptBrowser

View

Controller

JavaScript

Page 48: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.48

Avatar.js

Page 49: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.49

Avatar.js

Node.jsで利用できるモジュールをほぼそのまま利用可能

– Express、async、socket.ioなど

– npmで取り込んだモジュールを認識

利点

– Nodeプログラミングモデルの利用

–既存資産、ナレッジ、ツールの活用

Page 50: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.50

Avatar.js = Node + JavaThreadも含めたJavaテクノロジーを活用

JavaJavaScript

com.myorg.myObj

java.util.SortedSet

java.lang.Thread

require('async')

postEvent

Node App

Page 51: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.51

Avatar

Page 52: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.52

AvatarサーバーサイドJavaScriptサービスフレームワーク

REST、WebSocket、Server Sent Event (SSE) でのデータ送受信に特化

Node.jsのイベント駆動プログラミングモデルやプログラミングモジュールを活用

エンタープライズ機能(Java EE)との統合

Page 53: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.53

*.html

*.js

*.css

HTTP

Application

Services

Avatar Modules

Node Modules

Avatar.js

Avatar Runtime

Avatar Compiler

Server Runtime (Java EE)

JDK 8 / Nashorn

Application

Views

REST/WebSocket/SSE

Avatar (Avatar EE)

変更通知

データ

Page 54: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.54

HTTP

REST/WebSocket/SSE

Avatar Compiler

Application

Views

*.html

*.js

*.css

Application

Services

Avatar Modules

Node Modules

Avatar.js

Avatar Runtime

Server Runtime (Java EE)

JDK 8 / Nashorn

アーキテクチャ(Server)

変更通知

データ

Page 55: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.55

Avatar Service

Java

JavaS

cri

pt

HTTP Load Balancer

Services

Shared State

Services Services

変更通知

データ

Page 56: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.56

Avatar Runtime

Server Runtime (Java EE)

Avatar Modules

Node Modules

Avatar.js

*.html

*.js

*.css

Application

Services

JDK 8 / Nashorn

アーキテクチャ(Client)

変更通知

データ

HTTP

REST/WebSocket/SSE

Application

Views

Avatar Compiler

Page 57: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.57

Avatar and Avatar.jsprogress steadily.

Page 58: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.58

Nashornの今後

Page 59: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.59

Nashornの今後

Bytecode Persistence (JEP 194)

http://openjdk.java.net/jeps/194

Optimistic Typing

その他

Page 60: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.60

まとめ

Page 61: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.61

まとめ

Nashorn

– Javaと緊密に統合

– Rhinoからの移行にあたっては表記方法が変わっている箇所があるので注意

–今後も性能向上、機能追加、新しい仕様に対応

Server Side JavaScript

– Avatar.js、Avatarは鋭意開発中

–ぜひFeedbackを!

Page 62: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.62

Appendix

Page 63: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.63

ソースコード

Shell 主としてjjsで利用

Compiler ソースからバイトコード (class) を生成

Scanner ソースからトークンを作成

Parser トークンからAST/IRを作成

IR スクリプトの要素

Codegen AST/IRからscript classのバイトコードを生成

Objects ランタイム要素 (Object、String、Number、Date、RegExp)

Scripts スクリプト用のコードを含むclass

Runtime ランタイムタスク処理

Linker JSR-292 (InvokeDynamic) に基づきランタイムで呼び出し先をバインド

Dynalink 最適なメソッドを検索(言語間で動作)

Page 64: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.64

Nashorn Documentshttp://wiki.openjdk.java.net/display/Nashorn/Nashorn+Documentation

Java Platform, Standard Edition Nashorn User's Guide

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nash

orn/

Scripting for the Java Platform

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/

Oracle Java Platform, Standard Edition Java Scripting Programmer's

Guide

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_

guide/

Page 65: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.65

Nashornhttp://openjdk.java.net/projects/nashorn/

OpenJDK wiki – Nashorn

https://wiki.openjdk.java.net/display/Nashorn/Main

Mailing List

[email protected]

Blogs

– Nashorn - JavaScript for the JVM

http://blogs.oracle.com/nashorn/

– Nashorn Japan

https://blogs.oracle.com/nashorn_ja/

Page 66: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.66

Avatar.js

Project Page

https://avatar-js.java.net/

Mailing List

[email protected]

Page 67: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.67

Avatar

Project Page

https://avatar.java.net/

Mailing List

[email protected]

Page 68: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.68

Page 69: Nashorn : JavaScript Running on Java VM (Japanese)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.69