Slide 1

Slide 1 text

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. |

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda What's Nashorn? 8u20 8u40 In the future... 1 2 3 4 3

Slide 4

Slide 4 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | What’s Nashorn? 4

Slide 5

Slide 5 text

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)

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 8u20 12

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 8u40 19

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Partial support for ECMAScript 6 29

Slide 30

Slide 30 text

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”

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Server Side JavaScript 33

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 35 https://blogs.oracle.com/theaquarium/entry/project_avatar_update Avatar...

Slide 36

Slide 36 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | In the future... 36

Slide 37

Slide 37 text

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 ...

Slide 38

Slide 38 text

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...

Slide 39

Slide 39 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Java Flight Recorder 39

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Takeaway 42

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Nashorn Project • Nashorn Mailing List – [email protected] • 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/

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Slide 49

Slide 49 text

No content