| 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
| 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)
| 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
| Forbid using syntax against ECMAScript standard • Able to use API Extension such as Java.type • Disabled when using -scripting 16 --no-syntax-extensions (-nse)
| 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
| 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
| 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
| 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
| 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
| 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
| Lazy compilation • Compile methods on demand • Default : true – Before 8u40, default setting was false due to experimental option. 26 --lazy-compilation=true|false
| 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
| 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”
| // 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
| // 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
| 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 ...
| 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...
| 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
| 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
| 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