我使用Java 8,并且使用默认的JavaScript引擎(Nashorn)。

我想看看它与“高度宣传”的GRAAL JS相比如何。
看:

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

  • 特别是因为我听说他们想弃用nashorn:
  • http://openjdk.java.net/jeps/335

  • 有人知道如何(轻松)访问graaljs吗?
    我希望找到一个pom.xml或一个下载jar文件的地方
    但不是运气

    最佳答案

    目前,在GraalVM之外没有可用的预构建的Graal.js jar。要在另一个JDK上运行它,您可以从GraalVM中提取jar或像这样构建它:

    $ 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
    

    请注意,它可以用JDK 8很好地构建。它也可以在JDK 8上运行:
    $ mx js
    > typeof([] + 1)
    string
    >
    

    该 shell 起作用,按Ctrl + D退出。上一个命令行中的-v选项显示了如何启动它:
    $ 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
    

    因此,它将这些 jar 放在类路径中:
  • /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

  • 查看构建工件,我们还可以看到mxbuild/dists/graaljs-scriptengine.jar,它负责使用脚本引擎API注册Graal.js。

    使用一个小的测试文件:
    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);
        }
      }
    }
    

    在库存JDK 8上进行编译和运行可以得到:
    $ javac Test.java
    $ java -cp . Test
    ECMAScript ECMA - 262 Edition 5.1: Oracle Nashorn [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript]
    

    现在在类路径上使用Graal.js:
    $ 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
    

    (请注意,此命令行会忽略通过脚本引擎使用Graal.js时不需要的truffle-profilerchromeinspectorlauncher-commongraaljs-launcher。)

    由于标准的JDK 8不支持JVMCI和/或Graal编译器,因此将没有用于JS的JIT编译,因此不要对性能抱有太大期望。为了获得性能,您需要特殊的JDK 8或JDK 9+以及Graal-Truffle绑定(bind)。

    关于java - 如何使用graaljs?是否有放置.jar文件的地方?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50738198/

    10-11 05:02