Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will...

39
Rory Preddy JavaScript on the VM

Transcript of Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will...

Page 1: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Rory Preddy

JavaScript on the VM

Page 2: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

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

Page 3: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,
Page 4: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

What we going to discuss today

• Rhino on JDK 1.6 & 1.7

• Nashorn on Java 8

• Avatar.js - aka Node via Java

Page 5: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

In the Beginning - JavaScript

• Developed by Netscape as a portable version of Java offering a lightweight interpreted language

• Developed under the name Mocha, officially released as LiveScript in September 1995,

• Renamed JavaScript in December 1995 as a marketing ploy to leverage off Java's popularity

• Even reserved Java’s keywords

• Standardized by ECMA International

Page 6: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

JavaScript/Java Timeline

1.3 1.4 5.0 6 7 8

Rhino(Separate download)

invokeDynamic

Nashorn and Avatar

JDK with Scripting and built in Rhino

Page 7: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Rhino

Page 8: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

In the Beginning - Rhino

• In 1997 Netscape wanted a browser written fully in Java and so it needed an implementation of JavaScript written in Java.

• Code named "Javagator", project was canned but engine lived on

• Compiles JavaScript code to Java bytecodes in either Interpreted or generated Java class files.

• Suffers from:• Slow compile time• Memory leaks• Very unsecure!!!

Page 9: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Using Rhino - Command line and REPL

• jrunscript -e "print('hello world')“

• jrunscript -l js -f helloWorld.js

• jrunscriptjs> print('Hello World\n');>>Hello World

Page 10: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

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 …

Page 11: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Script API Basics

1) Create a ScriptEngineManager object.

ScriptEngineManager factory = new ScriptEngineManager();

2) Get a ScriptEngine object from the manager.

ScriptEngine engine = factory.getEngineByName("JavaScript");

3) Evaluate script using the ScriptEngine's eval methods.

engine.eval("print('Hello, World')");

Page 12: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Invoking Script Functions and Methods

String script = "function hello(name) {" + " return 'Hello, ' +

name; " + }";

engine.eval(script);

Invocable inv = (Invocable) engine; String val = (String) inv.invokeFunction("hello", "BBD!!"); System.out.println(val);

>>Hello, BBD!!

Page 13: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Binding Java objects into script space

Bindings bindings = new SimpleBindings();

bindings.put("author", new Person("Rory", "Preddy", 34));

engine.eval("print('Name:' + author.name)", bindings);

>>Name: Rory

Page 14: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Callbacks

… engine.put("cb", new JsCallback()); 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

Page 15: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Implementing Java Interfaces by Scripts

String script = "function run() { " + "println('run called'); " + "}";engine.eval(script);

Runnable r = ((Invocable) engine).getInterface(Runnable.class);

new Thread(r).start();

>>run called

Page 16: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Java Interfaces in JavaScript

var r = new java.lang.Runnable() { run: function() { print("running...\n"); }};

var th = new java.lang.Thread(r);th.start();

>>running…

Page 17: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Compiling

engine.put("counter", 0);Compilable compEngine = (Compilable) engine; CompiledScript script = compEngine.compile(“ function count(){

counter=counter+1; return counter;};

count();");System.out.println(script.eval());System.out.println(script.eval());…

>>1.0>>2.0

Page 18: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

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!

Page 19: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Optimization

-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

• Rhino optimizer not standard with JDK

• Runtime optimization of compilation can be set from -1 to 9

Page 20: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Nashorn

Page 21: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

InvokeDynamic

Opcode Usage

Invokestatic For static methods

Invokevirtual For non-private instance methods

Invokespecial For private instance

Invokeinterface

For the receiver that implements the interface

Bytecode operations that were available before JDK version 7:

Page 22: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

InvokeStatic

Page 23: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

• JavaScript is dynamic– Things can change at runtime

• For example, what is the type of:var x = 500000;

• x *= 500000; And now?

The challenge

Page 24: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

• Based on an experimental project, the Da Vinci Machine

• Adds two new concepts to the JVM:– invokedynamic bytecode instruction– MethodHandles

• The first bytecode extension since 1999!

Enter JSR 292

Page 25: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

InvokeDynamic

Page 26: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Nashorn

• NO COMILATION! - Compiles JavaScript directly into byte code

• 20x faster than uncompiled Rhino• Better typing• Smaller footprint• 100% compliant with ECMA-262 5.1 • Standard JSR-223 syntax

Page 27: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

> jjsjjs> var x = 10, y = 20;jjs> x + y;

>>30

Or

> jjs example.js

Nashorn- Command Line

Page 28: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Rhino VS Nashorn - Types

…engine.eval("function test() { return 1; };");Object result = ((Invocable)engine).invokeFunction("test");System.out.println(result.getClass().getName());

//Rhino output>>>java.lang.Double

// Nashorn output>>>java.lang.Integer

Page 29: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Rhino VS Nashorn - Exceptions

// in rhino.js !!try { java.lang.System.loadLibrary(null)} catch (e) {

print(e instanceof java.lang.NullPointerException)

// false!!

print(e.javaException instanceof java.lang.NullPointerException)

// true}

Page 30: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Lambdas in Nashorn

var list = java.util.Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);var odd = list.stream().filter(function(i) { return i % 2 == 0;});odd.forEach(function(i) { print(">>> " + i);});

>>> 2>>> 4>>> 6>>> 8

Page 31: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Nashorn Vs Rhino

Page 32: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Avatar.js

Page 33: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Node

Page 34: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Avatar.js (Node.jar)

Page 35: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

• Implements the Node model and API on the JVM

• Supports most Node modules

Supported module highlights

Avatar.js (Node.jar)

Page 36: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Avatar+ EJB = Avatar EE

Page 37: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Why you care

• Server-side JavaScript • Leverage Existing

JavaScript 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

Summary – JavaScript Why bother?

Page 38: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Demo

Page 39: Rory Preddy JavaScript on the VM. “Any application that can be written in JavaScript, will eventually be written in JavaScript” - Jeff Atwood (founder,

Demo overview

• JavaScript coding Demo• Credit card validation

• Avatar Demo

• Show and tell• Fibonacci numbers• Interest calculation• Groovy Examples