I am passing an object "transformContext" from Java into a Graal.js scriptEngine. The object contains a Hashmap called "dataRecordsByName" which in Java I can usually access with the following line:
transformContext.get("dataRecordsByName")
I am attempting to retrieve the hashmap from the engine. I thought to do this I needed to replace the above line with:
engine.eval("transformContext.dataRecordsByName")
However this just returns undefined.
Here is my full code for this task:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
engine.put("transformContext",transformContext);
engine.eval("console.log(transformContext)");
engine.eval("console.log(transformContext.dataRecordsByName)");
And here is the output to the console:
TransformContext(dataRecordsByName={$=[], CONVERSION=[{"From_Currency":"AUD","To_Currency":"AUD","Conversion_Rate":1.0,"historical_date":"31/05/2021","organisation_id":"ParentId","organisation_currency":"GBP","organisation_name":"Parent Id"}],...}, objectMapper=com.fasterxml.jackson.databind.ObjectMapper@21dfdc26)
undefined
Does anyone know why the engine is failing to retrieve the dataRecordsByName property?
After some further research it seems that graal.js does not map properties as I expected by default, but can do so if you run it in nashorn compatability mode.
System.setProperty("polyglot.js.nashorn-compat", "true");After doing this, you can retrieve the object properties as you normally would in JavaScript:
engine.eval("console.log(transformContext.dataRecordsByName)");Thanks to the guys on this thread: https://github.com/oracle/graaljs/issues/169