graaljs: Arrays passed to JS using ProxyArray do not mimic JS arrays

I have a Java host application that is executing JS scripts as guest using the Polyglot API. I would like to pass Java arrays (ArrayList) to the JS side and treat the array on the JS side as if it was a “native” JS array. As suggested in this SOF post: https://stackoverflow.com/questions/51707039/injected-members-from-the-host-language-to-arrive-to-guest-language-as-guest-lan I am using ProxyArray to achieve that. Specifically, I am using ProxyArray.fromArray() to pass the Java ArrayList to the JS side using bindings:

Value guestLanguageBindings = context.getBindings("js"); guestLanguageBindings.putMember("arrParamName", ProxyArray.fromArray(arrayArg));

However in the JS side (guest language) the array is not seen as a JS array, instead it is of type: org.graalvm.polyglot.proxy.ProxyArray (also a Java type). This prevents me from being able to use the array as if it were a regular JS array. Statements like arr[0] work, however specific calls such as arr.sort() fail.

The screenshot below illustrate a debugging session where variable is passed as described above with the resulting error.

screen shot 2018-08-20 at 18 17 05

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I looked at the R issue - there’s a bug which makes it only accept R-specific data types for bindings, which will be fixed soon. @timfel - would be great if you could take a look at the Python issue.

FYI: js.experimental-array-prototype option that adds Array.prototype on the prototype chain of array-like non-JavaScript objects will be removed in GraalVM 1.0.0-rc14. It will replaced by js.experimental-foreign-object-prototype option. We changed the name of this option because besides adding Array.prototype it also adds Function.prototype on the prototype chain of executable non-JavaScript objects and Object.prototype on the prototype chain of all non-JavaScript objects now.