nashorn in the future (english)
TRANSCRIPT
Nashorn intheFuture
Oracle Corporation JapanFusion Middleware Business UnitNISHIKAWA, Akihiro8, April, 2015
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.
2
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Agenda
What'sNashorn?
8u20
8u40
Inthefuture...
1
2
3
4
3
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Nashorn
• ReplacementforRhino– Forimprovingsecurityandperformance
• ProofofConceptforInvokeDynamic (JSR-292)
5
JavaScriptEngineintroducedinJava8 (JEP174)
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Nashorn
• ECMAScript-262Edition5.1
• javax.script (JSR223)API
• InteractionbetweenJavaandJavaScript
• Commandlinetool:jjs
JavaScriptEngineintroducedinJava8 (JEP174)
6
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Getstarted!• jjs– HelloWorld– LambdaExpression,Stream
• Scripting– Usertheoptionof“-scripting”– CallWebAPI
• JavaFX– Usertheoptionof“–fx”–WebView
7
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Lambda ExpressioninNashorn// Javaarray.stream().sorted(Comparator.naturalOrder()).forEach( t -> sortedArray.add(t) );
// Nashornarray.stream().sorted(Comparator.naturalOrder()).forEach(function(t) sortedArray.add(t));
8
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
CallfromJava(1/3)ScriptEngineManager manager =
new ScriptEngineManager();ScriptEngine engine =
manager.getEngineByName("nashorn");// Evaluationengine.eval("print('hello world')"); // hello worldengine.eval(new FileReader(“hello.js”)); // hello.js
9
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
CallfromJava(2/3)
engine.eval("function hello(name){print('Hello, ' + name); }");
Invocable inv=(Invocable)engine;
// Hello, TaroObject obj= inv.invokeFunction("hello","Taro");
10
InvokeJavaScriptfunction
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
CallfromJava(3/3)
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();
11
ImplementinterfacewithScriptfunction
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
8u20
• Focusedonsecurityimprovementandenhancementofunderlyingplatform– JIT– JDK
13
ReleasedinAugust,2014
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Replace“const”with“var”
• Default:false
• Itmightretireinthefuturesince8u40supportsconst.
14
--const-as-var=true|false
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
ForbidaccessJavaPackagesandClassesfromJavaScript
• Default:false
15
--no-java=true|false (-nj)
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
ForbidusingsyntaxagainstECMAScriptstandard
• AbletouseAPIExtensionsuchasJava.type• Disabledwhenusing-scripting
16
--no-syntax-extensions (-nse)
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
#sourceURL and @sourceURL
• Fordebuging use– //# sourceURL=myScript.js– @sourceURL=myScript.js
17
JDK-8032068:implement@sourceURL and#sourceURL directives
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
ClassCache
• Originallycompilationisrequiredevenifinvokingprevioususedscript.• Since8u20,previousevaluatedscriptisstoredincacheforreuse.
• Example– fromJavaengine.eval(new URLReader(myScriptURL));– fromJavaScriptload(url);
18
JDK-8021350:Sharescriptclassesbetweenthreads/globals withincontext
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
8u40
• Performanceimprovement–Optimistictyping(JEP196)– CodePersistence(JEP194)
• Security– ClassFilter(JEP202)
• PartialsupportforECMAScript6– Lexical-scopedvariablesandconstantdefinition(JEP203)
20
ImplementedJEP
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP196:OptimisticTyping
• Typeinferenceatcompilation(notatruntime)– Assumespecifictypeusedatoperationandmanipulationofarrayindex– Iftypeinferenceiswrong,fallbackandrecompilewithmorepessimisticassumption
• --optimistic-types=true|false (-ot)– Default:false
21
ForgeneratingbytecodelikeJava
int long
double
Object
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP196:OptimisticTyping
• nashorn.typeInfo.maxFiles–Maximum#offilestocachetypeinformation– if0 issettothisproperty,cachingisdisabled.
• nashorn.typeInfo.cacheDir– Directorywherefilesstoringcachedtypeinformationexist.•Windows: ${java.io.tmpdir}¥com.oracle.java.NashornTypeInfo• LinuxandSolaris: ~/.cache/com.oracle.java.NashornTypeInfo•MacOSX: ~/Library/Caches/com.oracle.java.NashornTypeInfo
22
Cachingresultoftypeinference
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP194:CodePersistence
• Cachecodeforre-usewithinthesameprocess.• Thishelpssmallermemoryusageandshorterstartuptime.
23
Performanceimprovementwithcachingcode
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP194:CodePersistence
• --class-cache-size=50 (-ccs)– Classcachesizeperglobalscope– Default:50
• --persistent-code-cache=true|false (-pcc)– Specifieswhetherornotoptimistictypeinformationaswellascompiledscriptsarepersistedtodisk– Default:false
24
Performanceimprovementwithcachingcode
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP194:CodePersistence
• Whereiscodecachestored?– Default:WheninvokingJavaScript,directorynamednashorn_code_cache iscreatedandcacheddataarestored.– Directorynameisconfigurable.• nashorn.persistent.code.cache
• Filestoredcacheddatacontainsnotonlyclassbytecodebutalsovariousmetadata.
25
Optimistictypeinformationisalsocachedtodisk
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Lazycompilation
• Compilemethodsondemand• Default:true– Before8u40,defaultsettingwasfalse duetoexperimentaloption.
26
--lazy-compilation=true|false
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP202:ClassFilter
• JEP202:Nashorn ClassFilterhttps://bugs.openjdk.java.net/browse/JDK-8043717
• Impementation isrequiredinJavaapplicationwhichusesJavaScript.– jdk.nashorn.api.scripting.ClassFilter
27
ForbidaccessingJavaclassesfromJavaScript
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
ClassFilter
// restrcting access to java.io.Fileimport jdk.nashorn.api.scripting.ClassFilter;
class MyFilter implements ClassFilter {@Overridepublic boolean exposeToScripts(String s) {
if (s.compareTo("java.io.File") == 0) return false;return true;
}}
28
Implementationexample
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP203:Lexically-scopedvariableandconstantdeclarations
• --language=es5|es6– Default:es5– --language=es6 ismandatorywhenusingconst andlet
30
Supportforkeyword“let”and“const”
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
// letlet a=2;
function f(x) {// a=2if(x) {
let a=42;}// a is still equal to 2
}
// varvar a=2;
function f(x) {// a is not undefinedif(x) {
var a=42;}// depending upon x, a is
equal to 42 or undefined}
31
let andvar
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
// 1) Syntax errorfunction f(x) {
const b=1;// Unable to assignb = 99;
}
// 2) Scope #1function f(x) {
const b=1;var z=b+1; //z=2
}//b is undefinedvar y=b+1;
32
// 3) Scope #2function f(x) {
const b=1;var z =b+1; //z=2
}//Able to declare bconst b = 10;
const
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
ServersideJavaScript...• Node.jscompatibleframeworkrunningonJVM– Nodyn• Vert.x+Dyn.JS+Netty
– Trieme• byApigee
• ReactiveProgramming– RxJS– React.JS
34
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.| 35
https://blogs.oracle.com/theaquarium/entry/project_avatar_updateAvatar...
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
IntheFuture
• Improvewithsteadywork– Shorterwarmuptime– Enhancement/improvementofOptimisticTypingandCodePersistence
• Java9– Fullysuport forECMAScript6(Java9)– ParserAPIforNashorn (JEP236)– JavaFlightRecorder• JavaScriptProfiler• TagforNashorn
...etc37
Java8u60,9,and...
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Improvewithsteadywork
• ImproveperformancewhenparsingJSON– PoorperformancewhenhandlingsmallJSONobjectsusingPropertyHashMap#findElement
• Re-writejdk.nashorn.internal.parser.JSONParser
38
Forexample...
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP236:ParserAPIforNashorn
• Objective– ParserAPIforrepresentingECMAScriptcodeasAST– Visitorpattern– Introducedtohideinternalimplementationpackage (jdk.nashorn.internal.ir)
• Notice– NotscriptlevelAPIbutJavaAPI
40
PublicAPIforECMAScript AST
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
JEP236:ParserAPIforNashorn
• AvailableafterJDK9b55• JavaDoc– https://bugs.openjdk.java.net/browse/JDK-8048176
41
jdk.nashorn.api.tree
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Keytakeaway• Manyfeaturesandimprovementhavebeenintroducedto8u20and8u40.
• Nashorn isbeingimproveddaybyday,ofcourse.
• Pleasegiveyourfeedbacktodevelopmentteam!
43
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
Nashorn Project
• Nashorn MailingList– [email protected]
• Nashorn Wiki– https://wiki.openjdk.java.net/display/Nashorn/Main
• DEVELOPER_README– http://hg.openjdk.java.net/jdk8u/jdk8u-dev/nashorn/file/tip/docs/DEVELOPER_README
• Nashorn - JavaScriptfortheJVM– http://blogs.oracle.com/nashorn/
44
http://openjdk.java.net/projects/nashorn/
Copyright©2015, Oracleand/oritsaffiliates.Allrightsreserved.|
SafeHarborStatementTheprecedingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.
45