简体   繁体   中英

Evaluating JavaScript in Java with GraalVM

Until now we have used Nashorn to evaluate JS code in our Java project. Nashorn has couple of known disadvantages and we would like to move to GraalVM.

So we did a small POC and run into couple of issues, i hope you can help us :)

Our business use case: We provide our users a platform to write custom js code according to the following interface:

function process(record, ctx, logger, next) {
   if (record && record.data){
        record.data.firstName = "Joe";
    }
    return record;
}

record represents our internal model which is a Java Map.

And we would like to invoke the 'process' function from within our Java code and provide the parameters.

Few questions for you:

  1. What is the best way to do it according to the best practices? The 'record' parameter is a Java Map object which represent our internal model. when we send it to the process function, we would like it to be handled as a standard js object so our users can write standard js code according to ECMA. What is the right way to do it?
  2. We noticed the performance of Nashorn is better than Graalvm when we run couple of tests, is that make sense or we probably do something wrong?

We really appreciate your assistance and would like to move very soon to Graalvm.

  1. If you are using the GraalVM polyglot API Take a look at ProxyObjects ( https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/proxy/ProxyObject.html ). I think that might be your best bet.

  2. The GraalVM JS implementation should definitively outperform Nashorn but may need a couple of iterations of warmup to get there. Did you allow for warmup in your measurements?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM