You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're trying to migrate our Java + Javascript project from Nashorn to Graal. In our project we had js files which are loaded when application starts. When java application receives some event, it calls js function with this event as parameter.
As I understand according oracle/graaljs#59, for multithreading support we need create org.graalvm.polyglot.Context each time when we call our js function and call "eval" for js file and it's dependencies.
Because of this we should not use classes from JDK like javax.script.ScriptEngine because we cannot transfer polyglot context to them. So we should use poliglot api and our code shold be like this:
//once after application start
Source source = Source.newBuilder("js", file.toFile()).build();
//every time when we need call js function from java
try (Context context = Context.newBuilder().option("js.nashorn-compat", "true").engine(Engine.create()).build()) {
context.eval(source);
context.getBindings("js").getMember("a").execute(params);
}
Is it correct?
The text was updated successfully, but these errors were encountered:
You need to create a JS context for each parallel thread, not necessarily for each script (depends on your use-case whether you want to allow the global object between scripts to be shared).
You may use the script engine API and instead enable Nashorn compatibility using -Dpolyglot.js.nashorn-compat=true as JVM argument. That being said we recommend using the polyglot API as it provides the most features.
Alternatively, you may use GraalJSScriptEngine#create to initialize the script engine from a custom polyglot Context.
We're trying to migrate our Java + Javascript project from Nashorn to Graal. In our project we had js files which are loaded when application starts. When java application receives some event, it calls js function with this event as parameter.
As I understand according oracle/graaljs#59, for multithreading support we need create org.graalvm.polyglot.Context each time when we call our js function and call "eval" for js file and it's dependencies.
Because of this we should not use classes from JDK like javax.script.ScriptEngine because we cannot transfer polyglot context to them. So we should use poliglot api and our code shold be like this:
Is it correct?
The text was updated successfully, but these errors were encountered: