Nashorn, JavaScript for JVM - Laskeyx

Download Report

Transcript Nashorn, JavaScript for JVM - Laskeyx

<Insert Picture Here>
Nashorn: JavaScript on the JDK
Jim Laskey
Multi-language Lead, Language/Tools Group, Java Platform
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.
2
Multi-language (Nashorn) Team
• Nashorn: JavaScript for the JVM
• Dynalink: Meta Object Protocol
• Node.jar: Nashorn implementation of Node.js
3
Multi-language (Nashorn) Team
4
Scripting on the JDK
• Scripting for Java
–
–
–
–
–
Access to a huge set of tools and libraries
Dynamic programming style
Content developer familiarity
Rapid prototyping
Modifiable post build
5
James M. Laskey, Esq.
EclipseCon 2013
200 Seaport Blvd
Boston, MA 02210
Formal
March 28, 2013
Attendee
EclipseCon 2013
200 Seaport Blvd
Boston, MA 02210
Subject: A personal introduction.
Dear Sir/Madam,
Please allow me this opportunity to offer this
introduction to the Nashorn JavaScript engine. This will not take much of
your time. Hopefully, you will gain enough insight to consider adding
Nashorn to the list of tools you use for your application development.
Respectfully,
James M. Laskey
cc: Eclipse community
6
Casual
'sup
7
Origins of Nashorn
• JavaFX script
– Adoption issue
• Rhino
– Showing age
– Security issues
– Not easily adapted to JSR-292 (InvokeDynamic)
• Other JVM languages
– Groovy, JRuby, Jython, PHP, Scala, …
8
Scripting in the JDK
Why JavaScript?
• Best interest of the JVM to have multiple languages
– Advances the technology
– Attracts new developers
• Choose one
– JavaScript has a Java-like syntax, but less formal
– Popularity of JavaScript (HTML5)
• Applications
– Client (Extending FX)
– Embedded/Mobile (light-weight)
– Server-side scripting (JSP, Business Process)
• Already using Rhino
9
Language Usage
http://langpop.corger.nl
Use on Github/StackOverflow Feb 2013
20
18
16
14
12
10
8
Percentage
6
4
2
0
10
Scripting in the JDK
Why Nashorn?
• Modify Rhino
– Pro: Reference for all things JavaScript
– Con: The engine would have to be rewritten for JSR 292
• Licensing another JavaScript
– Pro: Good performance
– Con: Second VM required, plus a VM to VM API
• Roll our own
–
–
–
–
Pro: We already have one of the best VMs in the industry
Pro: Can be tailored to Oracle’s needs
Pro: Validation of JSR 292
Con: Not a small effort
11
Nashorn JavaScript
What is Nashorn?
• Nashorn is an OpenJDK project to develop a version of
ECMAScript-262 to run on the OpenJDK JVM
– Fully passes test262 (http://test262.ecmascript.org)
– ES5.1 version of Nashorn will ship with JDK 8
– Tracking ES6 with changes included in JDK 8u release
• Nashorn (pronounced Naz-horn) is the German word for
rhinoceros. This is to pay homage to its ancestor, Rhino
and the rhinoceros impression on the cover of O'Reilly's
JavaScript Definitive Guide
• Nashorn is written from a clean code base, 100% Java
12
Rhino
13
Nashorn
14
Nashorn JavaScript Engine
Execution Model
• Compile to byte code
• Load and execute
• Compile on demand
– Type specialization optimization
• Dynamic linking via Dynalink
15
How Do I Use Nashorn?
Installation
As of JDK 8 b82 Nashorn is part of the prerelease
http://jdk8.java.net/download.html
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$JAVA_HOME/jre/bin:$PATH
16
How Do I Use Nashorn?
Build Your Own
http://hg.openjdk.java.net/jdk8/build/raw-file/tip/READMEbuilds.html
>
>
>
>
>
hg clone http://hg.openjdk.java.net/nashorn/jdk8/ nashorn
cd nashorn
sh get_source.sh
sh configure
make all
17
How Do You Use Nashorn?
Command Line
> jjs
jjs> var x = 10, y = 20;
jjs> x + y;
30
jjs> quit();
Or
> jjs example.js
18
How Do You Use Nashorn?
javax.script
import javax.script.*;
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
try {
e.eval("print('hello');");
} catch (final ScriptException se) {
...
}
19
Everything is Dynamic
• get, set, get index, set index, call, and new are all
implemented with invokeDynamic
• Most behaviour can change dynamically
20
JSR-292 and invokeDynamic
var x = receiver.property;
• Receiver can be a Nashorn object, a java object, a java
bean object, jRuby object, …
• Look up the best fit
21
Dynalink
• Dynalink is an invokedynamic-based high-level linking
and meta object protocol library. It enables languages on
the JVM to easily interoperate with plain Java objects
and each other.
• Plugins for Nashorn can be constructed for handling
special java classes. Ex. java.util.Map – treat maps like
objects.
• https://github.com/szegedi/dynalink
22
Dynalink and Nashorn
•
•
•
•
•
•
Able to search thru several alternatives
Type specific
Value conversions
Handle overloading
Default and special handling
Flexibility
23
Demos
24
Node.jar
•
•
•
•
•
Much of Node.js is native
Node.jar native is Java and runs on Nashorn
Open sourcing is in the works, stay tuned
0.8.15 working version
0.10.1 conversion in progress
25
Opportunities
• Development of Nashorn
• Adding features
• Performance
• Eclipse plugins for Nashorn development
• JavaScript aware, Nashorn extensions aware
• Debugging tools
• NashornJDI
26
Contacts
•
•
•
•
•
https://blogs.oracle.com/nashorn/
[email protected]
[email protected]
@wickund
#nashornjs (#nashorn)
27
The preceding 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.
28
29