<div dir="ltr">Hello Steve, Christian<div><br></div><div>  thanks for answering.</div><div><br></div><div>  @Steve As an alternative to your proposed solution I found the <i>Java.from</i> function provided by GraalJS[1]. I was directly passing the Java list and then transforming it inside the Javascript code by invoking Java.from(myList). But finally I&#39;ve taken your solution because makes my code simpler (see below).</div><div><br></div><div> @Christian Let me clarify a little bit my use case. I have a program built in Javascript. I&#39;m using Graal because I want to execute this program with Java. So I need to handle data transformations in both &quot;directions&quot;:</div><div><br></div><div>  1) Passing input data from Java to GraalJS (Java --&gt; GraalJS)</div><div><br></div><div>       In the Java side I have a list of JSONObjects (org.codehaus.jettison.json.JSONObject objects) that I pass to the Javascript program. The Javascript program invokes several functions of the Javascript Array prototype (like sort()) with the provided list. With the <i>js.experimental-foreign-object-prototype</i> option I was able to make it work by directly passing my JSONObject list. But being this an &quot;experimental&quot; option, I would like to have a more &quot;stable&quot; approach. This is my current solution (based on Steve&#39;s suggestion):</div><div><br></div><div><i>    public Value jsonToGraal(List&lt;JSONObject&gt; jsonList) {</i></div><i>      Value myArray = context.eval(JavaScriptLanguage.ID, &quot;[]&quot;);<br>      jsonList.stream()<br>        .map(this::jsonToGraal)<br>        .forEach(value -&gt; myArray.setArrayElement(myArray.getArraySize(), value));<br>      return myArray;<br>  }</i><div><i><br></i></div><div><i>    private Value jsonToGraal(JSONObject json) {<br>    return context.eval(JavaScriptLanguage.ID, &quot;dummy = &quot; + json.toString());<br>  }</i></div><div><i><br></i></div><div>  Also note that I&#39;m using the string representation of the JSONObject with eval to transform my JSONObject into a Javascript objects.</div><div><br></div><div>  2) Receiving the result of the Javascript calculation in Java (GraalJS --&gt; Java)</div><div><br></div><div>     Once the Javascript program finishes it returns a Javascript object which I&#39;m retrieving in Java through a polyglot Value. In this case, I need to turn this Value into a JSONObject. This is my current solution for this transformation:</div><div><br></div><div><i>    public JSONObject graalToJson(Value value) {</i></div><i>    try {<br>        context.getBindings(JavaScriptLanguage.ID).putMember(&quot;value&quot;, value);<br>        String jsonContent = context.eval(JavaScriptLanguage.ID, &quot;JSON.stringify(value)&quot;).asString();<br>        return new JSONObject(jsonContent);<br>    } catch (JSONException ignore) {<br>      return null;<br>    }<br>  }</i><div><i><br></i></div><div><i>  </i>I also had another alternative for this which consists of using the polyglot Value object methods to retrieve the members and create my JSONObject manually. So far, both solutions are similar for me in terms of performance.</div><div><br></div><div>  Currently all of this is working fine for me, but I&#39;m not sure if this is the best way of dealing with this data transformations. I would like to know if there are any recommended alternatives to achieve this (specially if they are better in performance). Any suggestions are very welcome.</div><div><br></div><div>  Thank you in advance!</div><div><br></div><div>[1] <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_graalvm_graaljs_blob_master_docs_user_JavaScriptCompatibility.md-23javafromjavadata&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=Av98W_zk2c7VEnRBSQVpPTajz98UeMxxyUxx8SRfhBI&s=lUcFYCNqY6z8pBBpBY-DspP2pQrLjC-HisODkTlrylA&e=">https://github.com/graalvm/graaljs/blob/master/docs/user/JavaScriptCompatibility.md#javafromjavadata</a></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>  <br><div><br></div><div>  </div><div><i>  <br></i><div><br></div><div>   </div><div><br></div><div><br></div><div>[1] <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_graalvm_graaljs_blob_master_docs_user_JavaScriptCompatibility.md-23javafromjavadata&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=Av98W_zk2c7VEnRBSQVpPTajz98UeMxxyUxx8SRfhBI&s=lUcFYCNqY6z8pBBpBY-DspP2pQrLjC-HisODkTlrylA&e=">https://github.com/graalvm/graaljs/blob/master/docs/user/JavaScriptCompatibility.md#javafromjavadata</a> </div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El vie., 26 jul. 2019 a las 13:04, Christian Wirth (&lt;<a href="mailto:christian.wirth@oracle.com" target="_blank">christian.wirth@oracle.com</a>&gt;) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF">
    <p>Hi Carlos,</p>
    <p>thank you for your question.</p>
    <p>The JavaScript language is very lenient towards what it accepts
      as array. If an object has a &quot;length&quot; property, it behaves like an
      array. What you provide fulfils that requirement, and can thus be
      sorted (and searched, filtered, etc.).</p>
    <p>However, what you provide does not have `Array.prototype`
      assigned as its prototype. You are basically calling
      `({}).sort();`, resulting in a TypeError in plain JS (or the
      &quot;Message not supported&quot; in Interop). From JS language semantics,
      the right thing to do would be
      `Array.prototype.sort.call(myInteropObj);`. That, however,
      requires a change in your source code and potentially libraries
      that you use.</p>
    <p>To mitigate that, we have the <i>js.experimental-foreign-object-prototype
      </i>option that you found already. This forces the Array.prototype
      on array-like object; however, this option might also break other
      code, which is why we don&#39;t use that behavior by default.</p>
    <p><br>
    </p>
    <p>The option is experimental as we are not sure yet whether this is
      the right way to go for a longer time, or whether we can support
      that in a better way in the future. In this case, `experimental`
      really means: there is no guarantee that exactly this option, with
      exactly this behavior, will exist in future releases. We are aware
      of the usecase though and will see to support it in reasonable
      fashion in any case.<br>
    </p>
    <p>Your feedback that you are using this option is helpful to us:
      the more we know about &quot;experimental&quot; features being used in
      practice, the more we will see to have them as stable features.<br>
    </p>
    <p>Best,<br>
      Christian<br>
    </p>
    <p><br>
    </p>
    <div class="gmail-m_-5484088777024521845gmail-m_6697680310964871685moz-cite-prefix">Am 24.07.2019 um 13:25 schrieb Carlos
      Aristu:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">Hello all,
        <div><br>
        </div>
        <div>  I&#39;m currently using GraalJS to execute some javascript
          functions from my Java code. One of this javascript functions
          receives an array as an argument and invokes the <i>sort()</i>
          method.</div>
        <div><br>
        </div>
        <div>  To invoke this function from Java, I&#39;m creating an
          ArrayList and I send it to through a ProxyArray, something
          similar to this code snippet:</div>
        <div><br>
        </div>
        <div><i>      Source sources = Source.newBuilder(&quot;js&quot;, jsCode,
            &quot;myCode&quot;).buildLiteral();</i></div>
        <div><i>      Source function = Source.create(&quot;js&quot;,
            &quot;myFunction&quot;);<br>
          </i></div>
        <div><i>      List&lt;String&gt; myList = new
            ArrayList&lt;&gt;();</i></div>
        <div><i>      try (Context context = Context.newBuilder()</i></div>
        <i>        .engine(Engine.create())<br>
                  .allowHostAccess(HostAccess.ALL)<br>
                  .build()) {<br>
                    context.eval(sources);<br>
                    Value res =
          context.eval(function).execute(ProxyArray.fromList(myList));<br>
              }</i>
        <div><br>
        </div>
        <div>  That code fails with the following error: &quot;<i>org.graalvm.polyglot.PolyglotException:
            TypeError: invokeMember on foreign object failed due to:
            Message not supported.</i>&quot;</div>
        <div><br>
        </div>
        <div>  To make it work, I need to enable the <i>js.experimental-foreign-object-prototype
          </i>option[1] for the Context.</div>
        <div><br>
        </div>
        <div>  Being an experimental option and according to the docs,
          it is not recommended to enable it for productive
          environments. My questions are:</div>
        <div><br>
        </div>
        <div>  a) ¿Is there any plan to switch this option as
          non-experimental?</div>
        <div>  b) ¿Do you know if there exists any alternative (or a
          better way) to provide a JS array as an argument of a JS
          function from Java?</div>
        <div><br>
        </div>
        <div>Thank you in advance.</div>
        <div><br>
        </div>
        <div>[1] <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_graalvm_graaljs_issues_88-23issuecomment-2D453021299&amp;d=DwMFaQ&amp;c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&amp;r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&amp;m=SONxRA-cJWH4INLX-uDgL1Afx7XABorqPK3ite36VtA&amp;s=Hw4Fpfxv8qQMK9XrdAcEW49J5nMi0ODhnsFdecPww3U&amp;e=" target="_blank">https://github.com/graalvm/graaljs/issues/88#issuecomment-453021299</a><br>
          <div>-- <br>
            <div dir="ltr" class="gmail-m_-5484088777024521845gmail-m_6697680310964871685gmail_signature">
              <div dir="ltr">
                <div>
                  <div dir="ltr">
                    <div dir="ltr">
                      <div><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__www.openbravo.com&amp;d=DwMFaQ&amp;c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&amp;r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&amp;m=SONxRA-cJWH4INLX-uDgL1Afx7XABorqPK3ite36VtA&amp;s=qLyjnBLTJ0nP65gCeCPRY2IqJC0l9sOfxepjQ0AnXfM&amp;e=" target="_blank"><img src="https://www.openbravo.com/wp-content/uploads/2018/08/logoOB_OBI.png" width="170px;" height="38px;"></a></div>
                      <div><span>Carlos Aristu
                          López</span><font size="3" face="Times New
                          Roman" color="#000000"> </font><br>
                        <font face="Century Gothic, Verdana" color="#6eb43f"><span style="font-size:12px">Applications
                            Engineer</span></font><br>
                        <br>
                      </div>
                      <p><i>This
                          e-mail is confidential and contains private
                          information. Any reading, retention,
                          distribution or copying of this communication
                          by any person other than its intended
                          recipient is prohibited.</i></p>
                      <p><i>Your
                          data is processed by Openbravo under our <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__www.openbravo.com_privacy-2Dpolicy_&amp;d=DwMFaQ&amp;c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&amp;r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&amp;m=SONxRA-cJWH4INLX-uDgL1Afx7XABorqPK3ite36VtA&amp;s=S2N4L5FShXFXrm0d1AT3OyWLXYYH51PCWsdofw_twng&amp;e=" style="color:rgb(110,180,63)" target="_blank">privacy
                            policy</a></i></p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="gmail-m_-5484088777024521845gmail-m_6697680310964871685mimeAttachmentHeader"></fieldset>
      <pre class="gmail-m_-5484088777024521845gmail-m_6697680310964871685moz-quote-pre">_______________________________________________
GraalVM-Users mailing list
<a class="gmail-m_-5484088777024521845gmail-m_6697680310964871685moz-txt-link-abbreviated" href="mailto:GraalVM-Users@oss.oracle.com" target="_blank">GraalVM-Users@oss.oracle.com</a>
<a class="gmail-m_-5484088777024521845gmail-m_6697680310964871685moz-txt-link-freetext" href="https://oss.oracle.com/mailman/listinfo/graalvm-users" target="_blank">https://oss.oracle.com/mailman/listinfo/graalvm-users</a></pre>
    </blockquote>
  </div>

_______________________________________________<br>
GraalVM-Users mailing list<br>
<a href="mailto:GraalVM-Users@oss.oracle.com" target="_blank">GraalVM-Users@oss.oracle.com</a><br>
<a href="https://oss.oracle.com/mailman/listinfo/graalvm-users" rel="noreferrer" target="_blank">https://oss.oracle.com/mailman/listinfo/graalvm-users</a></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_-5484088777024521845gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div style="color:rgb(0,0,0);font-family:&quot;Times New Roman&quot;;font-size:medium;float:left;margin-right:15px"><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__www.openbravo.com&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=Av98W_zk2c7VEnRBSQVpPTajz98UeMxxyUxx8SRfhBI&s=92BdYf5XGnh1ZH_JLABVzFfoITnqmapW4yzu6DOE1XI&e=" target="_blank"><img src="https://www.openbravo.com/wp-content/uploads/2018/08/logoOB_OBI.png" width="170px;" height="38px;"></a></div><div><span style="color:rgb(102,102,102);font-family:&quot;Century Gothic&quot;,Verdana;font-size:12px;font-weight:bold">Carlos Aristu López</span><font color="#000000" face="Times New Roman" size="3"> </font><br><font color="#6eb43f" face="Century Gothic, Verdana"><span style="font-size:12px">Applications Engineer</span></font><br><br></div><p style="font-size:11px;font-family:&quot;Century Gothic&quot;,Verdana;color:rgb(153,153,153)"><i>This e-mail is confidential and contains private information. Any reading, retention, distribution or copying of this communication by any person other than its intended recipient is prohibited.</i></p><p style="font-size:11px;font-family:&quot;Century Gothic&quot;,Verdana;color:rgb(153,153,153)"><i>Your data is processed by Openbravo under our <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__www.openbravo.com_privacy-2Dpolicy_&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=Av98W_zk2c7VEnRBSQVpPTajz98UeMxxyUxx8SRfhBI&s=tkGcYufAInp_ieW7JNvw1x0Olis5vwVHtxg578-lWFU&e=" style="color:rgb(110,180,63)" target="_blank">privacy policy</a></i></p></div></div></div></div></div>