<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);">
<p>Hello!</p>
<p><br>
</p>
<p>I am working on porting a rather large code base from nashorn to graal.js. The original code was using javax.script.ScriptEngine. I would like to replace it by the polyglot Context API if possible<br>
</p>
<pre><code> private ScriptEngine engine =
    new ScriptEngineManager().getEngineByName(&quot;nashorn&quot;);
</code></pre>
<p>I have decided to replace the ScriptEngine with the polyglot Context API.</p>
<pre><code>private Context cx = Context.newBuilder().allowAllAccess(true).allowHostAccess(HostAccess.ALL).build();
</code></pre>
<p>I replaced every call to engine.put() with a call to cx.getBindings(&quot;js&quot;).putMember() and similarly engine.get() with cx.getBindings(&quot;js&quot;).getMember() like this:</p>
<pre><code>cx.getBindings(&quot;js&quot;).putMember(&quot;mote&quot;, mote);
cx.getBindings(&quot;js&quot;).putMember(&quot;id&quot;, id);
//engine.put(&quot;mote&quot;, mote);
//engine.put(&quot;id&quot;, id);
</code></pre>
<p>I also replaced the engine.eval() call with a cx.eval() call. However, there is one place in the code where I cannot get rid of the engine. The engine is cast as an Invocable and runs a Thread in a thread group:</p>
<pre><code>scriptThread = new Thread(group, new Runnable() {
  public void run() {
    try {
      ((Invocable)engine).getInterface(Runnable.class).run();
    } catch (RuntimeException e) {
      Throwable throwable = e;
      while (throwable.getCause() != null) {
        throwable = throwable.getCause();
      }

      if (throwable.getMessage() != null &amp;&amp;
          throwable.getMessage().contains(&quot;test script killed&quot;) ) {
        logger.info(&quot;Test script finished&quot;);
      } else {
        if (!Cooja.isVisualized()) {
          logger.fatal(&quot;Test script error, terminating Cooja.&quot;);
          logger.fatal(&quot;Script error:&quot;, e);
          System.exit(1);
        }

        logger.fatal(&quot;Script error:&quot;, e);
        deactivateScript();
        simulation.stopSimulation();
        if (Cooja.isVisualized()) {
          Cooja.showErrorDialog(Cooja.getTopParentContainer(),
              &quot;Script error&quot;, e, false);
        }
      }
    }
    /*logger.info(&quot;test script thread exits&quot;);*/
  }
});
scriptThread.start(); /* Starts by acquiring semaphore (blocks) */
while (!semaphoreScript.hasQueuedThreads()) {
  Thread.yield();
}
</code></pre>
<p>This is the part I would like to change:</p>
<pre><code> ((Invocable)engine).getInterface(Runnable.class).run();
</code></pre>
<p>Is there a Context equivalent for this? I could not find any documentation regarding Invocables in the Context API. I have, however, read somewhere that it I should use Context to execute JavaScript code when using GraalVM. So my question is, is there a
 way to change this piece of code, so that it keeps its functionality but I don't have to use the engine anymore and use the Context API instead? If not, what would be a good way to do it?</p>
<p><br>
</p>
<p>Thanks in advance.</p>
<p><br>
</p>
<p>Best regards,<br>
</p>
<p>&nbsp;&nbsp; Katarina<br>
</p>
<br>
</div>
</body>
</html>