ES6 support?

118 views
Skip to first unread message

Mike Stay

unread,
Mar 29, 2018, 11:00:29 AM3/29/18
to ScriptCraft - Scripting Minecraft
Nashorn has ES6 support, but usually it's enabled with a command-line flag.  I suspect there's a way to set the flags when the engine itself is instantiated, but I can't find where that happens.  Any ideas?

Captain Starbuck

unread,
Mar 31, 2018, 12:49:30 AM3/31/18
to ScriptCraft - Scripting Minecraft
Does this work?

java -Xmx1024M -Xms1024M --language=es6 -jar minecraft_server.jar

Captain Starbuck

unread,
Mar 31, 2018, 3:15:34 AM3/31/18
to ScriptCraft - Scripting Minecraft
After posting my other note, it occurred to me that you're probably trying to use ES6 on a system where a user can add plugins and click a button to restart, without access to the command-line. Admin UI's like that generally include a textbox for command-line switches. But OK, you asked for something beyond that, so....

There might be a couple ways to do this.

First, define arguments, and then just start the engine as normal:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
...
System.setProperty("nashorn.args", "--language=es6");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");

That will append options at the end of the command-line that starts the engine. I've seen that syntax in forums.

The args may also be set like this (not sure):

Properties properties = System.getProperties();
properties
.setProperty("nashorn.args", "--language=es6");

That may not work if permissions are restricting our ability to set properties on System. So...


A second approach is to pass the arguments into a factory method that returns an engine:

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
...
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new String[]{"-strict","--language=es6"});

For some docs on those switches:
(http://hg.openjdk.java.net/jdk9/jdk9/nashorn/file/17cc754c8936/docs/DEVELOPER_README)
Note that is v9.

The thing is, I don't see the --language option mentioned explicitly in the above reference. And I don't see any references to ES6. Your challenge is to see if this works, and now with the above examples, to see if you can find any doc on it. XD

Here's a source of info that shows the factory approach.
(https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes)

And here is another source that shows both the Properties approach and the factory being used together.
(http://hg.openjdk.java.net/jdk9/dev/nashorn/rev/d6b5c2c6c1d0#l5.113)

For anyone who wants to use ScriptCraftJS, the information in the OpenJDK wiki is like an Enchanted Diamond Axe if you want to conquer Nashorn. Start here:
(https://wiki.openjdk.java.net/display/Nashorn/Nashorn+Documentation)

And if you want to see great JavaScript with Java examples, here are samples right from the JDK10 source!
(http://hg.openjdk.java.net/jdk10/jdk10/nashorn/file/3397ed166912/samples)


This might be another way to get an engine:

for (ScriptEngineFactory afactory : new ScriptEngineManager().getEngineFactories()) {
 
System.out.println(afactory.getLanguageVersion());
}

I'm guessing you'll get two in v9. Use the info there to save the ES6 instance if you find it.
I mention this because I can't find a single reference at OpenJDK or Oracle for "ES6", not even in JDK10 info:
(https://docs.oracle.com/javase/10/scripting/toc.htm)
It's possible that we automatically get whatever ES6 abilities they have built-in over ES5. It might all be "nashorn", there might not be a type "es6".
With the above code, you can iterate over whatever is available in the factory to get the best engine. That means, after all of the above about setting properties, that we may not need to specify the 'language' explicitly at all.

I'm eager to know if any of this is helpful.
Regards.

Mike Stay

unread,
Mar 31, 2018, 11:55:00 AM3/31/18
to ScriptCraft - Scripting Minecraft
Here's the flag:
https://www.oracle.com/corporate/features/nashorn-javascript-engine-jdk9.html

This doesn't work because it's not a flag for the JVM, it's a flag for Nashorn:
java -Xmx1024M -Xms1024M --language=es6 -jar minecraft_server.jar

This doesn't work because the JVM doesn't pass on any args it doesn't use:
java -Xmx1024M -Xms1024M -jar minecraft_server.jar -- -language=ES6


This part looks exactly like what I'm looking for:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
...
System.setProperty("nashorn.args", "--language=es6");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn");

And it looks like that setProperty line should appear just before this line in the source:

Thanks!

Captain Starbuck

unread,
Mar 31, 2018, 4:06:25 PM3/31/18
to ScriptCraft - Scripting Minecraft
Yeah, Jim Laskey didn't go further with that info and it seems neither has anyone else. :)
BTW, that page is just an excerpt from his full article, and the link at the bottom of the excerpt doesn't quite get us to the full article. So for anyone who wants a lot more info about Nashorn in JDK9, this is a direct link to the full article:
(http://www.javamagazine.mozaicreader.com/JulyAug2017/Default/34/0/3705122#&pageSet=34&page=0&contentItem=3705122)

Please note that my "Bad-Zombie" fork of the ScriptCraftJS repo is now behind Walter's live repo, so that should be used as the current reference. I made a lot of changes in my repo and it was a bit too much for Walter to absorb in one shot, but then I never got back to submitting small, focused changes back to the core. The process of re-incorporating some of those (what I believe are) changes of some value will begin soon.

Still anxious to know how this works for you. If you can do something a simple as define a Constant then this is the way to go.

Good luck!

Jonathan Perret

unread,
Mar 31, 2018, 5:52:03 PM3/31/18
to ScriptCraft - Scripting Minecraft
Hi Mike, this sounded familiar so I went to look in this group's archives. Looks like a bit over a year ago (https://groups.google.com/forum/#!msg/scriptcraft---scripting-minecraft/UVE-5T3rC-U/TScy3vnZBQAJ) I suggested you use

java -Dnashorn.args=--language=es6 minecraft_server.jar

to enable Nashorn's ES6 features. Didn't that work for you?

Regards,
Jonathan

Mike Stay

unread,
Apr 1, 2018, 6:41:37 PM4/1/18
to ScriptCraft - Scripting Minecraft
Oops!  You're right.  I'd forgotten---though now that I try it, even the features described as being supported in that article I quoted don't work.  The only things that work, as far as I can tell, are let and const. So maybe the reason I forgot is that ES6 just doesn't work under Nashorn.  Sorry for the noise!

Jonathan Perret

unread,
Apr 2, 2018, 7:17:09 AM4/2/18
to ScriptCraft - Scripting Minecraft
No worries. The referenced article talks about JDK 9 though, are you sure you're running that? Make it a recent update too, as they tend to slip additional features in updates.

Support for ES6 features in Nashorn is still incomplete (even as of JDK 10 — as far as I can tell that didn't improve upon JDK 9 on that front) but there's definitely more than let and const. Most notably arrow functions and for…of seem to work. Still no support for classes though. Confusingly, the built-in "JavaScript Parsing API" supports the class syntax, as shown in the article, but the implementation doesn't follow.

Regards,
Jonathan
Reply all
Reply to author
Forward
0 new messages