Nashorn - JavaScript on the JVM - Akhil Arora

472 views

Published on

The Nashorn JavaScript Project started as a quiet spin-off from JavaFX in the fall of 2010. Since then, Nashorn has evolved into a significant part of Oracle's roadmap and is causing quite a bit of excitement in the OpenJDK community. This session will give the attendees some background on how the project evolved, information on how Nashorn works, how Nashorn ties in with Java development and what is planned for Nashorn in the future. The session will also touch on the Node.jar project and dynamic inter-language invocation protocols via Dynalink.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
472
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Nashorn - JavaScript on the JVM - Akhil Arora

  1. 1. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 131
  2. 2. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 132Nashorn:JavaScript on the JVMAkhil AroraSoftware EngineerJava Platform Language and ToolsOracle
  3. 3. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.3The following is intended to outline our general product direction. It is intendedfor information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and shouldnot be relied upon in making purchasing decisions. The development, release,and timing of any features or functionality described for Oracle’s productsremains at the sole discretion of Oracle.
  4. 4. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.4Agenda●Nashorn: A replacement for Rhino●Using Nashorn●Demos
  5. 5. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.5Introducing Nashorn●Nashorn is an OpenJDK project to develop a version ofECMAScript-262 to run on the OpenJDK JVM●Passes test262 test suite at 100% (http://test262.ecmascript.org)●Will support ECMAScript 5.1 when it ships with JDK8●Tracking changes in ECMAScript6 to include in JDK 8uX releases●Nashorn (prononounced Naz-horn) is the German word forrhinoceros. This is to pay homage to its ancestor Rhino, and therhinoceros impression on the cover of OReillys JavaScript DefinitiveGuide●Nashorn is written from a clean code base, 100% Java
  6. 6. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.6Rhino
  7. 7. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.7Nashorn
  8. 8. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.8Getting Nashorn●Nashorn is part of JDK8 Early Access as of Build b82●https://jdk8.java.net/download.html
  9. 9. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.9Using Nashorn from the command-line$ jjsjjs> var x=10, y=20jjs> x+y30jjs> quit()jjs [options] script -- script-argsoptions:-Dkey=value-doe (-dump-on-error) – dump full stack on error-J-Xmx2g (JVM options)
  10. 10. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.10Java Scripting API: Evaluate a Stringimport javax.script.*;public class EvalScript {   public static void main(String[] args) throws Exception {        ScriptEngineManager factory = new ScriptEngineManager();      ScriptEngine engine = factory.getEngineByName("nashorn");      engine.eval("print(Hello JavaOne)");   }}
  11. 11. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.11Java Scripting API: Evaluate a Fileimport javax.script.*;public class EvalScript {   public static void main(String[] args) throws Exception {        ScriptEngineManager factory = new ScriptEngineManager();      ScriptEngine engine = factory.getEngineByName("nashorn");      engine.eval(new java.io.FileReader(args[0]));   }}
  12. 12. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.12Java Scripting API: Set a Variableimport javax.script.*;public class EvalScript {   public static void main(String[] args) throws Exception {        ScriptEngineManager factory = new ScriptEngineManager();      ScriptEngine engine = factory.getEngineByName("nashorn");      engine.put("message", "Hello JavaOne!");      engine.eval("print(message)");   }}
  13. 13. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.13Java Scripting API: Invoking a Functionimport javax.script.*;public class EvalScript {   public static void main(String[] args) throws Exception {        ScriptEngineManager factory = new ScriptEngineManager();      ScriptEngine engine = factory.getEngineByName("nashorn");      engine.eval("function greet(msg) { print(msg); }");      Invocable inv = (Invocable) engine;      inv.invokeFunction("greet", "Hello JavaOne!");   }}
  14. 14. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.14Java Scripting API: Implementing an Interfaceimport javax.script.*;public class EvalScript {   public static void main(String[] args) throws Exception {        ScriptEngineManager factory = new ScriptEngineManager();      ScriptEngine engine = factory.getEngineByName("nashorn");      engine.eval("function run() { print(Hello JavaOne); }");      Invocable inv = (Invocable) engine;      Runnable r = inv.getInterface(Runnable.class);      new Thread(r).start();   }}
  15. 15. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.15Accessing Java ClassesJava classes are directly accessible from JavaScriptvar map = new java.util.HashMap();map.put("message", "Hello JavaOne");print(map.get("message"));
  16. 16. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.16Accessing Java Classesvar intType = Java.type("int");var stringArrayType = Java.type("java.lang.String[]");var int2DArrayType = Java.type("int[][]");var ArrayList = Java.type("java.util.ArrayList");var anArrayList = new ArrayList();var anArrayListWithSize = new ArrayList(16);
  17. 17. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.17Creating Java Arraysvar StringArray = Java.type("java.lang.String[]");var a = new StringArray(5);a[0] = "Hello JavaOne!";print(a[0]);
  18. 18. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.18Converting JavaScript Arrays to Javavar array = ["Hello", "JavaOne"];var javaArray = Java.to(jsArray,                               "java.lang.String[]");print(javaArray[0], javaArray[1]);
  19. 19. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.19Extending a Java Classvar TimerTask =  Java.type("java.util.TimerTask")var task = new TimerTask({    run: function() {        print("Hello JavaOne!");    }});
  20. 20. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.20Extending a Java ClassPass just a function for „single abstract method“ (SAM) types!var TimerTask =  Java.type("java.util.TimerTask")var task = new TimerTask(function() print("Hello JavaOne!"););
  21. 21. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.21Java Beans as JavaScript propertiesjjs> var date = new java.util.Date();jjs> date.getYear() + 1900;2013jjs> date.year + 1900;2013jjs> date.year = 2014­1900;114jjs> date.year + 1900;2014
  22. 22. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.22Accessing Java Collections from JavaScriptjjs> var ArrayList = Java.type("java.util.ArrayList");jjs> var al = new ArrayList();jjs> al.add("a");jjs> al.add("b");jjs> for each (var i in al) print(i);ab
  23. 23. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.23Accessing Java Maps from JavaScriptjjs> var HashMap = Java.type("java.util.HashMap");jjs> var hm = new HashMap();jjs> hm.put("age", 40);jjs> hm["age"];40jjs> for each (var i in hm.values()) print(i);40jjs> for each (var i in hm.keySet()) print(i);age
  24. 24. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.24Scripting JavaFXload("fx:base.js");load("fx:controls.js");load("fx:graphics.js");function start(stage) {   stage.title = "Hello World!";   var button = new Button();   button.text = "Say Hello";   button.onAction = function() print("Hello JavaOne!");   var root = new StackPane();   root.children.add(button);   stage.scene = new Scene(root, 300, 250);   stage.show();}
  25. 25. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.25More Scripting●Shebang – sample.js#!/usr/bin/jjsprint(arguments.join(","));$ chmod +x sample.js$ sample.js – a b ca, b, c●Templates ­ ${var}$ jjs ­scriptingjjs> var date = Date();jjs> "date: ${date}"Date: Wed Jun 05 2013 12:05:50 GMT­0700 (PDT)●Heredoc – var message <<< EOFmulti­line messageEOF
  26. 26. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.26Special variables and global functions●$ARG (or arguments)jjs – a b cjjs> $ARG[1]b●$ENV        // current env variables●$EXEC       // fork sub­processes●print()     // write to stdout●quit()      // exit●readLine()  // read one line of input from stdin●readFully() // reads the contents of a file
  27. 27. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.27ResourcesProject home - openjdk.java.net/projects/nashornWiki - wiki.openjdk.java.net/display/NashornNashorn blog - blogs.oracle.com/nashornMailing list - nashorn-dev@openjdk.java.net
  28. 28. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.28Demo: Scripting JavaFX●a JavaFX application written entirely in JavaScript
  29. 29. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.29Demo: Node powered by Nashorn●Server-side JavaScript Framework●Uses Nashorn as the JavaScript engine●Runs Node.js applications on the JVM●Offers seamless interoperability with Java●Reuse all your existing Java libraries in Node applications●Write server-side enterprise applications in JavaScript
  30. 30. Copyright © 2013, Oracle and/or its affiliates. All rights reserved.30The preceding is intended to outline our general product direction. It is intendedfor information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and shouldnot be relied upon in making purchasing decisions. The development, release,and timing of any features or functionality described for Oracle’s productsremains at the sole discretion of Oracle.
  31. 31. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1331

×