iamgio
iamgio

Reputation: 93

Not in GraalVM JS runtime?

I'm using the latest (21.0.0) version of GraalVM for JavaScript via Maven in Nashorn compatibility mode.
Upon noticing functions like Java.addToClassPath and objects like Polyglot don't exist, I ran the sample code shown here:

if (typeof Graal != 'undefined') {
    print(Graal.versionJS);
    print(Graal.versionGraalVM);
    print(Graal.isGraalRuntime());
}

Output:

undefined
snapshot
false

This looks weird to me. How can I access functions and objects like the above ones?
Thanks.

Upvotes: 1

Views: 822

Answers (1)

Christian Wirth
Christian Wirth

Reputation: 296

Graal.isGraalRuntime() returning false is a red flag. That means you are NOT using GraalVM as optimizing compiler. You can evaluate JavaScript that way, but it will be horribly slow, because the code is never compiled to machine code.

The preferred way to execute the code is obviously on GraalVM - then everything will be in place already.

To run GraalVM JavaScript on a stock JDK, there is documentation on graalvm.org: https://www.graalvm.org/reference-manual/js/RunOnJDK/#graalvm-javascript-on-jdk-11

The basic commandline you will need after you downloaded the JAR files from maven, including the GraalVM compiler itself (this should work on JDK11 or later). The relevant part there that might be different from your current setup is the --upgrade-module-path where you add GraalVM Compiler:

JARS=/path/to/JARs
JDK=/path/to/JDK
$JDK/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler --module-path=$JARS/graal-sdk-21.0.0.jar:$JARS/truffle-api-21.0.0.jar --upgrade-module-path=$JARS/compiler-21.0.0.jar:$JARS/compiler-management-21.0.0.jar -cp $JARS/launcher-common-21.0.0.jar:$JARS/js-launcher-21.0.0.jar:$JARS/js-21.0.0.jar:$JARS/truffle-api-21.0.0.jar:$JARS/graal-sdk-21.0.0.jar:$JARS/js-scriptengine-21.0.0.jar:$JARS/regex-21.0.0.jar:$JARS/icu4j-67.1.jar com.oracle.truffle.js.shell.JSLauncher

Upvotes: 1

Related Questions