Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use graaljs ? Is there a place where to get a .jar file/files?

I use Java 8 and I use the default JavaScript Engine (Nashorn).

I would like to see how it compares with the 'highly hyped' GRAAL JS. See:

  • https://github.com/graalvm/graaljs
  • https://www.graalvm.org/

particularly because I hear they want to deprecate nashorn:

  • http://openjdk.java.net/jeps/335

Does anybody know how to get access (easily) to graaljs ? I was hoping to find a pom.xml or a place where to download a jar file but not luck

like image 643
Zo72 Avatar asked Jun 07 '18 09:06

Zo72


1 Answers

At the moment there are no pre-built jars of Graal.js available outside of GraalVM. To run it on an other JDK, you can extract the jars from GraalVM or build it like this:

$ git clone [email protected]:graalvm/graaljs.git
$ git clone [email protected]:graalvm/mx.git
$ export PATH=$PWD/mx:$PATH
$ export JAVA_HOME=/usr/java/jdk1.8.0_161
$ cd graaljs/graal-js
$ mx build

Note that it built fine with JDK 8. It also runs on JDK 8:

$ mx js
> typeof([] + 1)
string
>

The shell works, Ctrl+D exits it. The -v option in the previous command line shows how it launches it:

$ mx -v js
...
env JAVA_HOME=/usr/java/jdk1.8.0_161 ... \
/usr/java/jdk1.8.0_161/bin/java -d64 -cp /tmp/graal-js/graal/sdk/mxbuild/dists/graal-sdk.jar:/tmp/graal-js/graal/truffle/mxbuild/dists/truffle-api.jar:/tmp/graal-js/graal/tools/mxbuild/dists/truffle-profiler.jar:/tmp/graal-js/graal/tools/mxbuild/dists/chromeinspector.jar:/tmp/graal-js/graal/sdk/mxbuild/dists/launcher-common.jar:/tmp/graal-js/graaljs/graal-js/mxbuild/dists/graaljs-launcher.jar:/tmp/graal-js/graal/regex/mxbuild/dists/tregex.jar:/home/gmdubosc/.mx/cache/ASM_DEBUG_ALL_702b8525fcf81454235e5e2fa2a35f15ffc0ec7e.jar:/home/gmdubosc/.mx/cache/ICU4J_6f06e820cf4c8968bbbaae66ae0b33f6a256b57f.jar:/tmp/graal-js/graaljs/graal-js/mxbuild/dists/graaljs.jar -Dtruffle.js.BindProgramResult=false -Xms2g -Xmx2g -Xss16m com.oracle.truffle.js.shell.JSLauncher

So it puts those jars on the classpath:

  • /tmp/graal-js/graal/sdk/mxbuild/dists/graal-sdk.jar
  • /tmp/graal-js/graal/truffle/mxbuild/dists/truffle-api.jar
  • /tmp/graal-js/graal/tools/mxbuild/dists/truffle-profiler.jar
  • /tmp/graal-js/graal/tools/mxbuild/dists/chromeinspector.jar
  • /tmp/graal-js/graal/sdk/mxbuild/dists/launcher-common.jar
  • /tmp/graal-js/graaljs/graal-js/mxbuild/dists/graaljs-launcher.jar
  • /tmp/graal-js/graal/regex/mxbuild/dists/tregex.jar
  • /home/gmdubosc/.mx/cache/ASM_DEBUG_ALL_702b8525fcf81454235e5e2fa2a35f15ffc0ec7e.jar
  • /home/gmdubosc/.mx/cache/ICU4J_6f06e820cf4c8968bbbaae66ae0b33f6a256b57f.jar
  • /tmp/graal-js/graaljs/graal-js/mxbuild/dists/graaljs.jar

Looking into the build artifacts we can also see mxbuild/dists/graaljs-scriptengine.jar which is responsible for registering Graal.js with the script engine API.

Using a small test file:

import javax.script.*;
import java.util.Arrays;
public class Test {
  public static void main(String... args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    for (ScriptEngineFactory factory : manager.getEngineFactories()) {
      System.out.printf("%s %s: %s %s%n", factory.getLanguageName(), factory.getLanguageVersion(), factory.getEngineName(), factory.getNames());
    }
    ScriptEngine engine = manager.getEngineByName("Graal.js");
    if (engine != null) {
      Object result = engine.eval("typeof([] + 1)");
      System.out.println(result);
    }
  }
}

Compiling and running it on a stock JDK 8 gives:

$ javac Test.java
$ java -cp . Test
ECMAScript ECMA - 262 Edition 5.1: Oracle Nashorn [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]

Now with Graal.js on the classpath:

$ java -cp /tmp/graal-js/graal/sdk/mxbuild/dists/graal-sdk.jar:/tmp/graal-js/graal/truffle/mxbuild/dists/truffle-api.jar:/tmp/graal-js/graal/regex/mxbuild/dists/tregex.jar:/home/gmdubosc/.mx/cache/ASM_DEBUG_ALL_702b8525fcf81454235e5e2fa2a35f15ffc0ec7e.jar:/home/gmdubosc/.mx/cache/ICU4J_6f06e820cf4c8968bbbaae66ae0b33f6a256b57f.jar:/tmp/graal-js/graaljs/graal-js/mxbuild/dists/graaljs.jar:/tmp/graal-js/graaljs/graal-js/mxbuild/dists/graaljs-scriptengine.jar:. Test
ECMAScript ECMA - 262 Edition 6: Graal.js [Graal.js, graal.js, Graal-js, graal-js, Graal.JS, Graal-JS, GraalJS, GraalJSPolyglot, js, JS, JavaScript, javascript, ECMAScript, ecmascript]
ECMAScript ECMA - 262 Edition 5.1: Oracle Nashorn [nashorn, Nashorn, null, null, null, null, null, null]
string

(note that this command line ignores truffle-profiler, chromeinspector, launcher-common and graaljs-launcher which are not necessary when using Graal.js through the script engine.)

Since the standard JDK 8 doesn't support JVMCI and/or the Graal compiler, there will be no JIT compilations for JS so don't expect much in terms of performance. To get performance you need a special JDK 8 or JDK 9+ as well as the Graal-Truffle bindings.

like image 185
Gilles D. Avatar answered Sep 26 '22 06:09

Gilles D.