Skip to content
Advertisement

Sandboxed java scripting replacement for Nashorn

I’ve been using Nashorn for awk-like bulk data processing. The idea is, that there’s a lot of incoming data, coming row by row, one by another. And each row consists of named fields. These data are processed by user-defined scripts stored somewhere externally and editable by users. Scripts are simple, like if( c>10) a=b+3, where a, b and c are fields in the incoming data rows. The amount of data is really huge. Code is like that (an example to show the use case):

JavaScript

Now nashorn is obsolete and I’m looking for alternatives. Was googling for a few days but didn’t found exact match for my needs. The requirements are:

  • Speed. There’s a lot of data so it shall be really fast. So I assume as well, precompilation is the must
  • Shall work under linux/openJDK
  • Support sandboxing at least for data access/code execution

Nice to have:

  • Simple, c-like syntax (not lua;)
  • Support sandboxing for CPU usage

So far I found that Rhino is still alive (last release dated 13 Jan 2020) but I’m not sure is it still supported and how fast it is – as I remember, one of reasons Java switched to Nashorn was speed. And speed is very important in my case. Also found J2V8 but linux is not supported. GraalVM looks like a bit overkill, also didn’t get how to use it for such a task yet – maybe need to explore further if it is suitable for that, but looks like it is complete jvm replacement and cannot be used as a library.

It’s not necessary shall be javascript, maybe there are other alternatives. Thank you.

Advertisement

Answer

GraalVM’s JavaScript can be used as a library with the dependencies obtained as any Maven artifact. While the recommended way to run it is to use the GraalVM distribution, there are some explanations how to run it on OpenJDK.

You can restrict things script should have access to, like Java classes, creating threads, etc:

From the documentation:

JavaScript

And it is several times faster than Nashorn. Some measurements can be found for example in this article:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement