nashorn: javascript running on java vm (english)

69
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 1 JavaScript Running On JavaVM: Nashorn NISHIKAWA, Akihiro Oracle Corporation Japan

Upload: akihiro-nishikawa

Post on 08-May-2015

1.985 views

Category:

Technology


1 download

DESCRIPTION

This slide was used at Java Day Tokyo 2014.

TRANSCRIPT

Page 1: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.1

JavaScript Running On JavaVM: Nashorn

NISHIKAWA, Akihiro

Oracle Corporation Japan

Page 2: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.2

The following is intended to outline our general product direction. It

is intended for information purposes only, and may not be

incorporated into any contract.

It is not a commitment to deliver any material, code, or functionality,

and should not be relied upon in making purchasing decisions. The

development, release, and timing of any features or functionality

described for Oracle’s products remains at the sole discretion of

Oracle.

Page 3: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.

Agenda Nashorn

Server Side JavaScript

Nashorn in the future

Page 4: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4

Nashorn

Page 5: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5

NashornJavaScript Engine running on Java VM

Page 6: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6

NashornJavaScript Engine running on Java VM

Page 7: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7

Backgrounds

Replacing Rhino

– Concerns about security, performance, and so on

Proof of Concept for InvokeDynamic (JSR-292)

Atwood's law

Any application that can be written in JavaScript will

eventually be written in JavaScript.

Page 8: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8

Scope of Project NashornJEP 174

ECMAScript-262 Edition 5.1 compliant

Support for javax.script (JSR 223) API

Interaction between Java and JavaScript

Newly introduced command line tool (jjs)

Page 9: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9

Java VM

Scripting Engine

(Nashorn)

Scripting API

(JSR-223)

JavaScript codeJava code

Other runtime

Other APIs jjs

Page 10: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10

$JAVA_HOME/bin/jjs

$JAVA_HOME/jre/lib/ext/nashorn.jar

Page 11: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11

ECMAScript-262 Edition 5.1 compliant

Page 12: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12

Out of scope of Project Nashorn

ECMAScript 6 (Harmony)

– Generators

– Destructuring assignment

– const, let, ...

DOM/CSS and related libraries

– jQuery, Prototype, Dojo, …

Browser API and browser emulator

– HTML5 canvas, HTML5 audio, WebGL, WebWorkers...

Page 13: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13

Examples

Page 14: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14

Invoke Nashorn from Java

ScriptEngineManager manager= new ScriptEngineManager();

ScriptEngine engine= manager.getEngineByName("nashorn");

engine.eval("print('hello world')");

engine.eval(new FileReader("hello.js"));

Page 15: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15

Invoke Nashorn from JavaInvoke script function from Java

engine.eval("function hello(name) {print('Hello, ' + name)}");

Invocable inv=(Invocable)engine;

Object obj=inv.invokeFunction("hello","Taro");

Page 16: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16

Invoke Nashorn from JavaImplement an interface with script function

engine.eval("function run(){

print('run() called’)

}");

Invocable inv =(Invocable)engine;

Runnable r=inv.getInterface(Runnable.class);

Thread th=new Threads(r);

th.start();

th.join();

Page 17: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17

Invoke Java/JavaFX from Nashorn

print(java.lang.System.currentTimeMillis());

jjs -fx ...

Page 18: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18

Nashorn for ScriptingAdditional feature for scripting use

-scripting option

Here document

Back quote

String Interpolation

...

Page 19: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19

Nashorn Extensions

Page 20: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20

Nashorn ExtensionsThe following topics are covered in this session.

How to get references of Java type

How to access properties of Java objects

Relationship among Lambda, SAM, and Script function

Scope and context

Page 21: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21

Java type

Page 22: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22

How to get references of Java typeExpression in Rhino (This expression is valid in Nashorn)

var hashmap=new java.util.HashMap();

Or

var HashMap=java.util.HashMap;var hashmap=new HashMap();

Page 23: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23

How to get references of Java typeRecommended expression in Nashorn

var HashMap=Java.type('java.util.HashMap');var hashmap=new HashMap();

Page 24: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.24

Java.typeClass or Package?

java.util.ArrayList

java.util.Arraylist

Page 25: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25

Java ArrayExpression in Rhino

var intArray=java.lang.reflect.Array.newInstance(

java.lang.Integer.TYPE, 5);

var Array=java.lang.reflect.Array;

var intClass=java.lang.Integer.TYPE;

var array=Array.newInstance(intClass, 5);

Page 26: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26

Java ArrayExpression in Nashorn

var intArray=new(Java.type("int[]"))(5);

var intArrayType=Java.type("int[]");var intArray=new intArrayType(5);

Page 27: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27

Access properties ofJava objects

Page 28: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.28

Using getter and setter

var HashMap=Java.type('java.util.HashMap');

var map=new HashMap();

map.put('size', 2);

print(map.get('size')); // 2

Page 29: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.29

Directly

var HashMap=Java.type('java.util.HashMap');

var map=new HashMap();

map.size=3;

print(map.size); // 3

print(map.size()); // 1

Page 30: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.30

Using Associative Array

var HashMap=Java.type('java.util.HashMap');

var map=new HashMap();

map['size']=4;

print(map['size']); // 4

print(map['size']()); // 1

Page 31: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.31

Lambda, SAM, and Script function

Page 32: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.32

Lambda, SAM, and Script functionNashorn converts a script function to a lambda object or any SAM

interface implementing object automatically.

var timer=new java.util.Timer();

timer.schedule(

function() { print('Tick') }, 0, 1000);

java.lang.Thread.sleep(5000);

timer.cancel();

Page 33: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.33

Lambda, SAM, and Script functionAny Java object which is an instance of lambda type can be treated like a

script function.

var JFunction=Java.type('java.util.function.Function');

var obj=new JFunction() {

// x->print(x*x)

apply: function(x) { print(x*x) }

}

print(typeof obj); //function

obj(9); // 81 like Script function

Page 34: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.34

Scope and Context

Page 35: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.35

Scope and Contextload and loadWithNewGlobal

load

– Load scripts in the same global scope.

– If same name variables are in the original scripts and

loaded scripts, Corruption of each variable may happen.

loadWithNewGlobal

– Load scripts in another global scope newly created.

– Even if same name variables are in the original scripts

and loaded scripts, Corruption of each variable may not

happen.

Page 36: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.36

Scope and ContextScriptContext contains one or more Bindings each associated each jsr223

"scope".ScriptContext ctx=new SimpleScriptContext();

ctx.setBindings(engine.createBindings(),ScriptContext.ENGINE_SCOPE);

Bindings engineScope=ctx.getBindings(ScriptContext.ENGINE_SCOPE);

engineScope.put("x", "world");

engine.eval("print(x)", ctx);

Page 37: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.37

Scope and ContextUsing with clause and JavaImporter

with(new JavaImporter(java.util, java.io)){

var map=new HashMap(); //java.util.HashMap

map.put("js", "javascript");

map.put("java", "java");

print(map);

....

}

Page 38: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.38

Others

Convert between Java array and JavaScript array

– Java.from

– Java.to

Extend Java class and access super-class

– Java.extend

– Java.super

and so on...

Page 39: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.39

Server Side JavaScript

Page 40: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.40

Java EE for Next Generation ApplicationsDeliver HTML5, Dynamic and Scalable Applications

Web

So

ckets

Avata

r

Page 41: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.41

Evolution of Web application architectureRequest-Response and Multi-page application

Java EE/JVM

Presentation

(Servlet/JSP)

Business

Logic

Backend

ConnectivityBrowser

Page 42: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.42

Evolution of Web application architectureUse Ajax (JavaScript)

Java EE/JVM

Connectivity(REST, SSE)

Presentation(Servlet/JSP, JSF)

Business

Logic

Backend

ConnectivityBrowser

JavaScript

Page 43: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.43

Modern Web application architectureRather connectivity than Presentation – Single Page Application

Java EE/JVM

Connectivity(WebSocket,

REST, SSE)

Presentation(Servlet/JSP, JSF)

Business

Logic

Backend

ConnectivityBrowser

View

Controller

JavaScript

Page 44: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.44

How about Node.js?

Page 45: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.45

Mobile-enabled existing services with Node.js

Node.js

JavaScript

REST

SSE

WebSocket

Browser

View

Controller

JavaScript

Page 46: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.46

How aboutNode.js on JVM?

Page 47: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.47

Mobile-enabled existing services with NodeNode running on Java VM

Java EE/JVM

Node

Server

Business

Logic

Backend

Connectivity

Client

JavaScriptBrowser

View

Controller

JavaScript

Page 48: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.48

Avatar.js

Page 49: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.49

Avatar.jsOld name is "node.jar"

Able to use almost all of modules available on Node.js

– Express, async, socket.io, ...

Some modules are required to modify.

– Automatically recognize modules downloaded with

npm.

Pros.

– Leverage node programming model

– Leverage existing Java assets, knowledge, and tools

Page 50: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.50

Avatar.js = Node + JavaLeverage Java technologies including Threads

JavaJavaScript

com.myOrg.myObj

java.util.SortedSet

java.lang.Thread

require('async')

postEvent

Node App

Page 51: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.51

Avatar

Page 52: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.52

AvatarServer side JavaScript services framework

Provide data transmission features via REST, WebSocket,

and Server Sent Event (SSE)

Leverage event-driven programming model and

programming modules of Node.js

Integrate with Enterprise Features (Java EE)

Page 53: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.53

*.html

*.js

*.css

HTTP

Application

Services

Avatar Modules

Node Modules

Avatar.js

Avatar Runtime

Avatar Compiler

Server Runtime (Java EE)

JDK 8 / Nashorn

Application

Views

REST/WebSocket/SSE

Avatar (Avatar EE)

Change

Notification

Data

Page 54: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.54

HTTP

REST/WebSocket/SSE

Avatar Compiler

Application

Views

*.html

*.js

*.css

Application

Services

Avatar Modules

Node Modules

Avatar.js

Avatar Runtime

Server Runtime (Java EE)

JDK 8 / Nashorn

Architecture (Server Side)

Change

Notification

Data

Page 55: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.55

Avatar Service

Java

JavaS

cri

pt

HTTP Load Balancer

Services

Shared State

Services Services

Change

Notification

Data

Page 56: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.56

Avatar Runtime

Server Runtime (Java EE)

Avatar Modules

Node Modules

Avatar.js

*.html

*.js

*.css

Application

Services

JDK 8 / Nashorn

Architecture (Client Side)

Change

Notification

Data

HTTP

REST/WebSocket/SSE

Application

Views

Avatar Compiler

Page 57: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.57

Avatar and Avatar.jsprogress steadily.

Page 58: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.58

Nashorn in the future

Page 59: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.59

Nashorn in the future

Bytecode Persistence (JEP 194)

http://openjdk.java.net/jeps/194

Optimistic Typing

Others

Page 60: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.60

Summary

Page 61: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.61

Key takeaways

Nashorn

– Tightly integrated with Java

– Pay attention to different expression when migrating

artifacts from Rhino to Nashorn

– Progress steadily (performance improvement, new

features, compliant against new standards)

Server Side JavaScript

– Avatar.js and Avatar are on-going.

– Please feedback!

Page 62: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.62

Appendix

Page 63: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.63

Source code walk through

Shell jjs uses mainly.

Compiler generates classes (bytecode) from sources.

Scanner Create tokens from sources.

Parser Create AST/IR from tokens.

IR Elements of scripts

Codegen Create script class bytecode from AST/IR

Objects Runtime elements (Object, String, Number, Date, RegExp)

Scripts Classes including codes for scripts

Runtime Runtime tasks

Linker Binds callees on runtime based on JSR-292 (InvokeDynamic)

Dynalink Searching for best methods (available across languages)

Page 64: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.64

Nashorn Documentshttp://wiki.openjdk.java.net/display/Nashorn/Nashorn+Documentation

Java Platform, Standard Edition Nashorn User's Guide

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nash

orn/

Scripting for the Java Platform

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/

Oracle Java Platform, Standard Edition Java Scripting Programmer's

Guide

http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_

guide/

Page 65: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.65

Nashornhttp://openjdk.java.net/projects/nashorn/

OpenJDK wiki – Nashorn

https://wiki.openjdk.java.net/display/Nashorn/Main

Mailing List

[email protected]

Blogs

– Nashorn - JavaScript for the JVM

http://blogs.oracle.com/nashorn/

– Nashorn Japan

https://blogs.oracle.com/nashorn_ja/

Page 66: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.66

Avatar.js

Project Page

https://avatar-js.java.net/

Mailing List

[email protected]

Page 67: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.67

Avatar

Project Page

https://avatar.java.net/

Mailing List

[email protected]

Page 68: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.68

Page 69: Nashorn: JavaScript Running on Java VM (English)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.69