Skip to content

Instantly share code, notes, and snippets.

@shelajev
Created April 19, 2021 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelajev/9b35e504c63da77fddd86f5497e95be8 to your computer and use it in GitHub Desktop.
Save shelajev/9b35e504c63da77fddd86f5497e95be8 to your computer and use it in GitHub Desktop.
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.Context.Builder;
import org.graalvm.polyglot.proxy.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
Map<String, Object> javaMap = new HashMap<>();
javaMap.put("name", "GraalVM");
javaMap.put("version", 21.1);
context.getBindings("js").putMember("javaMap", javaMap);
Value jsObject = context.eval("js",
"{ " +
"print(javaMap.name);" +
"print(javaMap.version);" +
" new Map([['hello','world'],[1, 42]]);"+
"}");
var map = jsObject.as(Map.class);
System.out.println(map + "; " + map.get("hello"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment