Nashorn - The New JavaScript Engine For JVM

Nashorn - The New JavaScript Engine For JVM

Nashorn is a new JavaScript engine written in pure Java. In this session you’ll get introduced to Nashorn and will get answers to the following questions:
- What does it bring to the Java world?
- What capabilities does it provide for the server and the client sides?
- How can you leverage Nashorn in the real-life projects?
You’ll also learn how can we use it today, even before Nashorn is integrated into public JDK8 builds.

Anton Moiseev

May 26, 2013

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

23 slides
  1. 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
  2. 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 ;)
  3. Nashorn vs Rhino ๏ Aging ๏ Security issues ๏ Slow

    ๏ Hard to leverage invokedynamic
  4. Nashorn vs V8 ๏ No data, but... ๏ Synthetic tests

    - V8 wins ๏ Typical usage - performance the same
  5. 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
  6. What you can do with Nashorn? ๏ REPL ๏ Shell

    scripting ๏ Build scripts (ant’s <script> tag) ๏ JavaFX ๏ Server-side (e.g. JavaScript template engines) ๏ Node.jar
  7. 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
  8. Restrictions ๏ Only ECMA-262 (ECMAScript 5.1) ๏ No typical browser

    APIs: ๏ HTML5 canvas ๏ HTML5 audio ๏ WebWorkers ๏ WebSockets ๏ WebGL ๏ etc.
  9. 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:
  10. 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:
  11. How to: Java from JS var MapEntry = java.util.Map.Entry; var

    MapEntry = Java.type("java.util.Map$Entry"); ๏ Inner classes:
  12. 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:
  13. 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:
  14. 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
  15. 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:
  16. Build scripts $ ant test Buildfile: /Users/anton/Projects/nashorn/build.xml test: [script] Hey!

    It's Nashorn! BUILD SUCCESSFUL Total time: 1 second <project> <target name="test"> <script language="nashorn"> <!-- or "javascript" --> print("Hey! It’s Nashorn!"); </script> </target> </project> ๏ File build.xml: ๏ How to invoke:

More Decks by Anton Moiseev

Other Decks in Programming