- Introduction
- Compile and Run a Polyglot Application
- Define Guest Language Functions as Java Values
- Access Guest Languages Directly from Java
- Access Java from Guest Languages
- Lookup Java Types from Guest Languages
- Computed Arrays Using Polyglot Proxies
- Access Restrictions
- Build Native Images from Polyglot Applications
- Code Caching Across Multiple Contexts
- Build a Shell for Many Languages
- Step through with Execution Listeners
Introduction
The GraalVM Polyglot API lets you embed and run code from guest languages in JVM-based host applications.
Throughout this section, you learn how to create a host application in Java thatruns on GraalVM and directly calls a guest language. You can use the tabsbeneath each code example to choose between JavaScript, R, Ruby and Python.
Ensure you set up GraalVM before you begin. SeeGet Started.
Alternatively, you can have a look at the reference documentation in Javadoc:
- The Polyglot Package allows you to configure and run polyglot applications.
- The Proxy Package allows you to mimic guest language objects using proxies.
- The IO Package allows you to customize the file system access of languages.
Compile and Run a Polyglot Application
Polyglot applications run code written in any language implemented with the Truffle Language Implementation Framework.These languages are henceforth referenced as guests languages.
Complete the steps in this section to create a sample polyglotapplication that runs on GraalVM and demonstrates programming languageinteroperability.
Create a
hello-polyglot
project directory.In your project directory, add a
HelloPolyglot.java
file that includesthe following code:
// COMPILE-CMD: javac {file}
// RUN-CMD: java {file}
// BEGIN-SNIPPET
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
// END-SNIPPET
public class hello_polyglot_js {
static
// BEGIN-SNIPPET
public class HelloPolyglot {
public static void main(String[] args) {
System.out.println("Hello Java!");
try (Context context = Context.create()) {
context.eval("js", "print('Hello JavaScript!');");
}
}
}
// END-SNIPPET
public static void main(String[] args) {
HelloPolyglot.main(null);
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java {file}
// BEGIN-SNIPPET
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
// END-SNIPPET
public class hello_polyglot_R {
static
// BEGIN-SNIPPET
public class HelloPolyglot {
public static void main(String[] args) {
System.out.println("Hello Java!");
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.eval("R", "print('Hello R!');");
}
}
}
// END-SNIPPET
public static void main(String[] args) {
HelloPolyglot.main(null);
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java {file}
// BEGIN-SNIPPET
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
// END-SNIPPET
public class hello_polyglot_ruby {
static
// BEGIN-SNIPPET
public class HelloPolyglot {
public static void main(String[] args) {
System.out.println("Hello Java!");
try (Context context = Context.create()) {
context.eval("ruby", "puts 'Hello Ruby!'");
}
}
}
// END-SNIPPET
public static void main(String[] args) {
HelloPolyglot.main(null);
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java {file}
// BEGIN-SNIPPET
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
// END-SNIPPET
public class hello_polyglot_python {
static
// BEGIN-SNIPPET
public class HelloPolyglot {
public static void main(String[] args) {
System.out.println("Hello Java!");
try (Context context = Context.create()) {
context.eval("python", "print('Hello Python!')");
}
}
}
// END-SNIPPET
public static void main(String[] args) {
HelloPolyglot.main(null);
}
}
In this code:
import org.graalvm.polyglot.*
imports the base API for the Polyglot API.import org.graalvm.polyglot.proxy.*
imports the proxy classes of the Polyglot API, needed in later examples.Context
provides an execution environment for guest languages.R currently requires theallowAllAccess
flag to be set totrue
to run the example.eval
evaluates the specified snippet of guest language code.- The
try
with resource statement initializes theContext
and ensures that itis closed after use. Closing the context ensures that all resources includingpotential native resources are freed eagerly. Closing a context is optional butrecommended. Even if a context is not closed and no longer referenced it will befreed by the garbage collector automatically.
Run
javac HelloPolyglot.java
to compileHelloPolyglot.java
withGraalVM.Run
java HelloPolyglot
to run the application on GraalVM.
You now have a polyglot application that consists of a Java host applicationand guest language code that run on GraalVM. You can use this application withother code examples to demonstrate more advanced capabilities of thePolyglot API.
To use other code examples in this section, you simply need to do the following:
Add the code snippet to the main method of
HelloPolyglot.java
.Compile and run your polyglot application.
Define Guest Language Functions as Java Values
Polyglot applications let you take values from one programming language anduse them with other languages.
Use the code example in this section with your polyglot application to showhow the Polyglot API can return JavaScript, R, Ruby or Python functions asJava values.
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class function_js {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.create()) {
Value function = context.eval("js", "x => x+1");
assert function.canExecute();
int x = function.execute(41).asInt();
assert x == 42;
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class function_R {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
Value function = context.eval("R", "function(x) x + 1");
assert function.canExecute();
int x = function.execute(41).asInt();
assert x == 42;
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class function_ruby {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.create()) {
Value function = context.eval("ruby", "proc { |x| x + 1 }");
assert function.canExecute();
int x = function.execute(41).asInt();
assert x == 42;
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class function_python {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.create()) {
Value function = context.eval("python", "lambda x: x + 1");
assert function.canExecute();
int x = function.execute(41).asInt();
assert x == 42;
}
// END-SNIPPET
}
}
In this code:
Value function
is a Java value that refers to a function.- The
eval
call parses the script and returns the guest language function. - The first assertion checks that the value returned by the code snippet can beexecuted.
- The
execute
call executes the function with the argument41
. - The
asInt
call converts the result to a Javaint
. - The second assertion verifies that the result was incremented by one as expected.
Access Guest Languages Directly from Java
Polyglot applications can readily access most language types and are notlimited to functions. Host languages, such as Java, can directly access guestlanguage values embedded in the polyglot application.
Use the code example in this section with your polyglot application to showhow the Polyglot API can access objects, numbers, strings, and arrays.
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class access_js_from_java {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.create()) {
Value result = context.eval("js",
"({ " +
"id : 42, " +
"text : '42', " +
"arr : [1,42,3] " +
"})");
assert result.hasMembers();
int id = result.getMember("id").asInt();
assert id == 42;
String text = result.getMember("text").asString();
assert text.equals("42");
Value array = result.getMember("arr");
assert array.hasArrayElements();
assert array.getArraySize() == 3;
assert array.getArrayElement(1).asInt() == 42;
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class access_R_from_java {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
Value result = context.eval("R",
"list(" +
"id = 42, " +
"text = '42', " +
"arr = c(1,42,3)" +
")");
assert result.hasMembers();
int id = result.getMember("id").asInt();
assert id == 42;
String text = result.getMember("text").asString();
assert text.equals("42");
Value array = result.getMember("arr");
assert array.hasArrayElements();
assert array.getArraySize() == 3;
assert array.getArrayElement(1).asInt() == 42;
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class access_ruby_from_java {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.create()) {
Value result = context.eval("ruby",
"o = Struct.new(:id, :text, :arr).new(" +
"42, " +
"'42', " +
"[1,42,3] " +
")");
assert result.hasMembers();
int id = result.getMember("id").asInt();
assert id == 42;
String text = result.getMember("text").asString();
assert text.equals("42");
Value array = result.getMember("arr");
assert array.hasArrayElements();
assert array.getArraySize() == 3;
assert array.getArrayElement(1).asInt() == 42;
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
public class access_python_from_java {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.create()) {
Value result = context.eval("python",
"type('obj', (object,), {" +
"'id' : 42, " +
"'text': '42', " +
"'arr' : [1,42,3]" +
"})()");
assert result.hasMembers();
int id = result.getMember("id").asInt();
assert id == 42;
String text = result.getMember("text").asString();
assert text.equals("42");
Value array = result.getMember("arr");
assert array.hasArrayElements();
assert array.getArraySize() == 3;
assert array.getArrayElement(1).asInt() == 42;
}
// END-SNIPPET
}
}
In this code:
Value result
is an Object that contains three members: a number namedid
,a string namedtext
, and an array namedarr
.- The first assertion verifies that the return value can contain members, whichindicates that the value is an object-like structure.
- The
id
variable is initialized by reading the member with the nameid
fromthe resulting object. The result is then converted to a Javaint
usingasInt()
. - The next assert verifies that result has a value of
42
. - The
text
variable is initialized using the value of the membertext
, which is also converted to a JavaString
usingasString()
. - The following assertion verifies the result value is equal to theJava
String
"42"
. - Next the
arr
member that holds an array is read. - Arrays return
true
forhasArrayElements
. R array instances can havemembers and array elements at the same time. - The next assertion verifies that the size of the array equals three. ThePolyglot API supports big arrays, so the array length is of type
long
. - Finally we verify that the array element at index
1
equals42
. Arrayindexing with polyglot values is always zero-based, even for languages such asR where indices start with one.
Access Java from Guest Languages
Polyglot applications offer bi-directional access between guest languages andhost languages. As a result, you can pass Java objects to guest languages.
Use the code example in this section with your polyglot application to show howguest languages can access primitive Java values, objects, arrays, andfunctional interfaces.
To permit guest languages to access any public method or field of a Javaobject, set allowAllAccess(true)
when the context is built. In this mode, the guestlanguage code must be fully trusted, as it can access other not explicitly exported Java methodsusing reflection. A later section describes how to run less trusted code with the Polyglot API.
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import java.util.concurrent.Callable;
import org.graalvm.polyglot.*;
public class access_java_from_js {
// BEGIN-SNIPPET
public static class MyClass {
public int id = 42;
public String text = "42";
public int[] arr = new int[]{1, 42, 3};
public Callable<Integer> ret42 = () -> 42;
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.getBindings("js").putMember("javaObj", new MyClass());
boolean valid = context.eval("js",
" javaObj.id == 42" +
" && javaObj.text == '42'" +
" && javaObj.arr[1] == 42" +
" && javaObj.ret42() == 42")
.asBoolean();
assert valid == true;
}
}
// END-SNIPPET
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import java.util.concurrent.Callable;
import org.graalvm.polyglot.*;
public class access_java_from_R {
// BEGIN-SNIPPET
public static class MyClass {
public int id = 42;
public String text = "42";
public int[] arr = new int[]{1, 42, 3};
public Callable<Integer> ret42 = () -> 42;
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.getBindings("R").putMember("javaObj", new MyClass());
boolean valid = context.eval("R",
" javaObj$id == 42" +
" && javaObj$text == '42'" +
" && javaObj$arr[[2]] == 42" +
" && javaObj$ret42() == 42")
.asBoolean();
assert valid == true;
}
}
// END-SNIPPET
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import java.util.concurrent.Callable;
import org.graalvm.polyglot.*;
public class access_java_from_ruby {
// BEGIN-SNIPPET
public static class MyClass {
public int id = 42;
public String text = "42";
public int[] arr = new int[]{1, 42, 3};
public Callable<Integer> ret42 = () -> 42;
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.getPolyglotBindings().putMember("javaObj", new MyClass());
boolean valid = context.eval("ruby",
"javaObj = Polyglot.import('javaObj')\n" +
" javaObj[:id] == 42" +
" && javaObj[:text] == '42'" +
" && javaObj[:arr][1] == 42" +
" && javaObj[:ret42].call == 42")
.asBoolean();
assert valid == true;
}
}
// END-SNIPPET
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import java.util.concurrent.Callable;
import org.graalvm.polyglot.*;
public class access_java_from_python {
// BEGIN-SNIPPET
public static class MyClass {
public int id = 42;
public String text = "42";
public int[] arr = new int[]{1, 42, 3};
public Callable<Integer> ret42 = () -> 42;
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.getPolyglotBindings().putMember("javaObj", new MyClass());
boolean valid = context.eval("python",
"import polyglot \n" +
"javaObj = polyglot.import_value('javaObj')\n" +
"javaObj['id'] == 42" +
" and javaObj['text'] == '42'" +
" and javaObj['arr'][1] == 42" +
" and javaObj['ret42'].call() == 42")
.asBoolean();
assert valid == true;
}
}
// END-SNIPPET
}
In this code:
- The Java class
MyClass
has four public fieldsid
,text
,arr
andret42
. The fields are initialized with42
,"42"
,new int[]{1, 42, 3}
andlambda() -> 42
that always returns anint
value of42
. - The Java class
MyClass
is instantiated and exported with the namejavaObj
into the polyglot scope, which allows the host and guest languages to exchangesymbols. - A guest language script is evaluated that imports the
javaObj
symbol andassigns it to the local variable which is also namedjavaObj
. To avoidconflicts with variables, every value in the polyglot scope must be explicitlyimported and exported in the top-most scope of the language. - The next two lines verify the contents of the Java object by comparing itto the number
42
and the string'42'
. - The third verification reads from the second array position and compares itto the number
42
. Whether arrays are accessed using 0-based or 1-based indicesdepends on the guest language. Independently of the language, the Java arraystored in fieldarr
is always accessed using translated 0-based indices. Forexample, in the R language, arrays are 1-based so the second array element isaccessible using index2
. In the JavaScript and Ruby languages, the secondarray element is at index1
. In all language examples, the Java array is readfrom using the same index1
. - The last line invokes the Java lambda that is contained in the field
ret42
and compares the result to the number value42
. - After the guest language script executes, validation takes place to ensurethat the script returns a
boolean
value oftrue
as a result.
Lookup Java Types from Guest Languages
In addition to passing Java objects to the guest language, it is possibleto allow the lookup of Java types in the guest language.
Use the code example in this section with your polyglot application to show howguest languages lookup Java types and instantiate them.
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
public class lookup_java_from_js {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
java.math.BigDecimal v = context.eval("js",
"var BigDecimal = Java.type('java.math.BigDecimal');" +
"BigDecimal.valueOf(10).pow(20)")
.asHostObject();
assert v.toString().equals("100000000000000000000");
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
public class lookup_java_from_R {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
java.math.BigDecimal v = context.eval("R",
"BigDecimal = java.type('java.math.BigDecimal');\n" +
"BigDecimal$valueOf(10)$pow(20)")
.asHostObject();
assert v.toString().equals("100000000000000000000");
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
public class lookup_java_from_ruby {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
java.math.BigDecimal v = context.eval("ruby",
"BigDecimal = Java.type('java.math.BigDecimal')\n" +
"BigDecimal.valueOf(10).pow(20)")
.asHostObject();
assert v.toString().equals("100000000000000000000");
}
// END-SNIPPET
}
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
public class lookup_java_from_python {
public static void main(String[] args) {
// BEGIN-SNIPPET
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
java.math.BigDecimal v = context.eval("python",
"import java\n" +
"BigDecimal = java.type('java.math.BigDecimal')\n" +
"BigDecimal.valueOf(10).pow(20)")
.asHostObject();
assert v.toString().equals("100000000000000000000");
}
// END-SNIPPET
}
}
In this code:
- A new context is created with all access enabled (
allowAllAccess(true)
). - A new context is created with all access enabled (
allowAllAccess(true)
). - A guest language script is evaluated.
- The script looks the Java type
java.math.BigDecimal
up and stores it in a variable namedBigDecimal
. - The static method
BigDecimal.valueOf(long)
is invoked to create newBigDecimal
s with value10
. In addition to looking up static Java methods, itis also possible to directly instantiate the returned Java type., e.g. inJavaScript using thenew
keyword. - The new decimal is used to invoke the
pow
instance method with20
which calculates10^20
. - The result of the script is converted to a host object by calling
asHostObject()
. The return value is automatically cast to theBigDecimal
type. - The result decimal string is asserted to equal to
"100000000000000000000"
.
Computed Arrays Using Polyglot Proxies
The Polyglot API includes polyglot proxy interfaces that let youcustomize Java interoperability by mimicking guest language types, such asobjects, arrays, native objects, or primitives.
Use the code example in this section with your polyglot application to see howyou can implement arrays that compute their values lazily.
Note: The Polyglot API supports polyglot proxies either on the JVM orin a native image executable.
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
public class proxy_js {
// BEGIN-SNIPPET
static class ComputedArray implements ProxyArray {
public Object get(long index) {
return index * 2;
}
public void set(long index, Value value) {
throw new UnsupportedOperationException();
}
public long getSize() {
return Long.MAX_VALUE;
}
}
public static void main(String[] args) {
try (Context context = Context.create()) {
ComputedArray arr = new ComputedArray();
context.getBindings("js").putMember("arr", arr);
long result = context.eval("js",
"arr[1] + arr[1000000000]")
.asLong();
assert result == 2000000002L;
}
}
// END-SNIPPET
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
public class proxy_R {
// BEGIN-SNIPPET
static class ComputedArray implements ProxyArray {
public Object get(long index) {
return index * 2;
}
public void set(long index, Value value) {
throw new UnsupportedOperationException();
}
public long getSize() {
return Long.MAX_VALUE;
}
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
ComputedArray arr = new ComputedArray();
context.getPolyglotBindings().putMember("arr", arr);
long result = context.eval("R",
"arr <- import('arr');" +
"arr[2] + arr[1000000001]")
.asLong();
assert result == 2000000002L;
}
}
// END-SNIPPET
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
public class proxy_ruby {
// BEGIN-SNIPPET
static class ComputedArray implements ProxyArray {
public Object get(long index) {
return index * 2;
}
public void set(long index, Value value) {
throw new UnsupportedOperationException();
}
public long getSize() {
return Long.MAX_VALUE;
}
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
ComputedArray arr = new ComputedArray();
context.getPolyglotBindings().putMember("arr", arr);
long result = context.eval("ruby",
"arr = Polyglot.import('arr') \n" +
"arr[1] + arr[1000000000]")
.asLong();
assert result == 2000000002L;
}
}
// END-SNIPPET
}
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
public class proxy_python {
// BEGIN-SNIPPET
static class ComputedArray implements ProxyArray {
public Object get(long index) {
return index * 2;
}
public void set(long index, Value value) {
throw new UnsupportedOperationException();
}
public long getSize() {
return Long.MAX_VALUE;
}
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
ComputedArray arr = new ComputedArray();
context.getPolyglotBindings().putMember("arr", arr);
long result = context.eval("python",
"import polyglot\n" +
"arr = polyglot.import_value('arr') \n" +
"arr[1] + arr[1000000000]")
.asLong();
assert result == 2000000002L;
}
}
// END-SNIPPET
}
In this code:
- The Java class
ComputedArray
implements the proxy interfaceProxyArray
sothat guest languages treat instances of the Java class like arrays. ComputedArray
array overrides the methodget
and computes the valueusing an arithmetic expression.- The array proxy does not support write access. For this reason, it throwsan
UnsupportedOperationException
in the implementation ofset
. - The implementation for
getSize
returnsLong.MAX_VALUE
for its length. - The main method creates a new polyglot execution context.
- A new instance of the
ComputedArray
class is then exported using the namearr
. - The guest language script imports the
arr
symbol, which returns theexported proxy. - The second element and the
1000000000
th element is accessed, summed up, andthen returned. Note that array indices from 1-based languages such as R areconverted to 0-based indices for proxy arrays. - The result of the language script is returned as a long value and verified.
For more information about the polyglot proxy interfaces, see thePolyglot API JavaDoc.
Access Restrictions
The Polyglot API by default restricts access to certain critical functionality, such as file I/O.These restrictions can be lifted entirely by setting allowAllAccess
to true
.
Important: Access restrictions are currently only supported with JavaScript.
Configuring Host Access
It might be desirable to limit the access of guest applications to the host.For example, if a Java method is exposed that calls System.exit
then the guestapplication will be able to exit the host process.In order to avoid accidentally exposed methods, no host access is allowed bydefault and every public method or field needs to be annotated with@HostAccess.Export
explicitly.
// COMPILE-CMD: javac {file}
// RUN-CMD: java -ea {file}
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.PolyglotException;
public class explicit_access_java_from_js {
static
// BEGIN-SNIPPET
public class Employee {
private final String name;
Employee(String name) {this.name = name;}
@HostAccess.Export
public String getName() {
return name;
}
}//END-SNIPPET
static//BEGIN-SNIPPET
public class Services {
@HostAccess.Export
public Employee createEmployee(String name) {
return new Employee(name);
}
public void exitVM() {
System.exit(1);
}
}
public static void main(String[] args) {
try (Context context = Context.create()) {
Services services = new Services();
context.getBindings("js").putMember("services", services);
String name = context.eval("js",
"let emp = services.createEmployee('John Doe');" +
"emp.getName()").asString();
assert name.equals("John Doe");
try {
context.eval("js", "services.exitVM()");
assert false;
} catch (PolyglotException e) {
assert e.getMessage().endsWith(
"Unknown identifier: exitVM");
}
}
}
// END-SNIPPET
}
In this code:
- The class
Employee
is declared with a fieldname
of typeString
. Access to thegetName
method is explicitly allowed by annotating the method with@HostAccess.Export
. - The
Services
class exposes two methodscreateEmployee
andexitVM
. ThecreateEmployee
method takes the name of the employee as an argument and creates a newEmployee
instance. ThecreateEmployee
method is annotated with@HostAccess.Export
and therefore accessible to the guest application. TheexitVM
method is not explicitly exported and therefore not accessible. - The
main
method first creates new polyglot context in the default configuration, disallowing host access except for methods annotated with@HostAccess.Export
. - A new
Services
instance is created and put into the context as global variableservices
. - The first evaluated script creates a new employee using the services object and returns its name.
- The returned name is asserted to equal the expected name
John Doe
. - A second script is evaluated that calls the
exitVM
method on the services object. This fails with aPolyglotException
as the exitVM method is not exposed to the guest application.
Host access is fully customizable by creating a custom HostAccess
policy.
Access Privilege Configuration
It is possible to configure fine-grained access privileges for guest applications.The configuration can be provided using the Context.Builder
class when constructing a new context.The following access parameters may be configured:
- Allow access to other languages using
allowPolyglotAccess
. - Allow and customize access to host objects using
allowHostAccess
. - Allow and customize host lookup to host types using
allowHostLookup
. - Allow host class loading using
allowHostClassLoading
. - Allow the creation of threads using
allowCreateThread
. - Allow access to native APIs using
allowNativeAccess
. - Allow access to IO using
allowIO
and proxy file accesses usingfileSystem
.
Important: Granting access to class loading, native APIs or host IO effectively grants all access, as these privileges can be used to bypass other access restrictions.
Build Native Images from Polyglot Applications
Polyglot embeddings can also be compiled using GraalVM Native Image.By default, no language is included if the polyglot API is used.To enable guest languages the —language:<languageId>
(e.g. —language:js
) native image option needs to be specified.All examples on this page are also supported when compiled using Native Image Generator – the native-image
utility.Currently it is required to set the —initialize-at-build-time
option when building with a polyglot language. We plan to lift this restriction in future versions.
The following example shows how a simple HelloWorld JavaScript application can be built using native-image
:
$ javac HelloPolyglot.java
$ native-image --language:js --initialize-at-build-time -cp . HelloPolyglot
$ ./HelloPolyglot
Configuring Native Host Reflection
Accessing host Java code from the guest application requires Java reflection in order to work.When reflection is used within a native image, the reflection configuration file is required.
For this example we use JavaScript to show host access with native images.Copy the following code in a new file named AccessJavaFromJS.java
.
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
import java.util.concurrent.*;
public class AccessJavaFromJS {
public static class MyClass {
public int id = 42;
public String text = "42";
public int[] arr = new int[]{1, 42, 3};
public Callable<Integer> ret42 = () -> 42;
}
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.getBindings("js").putMember("javaObj", new MyClass());
boolean valid = context.eval("js",
" javaObj.id == 42" +
" && javaObj.text == '42'" +
" && javaObj.arr[1] == 42" +
" && javaObj.ret42() == 42")
.asBoolean();
System.out.println("Valid " + valid);
}
}
}
Next, copy the following code into reflect.json
:
[
{ "name": "AccessJavaFromJS$MyClass", "allPublicFields": true },
{ "name": "java.util.concurrent.Callable", "allPublicMethods": true }
]
Now the a native image can be created that supports host access:
$ javac AccessJavaFromJS.java
$ native-image --language:js --initialize-at-build-time -H:ReflectionConfigurationFiles=reflect.json -cp . AccessJavaFromJS
$ ./accessjavafromjs
Note that in case assertions are needed in the image the -H:+RuntimeAssertions
option can be passed to native-image
.For production deployments, this option should be omitted.
Code Caching Across Multiple Contexts
The GraalVM Polyglot API allows enabling code caching across multiple contexts.Code caching allows compiled code to be reused and allows sources to be parsed only once.Often, code caching can reduce memory consumption and warmup time of the application.
By default, code is cached within a single context instance only.In order to enable code caching between multiple contexts an explicit engine needs to be specified.The engine is specified when creating the context using the context builder.The scope of code sharing is determined by the engine instance.Code is only shared between contexts associated with one engine instance.
All sources are cached by default.Caching may be disabled explicitly by setting cached(boolean cached) to false
. Disabling caching may be useful in case the source is known to only be evaluated once.
Consider the following code snippet as an example.
import org.graalvm.polyglot.*;
public class Main {
public static void main(String[] args) {
try (Engine engine = Engine.create()) {
Source source = Source.create("js", "21 + 21");
try (Context context = Context.newBuilder()
.engine(engine)
.build()) {
int v = context.eval(source).asInt();
assert v == 42;
}
try (Context context = Context.newBuilder()
.engine(engine)
.build()) {
int v = context.eval(source).asInt();
assert v == 42;
}
}
}
}
In this code:
import org.graalvm.polyglot.*
imports the base API for the Polyglot API.Engine.create()
creates a new engine instance with the default configuration.Source.create()
creates a source object for the expression “21 + 21”with “js” language, which is the language identifier for JavaScript.Context.newBuilder().engine(engine).build()
builds a new context withan explicit engine assigned to it. All contexts associated with an engine share the code.context.eval(source).asInt()
evaluates the source and returns the result asValue
instance.
Build a Shell for Many Languages
With just a few lines of code, the GraalVM Polyglot API lets you buildapplications that integrate with any guest language that is available bydefault with GraalVM.
This shell implementation is agnostic to any particular guest language.
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintStream output = System.out;
Context context = Context.newBuilder().allowAllAccess(true).build();
Set<String> languages = context.getEngine().getLanguages().keySet();
output.println("Shell for " + languages + ":");
String language = languages.iterator().next();
for (;;) {
try {
output.print(language + "> ");
String line = input.readLine();
if (line == null) {
break;
} else if (languages.contains(line)) {
language = line;
continue;
}
Source source = Source.newBuilder(language, line, "<shell>")
.interactive(true).buildLiteral();
context.eval(source);
} catch (PolyglotException t) {
if(t.isExit()) {
break;
}
t.printStackTrace();
}
}
Step through with Execution Listeners
GraalVM Polyglot API allows to instrument the execution of guest languages through ExecutionListener class. For example, it lets you attach an execution listener that is invoked for every statement of the guest language program. Execution listenersare designed as simple API for polyglot embedders and may become handy in, e.g., single-stepping through the program.
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.management.*;
public class ExecutionListenerTest {
public static void main(String[] args) {
try (Context context = Context.create("js")) {
ExecutionListener listener = ExecutionListener.newBuilder()
.onEnter((e) -> System.out.println(
e.getLocation().getCharacters()))
.statements(true)
.attach(context.getEngine());
context.eval("js", "for (var i = 0; i < 2; i++);");
listener.close();
}
}
}
In this code:
- The
Context.create()
call creates a new context for the guest language. - Create an execution listener builder by invoking
ExecutionListeners.newBuilder()
. - Set
onEnter
event to notify when element’s execution is entered and consumed. At least one event consumer and one filtered source element needs to be enabled. - To complete the listener attachment,
attach()
needs to be invoked. - The
statements(true)
filters execution listeners to statements only. - The
context.eval()
call evaluates a specified snippet of guest language code. - The
listener.close()
closes a listener earlier, however execution listeners are automatically closed with the engine.