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

后端 未结 2 835
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 23:41

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://g
相关标签:
2条回答
  • 2020-12-29 00:17

    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 git@github.com:graalvm/graaljs.git
    $ git clone git@github.com: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.

    0 讨论(0)
  • 2020-12-29 00:24

    ok, sadly I will answer my own question. After a bit of research I found out that I could not get it running on java 8 and anyway there is no .jar (and neither I can manually create it) for graaljs

    details are here:

    https://github.com/graalvm/graaljs/blob/master/docs/BUILDING-GRAAL.JS.md

    0 讨论(0)
提交回复
热议问题