Re: ES6 from Java, using Nashorn

2018-01-19 Thread Hannes Wallnöfer
Hi Olivier,

Support for ECMAScript 6 is quite incomplete in Nashorn unfortunately. We only 
support a small set of ES6 features in JDK 9:

• Template strings
• let, const, and block scope
• Iterators and for..of loops
• Map, Set, WeakMap, and WeakSet
• Symbols
• Binary and octal literals

„require“ on the other hand is not part of ES6. It was created as a way to 
implement modules in the pre-ES6 world. It can be implemented in script and 
there are implementations out there, so this is probably the most promising 
approach to use modules in Nashorn.

Hannes

> Am 08.01.2018 um 17:33 schrieb Olivier :
> 
> Hi experts,
> I want to run ES6 scripts from Java, using Nashorn.
> I have scripts involving 'let', 'const', my Java code is happy with it, I 
> just need to use a property "nashorn.args" set to "--language=es6".
> 
> Now, I'd like to move forward, use imports and modules.
> I have one script like this (modules.01.js):
> 
> function displayMessage() {
> console.log("Hello JS World!");
> };
> 
> export default displayMessage;
> 
> I want to consume it from this (modules.consume.js):
> 
> import displayMessagefrom './modules.01';
> 
> displayMessage();
> 
> But this is where I have a question:
> My engine.eval("load('./modules.consume.js');"); complains about the "import" 
> statement.
> In the transpiled version, it complains about the "require" statement.
> 
> I must be missing something... I attach my code, in case it's needed.
> 
> Any idea, pointer, comment, help, etc, most welcome,
> Thank you!
> - Olivier
> 
> 
> 
> 



ES6 from Java, using Nashorn

2018-01-19 Thread Olivier

Hi experts,
I want to run ES6 scripts from Java, using Nashorn.
I have scripts involving 'let', 'const', my Java code is happy with it, 
I just need to use a property "nashorn.args" set to "--language=es6".


Now, I'd like to move forward, use imports and modules.
I have one script like this (modules.01.js):

function displayMessage() {
 console.log("Hello JS World!");
};

export default displayMessage;

I want to consume it from this (modules.consume.js):

import displayMessagefrom './modules.01';

displayMessage();

But this is where I have a question:
My engine.eval("load('./modules.consume.js');"); complains about the "import" 
statement.
In the transpiled version, it complains about the "require" statement.

I must be missing something... I attach my code, in case it's needed.

Any idea, pointer, comment, help, etc, most welcome,
Thank you!
- Olivier



package scripting;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.stream.Collectors;

public class Executor {
private final static String SCRIPT_TO_RUN = "es6" + File.separator + 
"out" + File.separator + "modules.consume.js"; // Transpiled
//private final static String SCRIPT_TO_RUN = "es6" + File.separator + 
"modules.consume.js"; // Not transpiled

private final static String NASHORN_ARGS = "nashorn.args";
private final static String ES_6 = "--language=es6";


public static void main(String... args) throws Exception {

System.out.println(String.format("Running from [%s]", 
System.getProperty("user.dir")));

String script =  SCRIPT_TO_RUN;
if (args.length > 0) {
script = args[0];
}

System.setProperty(NASHORN_ARGS, ES_6);

ScriptEngineManager factory = new ScriptEngineManager();
List engineFactories = 
factory.getEngineFactories();
System.out.println(String.format("%s factory(ies).", 
engineFactories.size()));
engineFactories.stream().forEach(ef -> {
System.out.println(String.format("%s (%s)",
ef.getEngineName(),

ef.getNames().stream().collect(Collectors.joining(", ";
});

ScriptEngine engine = factory.getEngineByName("nashorn");
FileReader reader = new FileReader(script);
try {
engine.eval(reader);
} catch (Exception ex) {
System.err.println("Ooops:" + ex.toString());
} finally {
reader.close();
}
// Another approach
try {
String command = String.format("load('%s');", script);
System.out.println(String.format("Executing [%s]", 
command));
engine.eval(command);
} catch (Exception ex) {
ex.printStackTrace();
}

engine.eval("print('Bye now.');");
}
}