作者:小森林 | 来源:互联网 | 2022-12-14 14:44
我使用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文件但不运气的地方
1> Gilles D...:
目前GraalVM之外没有预制的Graal.js罐子.要在其他JDK上运行它,您可以从GraalVM中提取jar或者像这样构建它:
$ 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
请注意,它使用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
(请注意,此命令行无视truffle-profiler
,chromeinspector
,launcher-common
并graaljs-launcher
通过脚本引擎使用Graal.js时不是必需的.)
由于标准JDK 8不支持JVMCI和/或Graal编译器,因此JS不会有JIT编译,所以在性能方面不要期望太多.要获得性能,您需要特殊的JDK 8或JDK 9+以及Graal-Truffle绑定.