Background:
Since Nashorn is being dropped in JDK15, I am looking for an alternative for an application I am working on. I am currently only using for the dynamic execution of some user-definable formatting snippets within a java swing desktop application.
I don't particularly want to add another library dependency to my app (like rhino). I would be willing to use nashorn as an additional dependency if it is available (this would save me from having to rewrite code, and ensure compatibility with existing js snippets). I haven't seen that it is available anywhere except as something that was related to minecraft.
I won't switch to Graal vm.
The problem:
I was considering using JShell (though not javascript, much of the formatting code is very similar), but the performance is abysmal the way I am calling it:
try(JShell js = JShell.create())
{
js.eval("public int add(int a, int b) { return a + b; }");
for(int i = 0; i < 100; i++)
{
List<SnippetEvent> eval = js.eval("add(5,6)");
eval.forEach(se -> {
System.out.println(se.value());
});
}
}
The for loop in the that code is taking ~6 seconds to run (compared to ~11 microseconds in nashorn). This will not be fast enough for my application.
Is there a way to get the class bytecode back out of JSell so I can use reflection to execute the method directly instead of calling 'eval' again?
Is there a way to get a 'method handle' to a method I create in JShell?
Is there any way of making a function where the behavior is defined in JShell, but can be called with high performance from 'normal' java?