graalvm / graal-js-jdk11-maven-demo

An example project how to run Graal/JavaScript on JDK 11 with Graal as optimizing JIT compiler for best performance.
Universal Permissive License v1.0
177 stars 63 forks source link

How to work with PolyglotMap #25

Closed wlnirvana closed 4 years ago

wlnirvana commented 5 years ago

I have something similar to the following, where invokeFunction returns an object myDataObject here) internally of the type PolyglotMap, but can only be typed statically as Object. I'm wondering how the data can be extracted out.

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("graal.js");
        URL url = Foobar.class.getClassLoader().getResource("foobar.js");
        engine.eval(Files.newBufferedReader(Paths.get(url.toURI()), StandardCharsets.UTF_8));

        Invocable inv = (Invocable) engine;
        var myDataObject =  (inv.invokeFunction("main", "testing data"));
wirthi commented 4 years ago

Hi,

PolyglotMap implements java.util.Map so that's how you can access it.

Best, Christian