1

I am using GraalVM to interoperate with JavaScript because Nashorn is due to be deprecated.

The problem is that my variable is undefined in JavaScript runtime.

ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
String path = "rules.js";
InputStream is = Main.class.getClassLoader().getResourceAsStream(path);
Reader reader = new InputStreamReader(is);

engine.eval(reader);
Invocable inv = (Invocable) engine;
Object obj = engine.get("obj");

User user = new User();
user.setName("username");
user.setProfile("admin");

Boolean value = (Boolean) inv.invokeMethod(obj, "validatePermission", user);

This is the JavaScript code that I am running:

var obj = new Object();

obj.validatePermission = function(user) {
    console.log(user.profile)
    if(user.profile === 'admin') {
        return true;
    } else {
        throw 'Unauthorized. User must be administrator.';
    }
}

POM dependencies:

<project>
    <properties>
        <graalvm.version>19.0.0</graalvm.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>${graalvm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>${graalvm.version}</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.truffle</groupId>
            <artifactId>truffle-api</artifactId>
            <version>${graalvm.version}</version>
        </dependency>
    </dependencies>
</project>
6
  • Note that you can include nashorn in your deployment. Then you do not rely on the JVM including it. Commented May 23, 2019 at 5:27
  • @ThorbjørnRavnAndersen didn't understand your comment. I made it work with Nashhord, but it is supposed to be deprecated. What do you mean? Commented May 23, 2019 at 11:07
  • Nashorn is deprecated by Oracle and will be removed from the JDK distribution. That does not prohibit you from including a copy of Nashorn in your own application (like any other dependency you use) and use that even on new versions of Java. Commented May 23, 2019 at 12:11
  • @ThorbjørnRavnAndersen hmmm gotcha, I assumed that was the case. I did search for the Nashorn distro on Maven and didn't find it. My guess is that Nashorn is not yet deprecated so it did not move to the community yet? Commented May 23, 2019 at 12:33
  • 1
    nashorn is being deprecated as it is falling behind the more recent ECMAScript. if someone were to come forward and maintain it then it wouldn't be removed. the fact that it isn't being maintained means you probably don't want to ship it unless you are maintaining some long-term supported legacy software. graalvm claims to support the newer ECMAScript 2018 and says it will support 2019 when it isn't draft. so it seems like a good idea to upgrade to 'graal.js' and fall back to nashorn. Commented Jun 7, 2019 at 18:18

1 Answer 1

1

Setting Nashorn compatibility mode solved it.

System.setProperty("polyglot.js.nashorn-compat", "true");

More information about here: Nashorn Migration Guide.

All info above provided this issue by the GraalVM team.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.