Replace nashorn with graalVM

3.1k Views Asked by At

I am migrating a Javascript engine from Nashorn to GraalVm and it's failing to instantiate GraalJSScriptEngine/ScriptEngine. I tried different ways of doing it:

GraalJSEngineFactory factory =  new GraalJSEngineFactory();
GraalJSScriptEngine engine = factory.getScriptEngine();
GraalJSScriptEngine engine = GraalJSScriptEngine.create(
            null,
            Context.newBuilder("js")
                .allowHostAccess(HostAccess.ALL)
                .allowHostClassLookup(s -> true)
        );

I am getting following errors for above:

ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider com.oracle.truffle.js.scriptengine.GraalJSEngineFactory could not be instantiated

java.lang.AbstractMethodError: Receiver class com.oracle.truffle.polyglot.PolyglotImpl does not define or inherit an implementation of the resolved method abstract buildEngine(Ljava/io/OutputStream;Ljava/io/OutputStream;Ljava/io/InputStream;Ljava/util/Map;JLjava/util/concurrent/TimeUnit;ZJZZZLorg/graalvm/polyglot/io/MessageTransport;Ljava/lang/Object;Lorg/graalvm/polyglot/HostAccess;)Lorg/graalvm/polyglot/Engine; of abstract class org.graalvm.polyglot.impl.AbstractPolyglotImpl.
ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");

It returns null while doing like ^. Also I noticed there is only one factory(Nashorn) present in ScriptEngineManager instance.

I have added truffle-api jar in classpath as well as runnning it with VM options: -ea -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -Dpolyglot.js.nashorn-compat=true

I'm running it on Java 13 with the following additional dependencies:

         <dependency>
            <groupId>org.graalvm.sdk</groupId>
            <artifactId>graal-sdk</artifactId>
            <version>19.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.graalvm.js/js -->
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>19.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.graalvm.js/js-scriptengine -->
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>19.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.graalvm.compiler/compiler -->
        <dependency>
            <groupId>org.graalvm.compiler</groupId>
            <artifactId>compiler</artifactId>
            <version>19.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.graalvm.truffle</groupId>
            <artifactId>truffle-api</artifactId>
        </dependency>
0

There are 0 best solutions below