-2

I am trying to migrate from nashorn to graalvm (java 11). I have a lot of java script.

Description -> Connecting to java project as maven libs org.graalvm.js: graal-sdk, js, js-scriptengine, org.graalvm.truffle: truffle-api. Version 1.0.0-rc9.
ScriptEngine created like 'new ScriptEngineManager().getEngineByName("graal.js")';
Then you can create map in array in map like:

Map mapOuter = new HashMap();
Map mapInner = new HashMap();
List arrayInner = new ArrayList();

mapInner.put("1", 3);
arrayInner.add(mapInner);
mapOuter.put("2", arrayInner);

Then convert mapOuter to ProxyObject like ProxyObject.fromMap(mapOuter);
Evaluate test javascript with code like:

function test(param) {
     print(param["2"]);
     param["2"].forEach(function(inArray){
       print(inArray);
     });
}

Which will invoke error like:

javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: INVOKE on foreign object failed due to: Message not supported: INVOKE

So the question is: how to make graalvm to transfer complex java object to javascript function properly?

p.s. What i want in this example is to call object like:

print(param["2"][0]["1"]);

and it prints me 3.

What i tried:
tried to parse each member of outerMap as ProxyObject/ProxyArray.

What was working If you change javascript like:

print(param["2"][0].get("1"));

it will prints 3;

Alicia
  • 1,152
  • 1
  • 23
  • 41

1 Answers1

0

Currently (Dec 2018) using [] to access values of a Java Map is not supported.You need to convert maps to a ProxyObject to make [] work. Like you did with the mapOuter object.

Otherwise it's a normal Java map on which you call .get(key) to obtain the value.

Oleg Šelajev
  • 3,530
  • 1
  • 17
  • 25
  • I used ProxyObject, but it's not working. I also tried to use [ProxyObject in ProxyArray in ProxyObject] construction but it's not working. – Nickolay Kim Dec 20 '18 at 09:01
  • If I do it like in the following gist it works for me: https://gist.github.com/shelajev/5529229d2913d952529d249251f9abcb. Can you please elaborate the question or provide a better code example? – Oleg Šelajev Dec 20 '18 at 09:53
  • Thank you it worked, but there is another problem -> "java.lang.IllegalStateException: Multi threaded access requested by thread Thread[] but is not allowed for language(s) js" such error invoking alsmot every run. Is there a way to solve it? – Nickolay Kim Dec 20 '18 at 13:10
  • **In your example you use ArrayList as is, so when i try to use javascript array push() method, it causes to error "javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: INVOKE on JavaObject (java.util.ArrayList)] failed due to: Unknown identifier: push"** – Nickolay Kim Dec 25 '18 at 13:26