<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hello!</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Sorry for long post. This is the main issue I have at the moment:<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I have a Value called engine that is first set to null and then later it is set to engine = cx.eval(&quot;js&quot;, jsCode); where cx is the context and jsCode is parsed JavaScript code. I do
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>engine.putMember(&quot;mote&quot;, currentMote);</i></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
where currentMote is a Mote,which is an interface. When I try to do <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<i>final Mote currentMote = (Mote) engine.getMember(&quot;mote&quot;);</i></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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?<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Here are some more details, to clarify what I am trying to do/ what I did/tried so far:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I made a copy of this repository <a href="https://urldefense.com/v3/__https://github.com/contiki-ng/contiki-ng__;!!GqivPVa7Brio!Mas5wN6fj94Hy2y3A8OY7fRE9VwqQBbC7SbatXnJzMEm68EjsDZEwwK8hAEYWd1ROm_L3Q$" id="LPlnk847501">
https://github.com/contiki-ng/contiki-ng</a> 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.
<br>
<br>
The code contains the following line:
<table class="highlight tab-size js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">
<tbody>
<tr>
<td class="blob-code blob-code-inner js-file-line"><i><span class="pl-k">private</span>
<span class="pl-smi">ScriptEngine</span> engine </i><span class="pl-k"><i>= <span class="pl-k">
new</span> <span class="pl-smi">ScriptEngineManager</span>()<span class="pl-k">.</span>getEngineByName(<span class="pl-s"><span class="pl-pds">&quot;</span>JavaScript<span class="pl-pds">&quot;</span></span>);</i><br>
</span></td>
</tr>
<tr>
</tr>
</tbody>
</table>
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:<br>
<i>private HostAccess access = HostAccess.newBuilder(HostAccess.ALL) <br>
</i>
<div><i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .targetTypeMapping(Value.class, Mote.class, null, null).build();</i></div>
<div><i>private Context cx = Context.newBuilder(&quot;graal.js&quot;).allowHostAccess(access).allowHostClassLookup(s -&gt; true).allowExperimentalOptions(true).option(&quot;js.nashorn-compat&quot;,&quot;true&quot;).build();</i></div>
This setup results in the current type error.<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Best regards,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
&nbsp;&nbsp; Katarina <br>
</div>
</body>
</html>