[graalvm-users] Question about js.experimental-foreign-object-prototype option

Christian Wirth christian.wirth at oracle.com
Fri Jul 26 04:00:33 PDT 2019

Hi Carlos,

thank you for your question.

The JavaScript language is very lenient towards what it accepts as 
array. If an object has a "length" property, it behaves like an array. 
What you provide fulfils that requirement, and can thus be sorted (and 
searched, filtered, etc.).

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 "Message not supported" 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.

To mitigate that, we have the /js.experimental-foreign-object-prototype 
/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't use that behavior by default.

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.

Your feedback that you are using this option is helpful to us: the more 
we know about "experimental" features being used in practice, the more 
we will see to have them as stable features.


Am 24.07.2019 um 13:25 schrieb Carlos Aristu:
> Hello all,
>   I'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 /sort()/ method.
>   To invoke this function from Java, I'm creating an ArrayList and I 
> send it to through a ProxyArray, something similar to this code snippet:
> /      Source sources = Source.newBuilder("js", jsCode, 
> "myCode").buildLiteral();/
> /      Source function = Source.create("js", "myFunction");
> /
> /      List<String> myList = new ArrayList<>();/
> /      try (Context context = Context.newBuilder()/
> /        .engine(Engine.create())
>         .allowHostAccess(HostAccess.ALL)
>         .build()) {
>           context.eval(sources);
>           Value res = 
> context.eval(function).execute(ProxyArray.fromList(myList));
>     }/
>   That code fails with the following error: 
> "/org.graalvm.polyglot.PolyglotException: TypeError: invokeMember on 
> foreign object failed due to: Message not supported./"
>   To make it work, I need to enable the 
> /js.experimental-foreign-object-prototype /option[1] for the Context.
>   Being an experimental option and according to the docs, it is not 
> recommended to enable it for productive environments. My questions are:
>   a) ¿Is there any plan to switch this option as non-experimental?
>   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?
> Thank you in advance.
> [1] 
> https://github.com/graalvm/graaljs/issues/88#issuecomment-453021299 
> <https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_graalvm_graaljs_issues_88-23issuecomment-2D453021299&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=SONxRA-cJWH4INLX-uDgL1Afx7XABorqPK3ite36VtA&s=Hw4Fpfxv8qQMK9XrdAcEW49J5nMi0ODhnsFdecPww3U&e=>
> -- 
> <https://urldefense.proofpoint.com/v2/url?u=https-3A__www.openbravo.com&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=SONxRA-cJWH4INLX-uDgL1Afx7XABorqPK3ite36VtA&s=qLyjnBLTJ0nP65gCeCPRY2IqJC0l9sOfxepjQ0AnXfM&e=>
> Carlos Aristu López
> Applications Engineer
> /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./
> /Your data is processed by Openbravo under our privacy policy 
> <https://urldefense.proofpoint.com/v2/url?u=https-3A__www.openbravo.com_privacy-2Dpolicy_&d=DwMFaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=CUkXBxBNT_D5N6HMJ5T9Z6rmvNKYsqupcbk72K0lcoQ&m=SONxRA-cJWH4INLX-uDgL1Afx7XABorqPK3ite36VtA&s=S2N4L5FShXFXrm0d1AT3OyWLXYYH51PCWsdofw_twng&e=>/
> _______________________________________________
> GraalVM-Users mailing list
> GraalVM-Users at oss.oracle.com
> https://oss.oracle.com/mailman/listinfo/graalvm-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20190726/5f02944a/attachment-0001.html 

More information about the GraalVM-Users mailing list