Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created May 19, 2018 15:40
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 lucaswerkmeister/391b59e415d17cd2d8282170338a23cf to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/391b59e415d17cd2d8282170338a23cf to your computer and use it in GitHub Desktop.
A brief look at Graal
public class Hello {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "World";
System.out.println("Hello, " + name + "!");
}
}
import org.graalvm.polyglot.*;
public class HelloPolyglot {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "World";
System.out.println("Hello, " + name + ", from Java!");
Context context = Context.create();
context.getPolyglotBindings().putMember("name", name);
context.eval("js",
"const name = Interop.import('name');\n" +
"print('Hello, ' + name + ', from JavaScript!');");
}
}
jsPlus = eval.polyglot('js', 'function(s1, s2) { return s1 + s2; }')
rubyOne = eval.polyglot('ruby', '1')
pythonOne = eval.polyglot('python', '1')
print(jsPlus(rubyOne, pythonOne))
@lucaswerkmeister
Copy link
Author

The Java examples can be compiled with javac (assuming Graal’s bin is in the PATH) and run with java; instead of running them, you can also create native images with native-image (add --language:js for HelloPolyglot).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment