Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

javascript - Using GraalVM instead of Nashorn Engine to use Jvascript's eval() function

I am making a calculator using JFrame. I want to use the eval() built-in function of javascript. I used to do this using the Nashorn engine. But on further googling, I found that it has been removed from JDK 15. I found that an alternative is GraalVM. But how to use it for the eval() function. Do I need to download it for use? Like in Nashorn I used to import something and then write the following code:

try
                {
                    ScriptEngineManager manager = new ScriptEngineManager();
                    ScriptEngine engine = manager.getEngineByName("js");
                    ans=String.valueOf(engine.eval(str));
                }
                catch(ScriptException h)
                {
                    System.err.println("Error evaluating the script: " + h.getMessage());
                }

How to do this in GraalVM? Thanks in Advance????:-)

question from:https://stackoverflow.com/questions/66058771/using-graalvm-instead-of-nashorn-engine-to-use-jvascripts-eval-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're using GraalVM as your JDK then the GraalJS - GraalVM's Javascript implementation will be available on the classpath automatically and you can use it the with a bunch of import statements as you've used to.

For example the following program would work like below:

import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;

public class HelloPolyglot {
    public static void main(String[] args) {
        System.out.println("Hello Java!");
        try (Context context = Context.create()) {
            context.eval("js", "print('Hello JavaScript!');");
        }
    }
}
> javac HelloPolyglot.java
> java HelloPolyglot
Hello Java!
Hello JavaScript! 

This is the recommended way to use it, the polyglot API from the GraalVM's sdk, and running on graalvm.

You can also use the ScriptEngine API: https://www.graalvm.org/reference-manual/js/ScriptEngine/

And you also can run it on an arbitrary JDK distribution and use GraalJS as a bunch of dependencies. Here's a sample project you can look at for going this path: https://www.graalvm.org/reference-manual/js/RunOnJDK/, which I would not recommend. GraalVM's JIT is really good at optimizing it at runtime, other JITs aren't that good, so the performance could be worse.

This document can be very helpful when migrating from Nashorn - https://www.graalvm.org/reference-manual/js/ScriptEngine usually there are no complications, but there could be some corner cases around concurrency, etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...