[graalvm-users] User defined types in GraalVM Java/JavaScript interoperability

Kulkova, Katarina katarina.kulkova18 at imperial.ac.uk
Tue Jul 28 03:24:31 PDT 2020


Sorry for long post. This is the main issue I have at the moment:
I have been working on migrating a rather large project from nashorn to graalJS and I believe it would help me to know how GraalVM handles user defined types when it comes to Java and JavaScript interoperability. I have been using the Context API and I tried to use Values/Bindings, as well as context.enter() and context.leave() blocks to avoid multi-threaded access to JavaScript objects.
I have a Value called engine that is first set to null and then later it is set to engine = cx.eval("js", jsCode); where cx is the context and jsCode is parsed JavaScript code. I do
engine.putMember("mote", currentMote);
where currentMote is a Mote,which is an interface. When I try to do
final Mote currentMote = (Mote) engine.getMember("mote");
So my question is, how are user defined types handled, especially if I want to pass an interface or superclass to the engine and get the it back from the engine. If an interface/subclass is passed to the engine, are all its subclasses also passed to the engine, or do I have to do it? If so, how?
I could not find very clear documentation on this subject. It would be very helpful if you could point me to a place where this is discussed/explained or give me an example.

Here are some more details, to clarify what I am trying to do/ what I did/tried so far:

I made a copy of this repository https://urldefense.com/v3/__https://github.com/contiki-ng/contiki-ng__;!!GqivPVa7Brio!Mas5wN6fj94Hy2y3A8OY7fRE9VwqQBbC7SbatXnJzMEm68EjsDZEwwK8hAEYWd1ROm_L3Q$  and started making my own changes. I had to do some fixes regarding java 11 compatibility as well as updating the javax syntax pane. Most of my work at the moment is focused inside of contiki-ng/tools/cooja/java/org/contikios/cooja/plugins/LogScriptEngine.java, which uses ScriptEngine. I set up my environment to run java code with GraalVM. The code does not run properly on GraalVM.

The code contains the following line:
private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
I changed the engine to nashorn and that solved the issue, but this is not a very permanent fix as nashorn support will soon be removed. So I tried changing the engine to graal.js and this gave me a Type Error. I discovered that this is due to GraalVM having a stricter sandboxing policy and being much more restrictive in terms of access. I tried adding a ScriptContext and allowing all access, which fixed the type error, however, this caused a lot of illegal multi-threaded access errors. I tried using synchronized blocks to restrict every access to the engine, but this only seemed to deadlock the code. I tried making the engine a Value and use the GraalVM Context API and surround all engine access with cx.enter() and cx.leave(). This is how I define my context:
private HostAccess access = HostAccess.newBuilder(HostAccess.ALL)
          .targetTypeMapping(Value.class, Mote.class, null, null).build();
private Context cx = Context.newBuilder("graal.js").allowHostAccess(access).allowHostClassLookup(s -> true).allowExperimentalOptions(true).option("js.nashorn-compat","true").build();
This setup results in the current type error.

Again, sorry if this is long or not very clear, the issue is rather confusing to me, feel free to ask for more clarification on what I have done so far. Any help is appreciated.

Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20200728/e03895ac/attachment.html 

More information about the GraalVM-Users mailing list