Java Script on the VM Rory Preddy Any

  • Slides: 39
Download presentation
Java. Script on the VM Rory Preddy

Java. Script on the VM Rory Preddy

“Any application that can be written in Java. Script, will eventually be written in

“Any application that can be written in Java. Script, will eventually be written in Java. Script” - Jeff Atwood (founder, stackoverflow. com)

What we going to discuss today • Rhino on JDK 1. 6 & 1.

What we going to discuss today • Rhino on JDK 1. 6 & 1. 7 • Nashorn on Java 8 • Avatar. js - aka Node via Java

In the Beginning - Java. Script • Developed by Netscape as a portable version

In the Beginning - Java. Script • Developed by Netscape as a portable version of Java offering a lightweight interpreted language • Developed under the name Mocha, officially released as Live. Script in September 1995, • Renamed Java. Script in December 1995 as a marketing ploy to leverage off Java's popularity • Even reserved Java’s keywords • Standardized by ECMA International

Java. Script/Java Timeline Nashorn and Avatar invoke. Dynamic Rhino (Separate download) 1. 3 1.

Java. Script/Java Timeline Nashorn and Avatar invoke. Dynamic Rhino (Separate download) 1. 3 1. 4 5. 0 JDK with Scripting and built in Rhino 6 7 8

Rhino

Rhino

In the Beginning - Rhino • In 1997 Netscape wanted a browser written fully

In the Beginning - Rhino • In 1997 Netscape wanted a browser written fully in Java and so it needed an implementation of Java. Script written in Java. • Code named "Javagator", project was canned but engine lived on • Compiles Java. Script code to Java bytecodes in either Interpreted or generated Java class files. • Suffers from: • Slow compile time • Memory leaks • Very unsecure!!!

Using Rhino - Command line and REPL • jrunscript -e "print('hello world')“ • jrunscript

Using Rhino - Command line and REPL • jrunscript -e "print('hello world')“ • jrunscript -l js -f hello. World. js • jrunscript js> print('Hello Worldn'); >>Hello World

Using Scripting API - background • “javax. script” package • Use any JSR-223 compliant

Using Scripting API - background • “javax. script” package • Use any JSR-223 compliant scripting language. • Java SE 6 & 7 include Scripting API and Mozilla Rhino as default engine • Extensive list of available engines: Python, Ruby, PHP, Groovy …

Script API Basics 1) Create a Script. Engine. Manager object. Script. Engine. Manager factory

Script API Basics 1) Create a Script. Engine. Manager object. Script. Engine. Manager factory = new Script. Engine. Manager(); 2) Get a Script. Engine object from the manager. Script. Engine engine = factory. get. Engine. By. Name("Java. Script"); 3) Evaluate script using the Script. Engine's eval methods. engine. eval("print('Hello, World')");

Invoking Script Functions and Methods String script = "function hello(name) {" + " return

Invoking Script Functions and Methods String script = "function hello(name) {" + " return 'Hello, ' + name; " + }"; engine. eval(script); Invocable inv = (Invocable) engine; String val = (String) invoke. Function("hello", "BBD!!"); System. out. println(val); >>Hello, BBD!!

Binding Java objects into script space Bindings bindings = new Simple. Bindings(); bindings. put("author",

Binding Java objects into script space Bindings bindings = new Simple. Bindings(); bindings. put("author", new Person("Rory", "Preddy", 34)); engine. eval("print('Name: ' + author. name)", bindings); >>Name: Rory

Callbacks … engine. put("cb", new Js. Callback()); engine. eval("println('Doing something in javascript here first');

Callbacks … engine. put("cb", new Js. Callback()); engine. eval("println('Doing something in javascript here first'); " + "cb. apply('bar'); "); } public void apply(String s){ System. out. println("Back in java code here: " + s); } … >>Doing something in javascript here first >>Back in java code here: bar

Implementing Java Interfaces by Scripts String script = "function run() { " + "println('run

Implementing Java Interfaces by Scripts String script = "function run() { " + "println('run called'); " + "}"; engine. eval(script); Runnable r = ((Invocable) engine). get. Interface(Runnable. class); new Thread(r). start(); >>run called

Java Interfaces in Java. Script var r = new java. lang. Runnable() { run:

Java Interfaces in Java. Script var r = new java. lang. Runnable() { run: function() { print("running. . . n"); } }; var th = new java. lang. Thread(r); th. start(); >>running…

Compiling engine. put("counter", 0); Compilable comp. Engine = (Compilable) engine; Compiled. Script script =

Compiling engine. put("counter", 0); Compilable comp. Engine = (Compilable) engine; Compiled. Script script = comp. Engine. compile( “ function count(){ counter=counter+1; return counter; }; count(); "); System. out. println(script. eval()); … >>1. 0 >>2. 0

Compiling with Rhino cat test. js java. lang. System. out. println("hi, mom!"); java org.

Compiling with Rhino cat test. js java. lang. System. out. println("hi, mom!"); java org. mozilla. javascript. tools. jsc. Main test. js ls *. class test. class java test >>hi, mom!

Optimization • Rhino optimizer not standard with JDK • Runtime optimization of compilation can

Optimization • Rhino optimizer not standard with JDK • Runtime optimization of compilation can be set from -1 to 9 -1 Interpretive mode is always used. No class files are generated, 0 Basic Compilation. No optimizations are performed. The compiler runs fastest in this mode, but the generated byte code is less efficient 1 -9 All optimizations are performed. Java Class Files generated

Nashorn

Nashorn

Invoke. Dynamic Bytecode operations that were available before JDK version 7: Opcode Usage Invokestatic

Invoke. Dynamic Bytecode operations that were available before JDK version 7: Opcode Usage Invokestatic For static methods Invokevirtual For non-private instance methods Invokespecial For private instance Invokeinterface For the receiver that implements the interface

Invoke. Static

Invoke. Static

The challenge • Java. Script is dynamic – Things can change at runtime •

The challenge • Java. Script is dynamic – Things can change at runtime • For example, what is the type of: var x = 500000; • x *= 500000; And now?

Enter JSR 292 • Based on an experimental project, the Da Vinci Machine •

Enter JSR 292 • Based on an experimental project, the Da Vinci Machine • Adds two new concepts to the JVM: – invokedynamic bytecode instruction – Method. Handles • The first bytecode extension since 1999!

Invoke. Dynamic

Invoke. Dynamic

Nashorn • NO COMILATION! - Compiles Java. Script directly into byte code • 20

Nashorn • NO COMILATION! - Compiles Java. Script directly into byte code • 20 x faster than uncompiled Rhino • Better typing • Smaller footprint • 100% compliant with ECMA-262 5. 1 • Standard JSR-223 syntax

Nashorn- Command Line > jjs> var x = 10, y = 20; jjs> x

Nashorn- Command Line > jjs> var x = 10, y = 20; jjs> x + y; >>30 Or > jjs example. js

Rhino VS Nashorn - Types … engine. eval("function test() { return 1; }; ");

Rhino VS Nashorn - Types … engine. eval("function test() { return 1; }; "); Object result = ((Invocable)engine). invoke. Function("test"); System. out. println(result. get. Class(). get. Name()); //Rhino output >>>java. lang. Double // Nashorn output >>>java. lang. Integer

Rhino VS Nashorn - Exceptions // in rhino. js !! try { java. lang.

Rhino VS Nashorn - Exceptions // in rhino. js !! try { java. lang. System. load. Library(null) } catch (e) { print(e instanceof java. lang. Null. Pointer. Exception) // false!! print(e. java. Exception instanceof java. lang. Null. Pointer. Exception) // } true

Lambdas in Nashorn var list = java. util. Arrays. as. List(1, 2, 3, 4,

Lambdas in Nashorn var list = java. util. Arrays. as. List(1, 2, 3, 4, 5, 6, 7, 8); var odd = list. stream(). filter(function(i) { return i % 2 == 0; }); odd. for. Each(function(i) { print(">>> " + i); }); >>> 2 >>> 4 >>> 6 >>> 8

Nashorn Vs Rhino

Nashorn Vs Rhino

Avatar. js

Avatar. js

Node

Node

Avatar. js (Node. jar)

Avatar. js (Node. jar)

Avatar. js (Node. jar) Supported module highlights • Implements the Node model and API

Avatar. js (Node. jar) Supported module highlights • Implements the Node model and API on the JVM • Supports most Node modules

Avatar+ EJB = Avatar EE

Avatar+ EJB = Avatar EE

Summary – Java. Script Why bother? Why you care • Server-side Java. Script •

Summary – Java. Script Why bother? Why you care • Server-side Java. Script • Leverage Existing Java. Script Libraries • Cross Platform scripting – Rhino runs on Android • Fast performance with Nashorn • Leverage new Node Libraries with Nashorn Why Oracle cares • Atwood’s law • Node. js – A real threat to Java’s server-side growth • Let developers handle typing with invokedynamic – Jruby, Jython

Demo

Demo

Demo overview • Java. Script coding Demo • Credit card validation • Avatar Demo

Demo overview • Java. Script coding Demo • Credit card validation • Avatar Demo • Show and tell • Fibonacci numbers • Interest calculation • Groovy Examples