I am migrating a project from Nashorn to graal.js. The project utilizes a large amount of scripts (over 3,400) and there's a function on the Java side which invokes a method; the method returns a JavaScript list of objects.
function filterList(ob)
{
var list = [];
var arr = ob.toArray();
for(var i = 0; i < arr.length; i++)
{
if(global.isValid(arr[i]))
{
list.push(arr[i]);
}
}
return list;
}
This worked fine on Nashorn previously with the use of ScriptUtils. This code was written by the developer who worked on the project before I picked it up:
try {
Object p = iv.invokeFunction("filterList", this.getList());
if(p != null) {
List<MyObj> lObj = new ArrayList<>(((Map<String, MyObj>)(ScriptUtils.convert(p, Map.class))).values());
return lObj;
}
} catch (ScriptException | NoSuchMethodException ex) {
ex.printStackTrace();
}
How can I access the array through Java with graal.js? I have tried using Value.asValue(p)as(MyObj[])
to no avail. I have also tried following the Nashorn migration guide where they suggest to cast the object to List or Map, to no avail either.
I'm aware of a solution where I would have to rewrite the script to just use new Java.type('java.util.ArrayList');
and return a List
rather than an array - however there are thousands of scripts and rewriting all of them will be incredibly tedious.