Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Nashorn The New JavaScript Engine For JVM Anton Moiseev Software Developer, Farata Systems

Slide 3

Slide 3 text

What is Nashorn? ๏ JavaScript Engine for JVM ๏ Ultimate invokedynamic consumer ๏ 100% pure Java implementation ๏ 100% compiled to bytecode, no interpreter ๏ 100% ECMAScript 5.1 compliant

Slide 4

Slide 4 text

Why Nashorn? ๏ Oracle’s motivation: ๏ Validate JSR-292 (invokedynamic) ๏ Reveal performance issues

Slide 5

Slide 5 text

Why Nashorn? ๏ User’s benefits: ๏ Cross-pluggability ๏ Multi-threaded environment ๏ JVM gives: ๏ Multi-platform support ๏ Automatic memory management ๏ Code optimization ๏ Performance ๏ Everyone knows how to write JavaScript ;)

Slide 6

Slide 6 text

Nashorn vs Rhino ๏ Aging ๏ Security issues ๏ Slow ๏ Hard to leverage invokedynamic

Slide 7

Slide 7 text

Performance http://parleys.com/play/5148922b0364bc17fc56c90e/chapter0/about

Slide 8

Slide 8 text

Nashorn vs V8 ๏ No data, but... ๏ Synthetic tests - V8 wins ๏ Typical usage - performance the same

Slide 9

Slide 9 text

JSR-292: invokeDynamic ๏ Since Java 7, greatly improved in Java 8 ๏ For dynamic languages in general ๏ invokeDynamic ≈ function pointer ๏ Everything can be resolved in runtime ๏ Abstracts away bytecode

Slide 10

Slide 10 text

What you can do with Nashorn? ๏ REPL ๏ Shell scripting ๏ Build scripts (ant’s tag) ๏ JavaFX ๏ Server-side (e.g. JavaScript template engines) ๏ Node.jar

Slide 11

Slide 11 text

Features ๏ The only API is JSR-223: javax.scripting.* ๏ Java from JavaScript and vice versa ๏ Create and manipulate Java/JavaScript objects ๏ Extend Java classes ๏ Implement Java interfaces

Slide 12

Slide 12 text

Restrictions ๏ Only ECMA-262 (ECMAScript 5.1) ๏ No typical browser APIs: ๏ HTML5 canvas ๏ HTML5 audio ๏ WebWorkers ๏ WebSockets ๏ WebGL ๏ etc.

Slide 13

Slide 13 text

How to: Java from JS // create instance var list = new java.util.ArrayList(); // package shortcut var util = java.util; var list = new util.ArrayList(); // class shortcut var ArrayList = java.util.ArrayList; var list = new ArrayList(); // using Java.type() var ArrayList = Java.type('java.util.ArrayList'); var list = new ArrayList(); ๏ Object creation:

Slide 14

Slide 14 text

How to: Java from JS var Thread = Java.type('java.lang.Thread'), Runnable = Java.type('java.lang.Runnable'); var thread1 = new Thread(new Runnable({ run: function () { print('Hello from thread1!'); } })); var thread2 = new Thread(new Runnable( function () { print('Hello from thread2!'); } )); var thread3 = new Thread(function () { print('Hello from thread3!'); }); ๏ Interface implementation:

Slide 15

Slide 15 text

How to: Java from JS var MapEntry = java.util.Map.Entry; var MapEntry = Java.type("java.util.Map$Entry"); ๏ Inner classes:

Slide 16

Slide 16 text

How to: JS from Java package jeeconf.demo; import javax.script.*; public class Listing1 { public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine nashorn = manager.getEngineByName("Nashorn"); nashorn.eval( "function sayHi(){ print('Hey! It\\'s Nashorn!'); } sayHi();" ); } } ๏ Simple invocation:

Slide 17

Slide 17 text

How to: JS from Java package jeeconf.demo; import javax.script.*; public class Listing2 { public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine nashorn = manager.getEngineByName("Nashorn"); nashorn.eval(            "function  sayHi  (x)  {  print('Hey!  It\\'s  '  +  x  +  '!');  }"        );        Invocable  i  =  (Invocable)  nashorn;        i.invokeFunction("sayHi",  "Nashorn");        i.invokeFunction("sayHi",  "JEEConf"); } } ๏ Passing params and multiple invocation:

Slide 18

Slide 18 text

How to: JS from Java package jeeconf.demo; import javax.script.*; public class Listing2 { public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine nashorn = manager.getEngineByName("Nashorn"); nashorn.eval( "function sayHi (x) { print('Hey! It\\'s ' + x + '!'); }" + "function run () { sayHi('Nashorn'); }" ); Invocable i = (Invocable) nashorn; Runnable r = i.getInterface(Runnable.class); r.run(); } } ๏ Interface implementation

Slide 19

Slide 19 text

Shell scripting # Using jjs $ jjs ls.js # As executable script (chmod +x ls.js) $ ./ls.js #!/usr/local/bin/jjs -scripting var currentDir = new java.io.File('.'), allFiles = currentDir.list(); for (var i = 0; i < allFiles.length; i ++) { print(allFiles[i]); } ๏ File ls.js: ๏ How to invoke:

Slide 20

Slide 20 text

Build scripts $ ant test Buildfile: /Users/anton/Projects/nashorn/build.xml test: [script] Hey! It's Nashorn! BUILD SUCCESSFUL Total time: 1 second <!-- or "javascript" --> print("Hey! It’s Nashorn!"); ๏ File build.xml: ๏ How to invoke:

Slide 21

Slide 21 text

Demo

Slide 22

Slide 22 text

Q & A

Slide 23

Slide 23 text

Contacts Farata Systems: http://faratasystems.com Email: [email protected] Twitter: @antonmoiseev