Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression when migrating from 20.1.0 to 24.1.2 #886

Open
gabrieltulli opened this issue Jan 29, 2025 · 2 comments
Open

Regression when migrating from 20.1.0 to 24.1.2 #886

gabrieltulli opened this issue Jan 29, 2025 · 2 comments

Comments

@gabrieltulli
Copy link

After updating the lib version my application start to throw the exception bellow k.equals is not a function.
Below I provide a small way to reproduce the error.
Changing the comment dependencies back, the code works as expected.
There is any document that explicit any regressions on this update or this is a bug?

I have hundreds of different files that will need fixed if I want to upgrade.

Code:

import org.graalvm.polyglot.Context;

public class Main {
    public static void main(String[] args) {

        Context context = Context.newBuilder()
                .allowExperimentalOptions(true)
                .option("js.nashorn-compat", "true")
                .option("js.ecmascript-version", "2020")
                .allowAllAccess(true)
                .build();

        var script =
        """
        var namesList = java.util.List.of("test","test1");
        
        namesList.stream()
        .findFirst()
        .map(s => java.util.stream.Stream.of(s.split(' '))
            .anyMatch(k => k.equals('test'))
        )
        """;
        Object contextEval = context.eval("js", script);

        System.out.println(contextEval.toString());
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>graalvm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
<!--        <dependency>-->
<!--            <groupId>org.graalvm.sdk</groupId>-->
<!--            <artifactId>graal-sdk</artifactId>-->
<!--            <version>20.1.0</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.graalvm.js</groupId>-->
<!--            <artifactId>js</artifactId>-->
<!--            <version>20.1.0</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.graalvm.polyglot</groupId>
            <artifactId>polyglot</artifactId>
            <version>24.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.polyglot</groupId>
            <artifactId>js-community</artifactId>
            <version>24.1.2</version>
            <type>pom</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

stacktrace:

~/.sdkman/candidates/java/21.0.3-tem/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=56879:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath ~/graalvm/target/classes:~/.m2/repository/org/graalvm/polyglot/polyglot/24.1.2/polyglot-24.1.2.jar:~/.m2/repository/org/graalvm/sdk/collections/24.1.2/collections-24.1.2.jar:~/.m2/repository/org/graalvm/sdk/nativeimage/24.1.2/nativeimage-24.1.2.jar:~/.m2/repository/org/graalvm/sdk/word/24.1.2/word-24.1.2.jar:~/.m2/repository/org/graalvm/js/js-language/24.1.2/js-language-24.1.2.jar:~/.m2/repository/org/graalvm/regex/regex/24.1.2/regex-24.1.2.jar:~/.m2/repository/org/graalvm/truffle/truffle-api/24.1.2/truffle-api-24.1.2.jar:~/.m2/repository/org/graalvm/shadowed/icu4j/24.1.2/icu4j-24.1.2.jar:~/.m2/repository/org/graalvm/truffle/truffle-runtime/24.1.2/truffle-runtime-24.1.2.jar:~/.m2/repository/org/graalvm/sdk/jniutils/24.1.2/jniutils-24.1.2.jar:~/.m2/repository/org/graalvm/truffle/truffle-compiler/24.1.2/truffle-compiler-24.1.2.jar Main
[To redirect Truffle log output to a file use one of the following options:
* '--log.file=<path>' if the option is passed using a guest language launcher.
* '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher.
* Configure logging using the polyglot embedding API.]
[engine] WARNING: The polyglot engine uses a fallback runtime that does not support runtime compilation to native code.
Execution without runtime compilation will negatively impact the guest application performance.
The following cause was found: JVMCI is not enabled for this JVM. Enable JVMCI using -XX:+EnableJVMCI.
For more information see: https://www.graalvm.org/latest/reference-manual/embed-languages/#runtime-optimization-support.
To disable this warning use the '--engine.WarnInterpreterOnly=false' option or the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property.
Exception in thread "main" TypeError: k.equals is not a function
	at <js> :=>(Unnamed:6:154-169)
	at com.oracle.truffle.polyglot.PolyglotFunctionProxyHandler.invoke(PolyglotFunctionProxyHandler.java:151)
	at jdk.proxy1/jdk.proxy1.$Proxy21.test(Unknown Source)
	at java.base/java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
	at java.base/java.util.stream.Streams$StreamBuilderImpl.tryAdvance(Streams.java:397)
	at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
	at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
	at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.anyMatch(ReferencePipeline.java:632)
	at <js> :=>(Unnamed:5-6:94-170)
	at com.oracle.truffle.polyglot.PolyglotFunction.apply(PolyglotFunction.java:66)
	at java.base/java.util.Optional.map(Optional.java:260)
	at <js> :program(Unnamed:3-7:52-172)
	at org.graalvm.polyglot.Context.eval(Context.java:428)
	at Main.main(Main.java:23)

Process finished with exit code 1
@jeffque
Copy link

jeffque commented Jan 31, 2025

Created a MVCE: https://github.com/jeffque/graalvm-regression

To run the problem:

./mvnw compile exec:java -Pgraalvm-24

To run the previous version os graalvm:

./mvnw compile exec:java -Pgraalvm-20

@iamstolis
Copy link
Member

The root of the problem is that there are two overrides of Stream.of: one that takes Object parameter and one that takes Object.... GraalVM 20.1.x prefers the second override while more recent versions prefer the first override.

Honestly, the selection of the right override is a bit tricky even in Java in this case. So, it is error-prone to depend on particular selection when this method is invoked using JavaScript-Java interoperability. I would suggest using an explicit selection of the override (which is possible in recent versions of graal-js). For example, java.util.stream.Stream['of(java.lang.Object[])'](s.split(' ')) in your case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants