Open Aerilym opened 1 year ago
I am able to reproduce the missing conversion of JavaScript arrays to Java Collection
. I agree that this is unfortunate. I have no idea why the corresponding code in truffle
does not do that by default (considering that it is willing to convert arrays to Java List
). The consequence is that methods like Collection.addAll()
/Collections.min()
do not work while methods that take List
(like Collections.sort()
) work fine.
You are right that you can add the missing conversion through a custom target type mapping. Personally, I would use
targetTypeMapping(Value.class, Collection.class, Value::hasArrayElements, v -> v.as(List.class))
I don't understand your troubles with errors. I don't see the relation to the mentioned target type mapping. Errors do not have array elements, so they should not be affected at all. I am afraid that I cannot help you without a reproducible test-case.
Versions
Graal Scripting Engine Setup
To run each script file I used the following:
JS Code Example
See MockClass:
Issue
Calling the .addAll() method on a Java HashSet in JavaScript with a JavaScript array as the parameter (See above code example) throws the exception:
See Stack Trace for more information.
The code example at the top works on Nashorn, and it appears the problem is with Graal not mapping the JavaScript Array to a Collection, which Set.addAll() requires [1].
Nashorn Scripting Engine Setup: (OpenJDK 11.0.19)
As before, I ran the same script in the same way:
In Graal, I could get around this issue by adding explicit type maps [2]
This explicit mapping solution came from another issue [2]. Using this type map caused other problems as the type mapping is too generic and caught other data types.
JS Error object no longer being passable to the logger:
The issue is caused by the JSErrorObject being passed into a Java logger class method which does not have a method that takes JSErrorObject. I have been unable to find a way to map JSErrorObject to anything else, as it seems to be indistinguishable from an object. From my research, it seems we can’t directly reference JSErrorObject from truffle. Is there a way to explicitly map JSErrorObject to something else?
Is there a proper way to use JavaScript Arrays in Java methods requiring Collections as their parameters?
Stack Trace
[1] https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Set.html#addAll(java.util.Collection)
[2] https://github.com/oracle/graaljs/issues/3#issuecomment-555661056