GraalVM how to transfer java List object to javascript so that i can make .push() an .forEach() on them?

Viewed 562

The problem is when I trying to transfer java List (ArrayList) object to javascript as ProxyArray (ProxyArray.fromList()) it can't be used with .forEach and .push. Also if I transfer java List object as is it can be used with .forEach but not with .push. Is there any solution?

Here is gist https://gist.github.com/okrean/5d6978bac15f257b90f581f371769935

1 Answers

An instance of java.util.ArrayList behaves like a JS Array in some ways, however, instances of java.util.ArrayList do not have their prototype set to Array.prototype.

That's the reason why the push function is not found on your value, and the forEach function that is called is actually the forEach function found on the Java object, not the forEach from the JS array.

A workaround is to convert the java.util.ArrayList to a JS Array manually using Java.from(value) in JS.

See also the discussion in this issue.

Related