Created
January 9, 2018 01:46
-
-
Save AdamMcM/87bed21323aa5ff90ed020412f16eb02 to your computer and use it in GitHub Desktop.
GraalTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import javax.script.*; | |
| public class GraalTest { | |
| public static void main(String[] args) { | |
| try { | |
| ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js"); | |
| // ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); | |
| if (engine != null) { | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append("var Utils = Java.type('test.GraalTest');"); | |
| sb.append("var obj = {};"); | |
| sb.append("obj.n=100;"); | |
| sb.append("print(''+ obj.n);"); | |
| sb.append("Utils.passToJava(obj);"); | |
| engine.eval(sb.toString()); | |
| } else { | |
| System.out.println("null engine"); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void passToJava(Object o) { | |
| System.out.println("class name = " + o.getClass().getName()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment