I have a Map in Scala/Java I want to be visible in Javascript running on graal.js engine.
case class Thing() {
def foo() { println("FOO!") } // just to see if this is callable from js (it is)
val m = Map("foo" -> "bar", "one" -> 1)
val d1 = m
val d2 = m.asJava
val d3 = toGraalValue(m)
val d4 = MapProxyObject(m.map { case (k, v) => (k.toString, toGraalValue(v)) })
def toGraalValue(a: Any): Value =
a match {
case s: List[_] => Value.asValue(ListProxyArray(s.map(toGraalValue).toArray))
case m: Map[_, _] => Value.asValue(MapProxyObject(m.map { case (k, v) => (k.toString, toGraalValue(v)) }))
case _ => Value.asValue(a)
}
}
Later, a Javascript function in graal.js is called with:
inv.invokeFunction(bindFn, args: _*)
Where bindFn is a compiled function (below) and args is a 1-element list containing my Thing object.
Javascript:
function(thing) {
console.log(thing.d1);
console.log(thing.d2);
console.log(thing.d3);
console.log(thing.d4);
console.log(thing.foo());
}
The output from thing.foo() worked, but all the others resolved to 'foreign {}' in Javascript. None of them have any values in the Map.
How can I get Map data created on the JVM visible in graal.js Javascript code (preferably natively to Javascript)?