GraalVM Polyglot API for multi-language runtime environments with host-guest interoperability and security controls.
npx @tessl/cli install tessl/maven-org-graalvm-polyglot--graalvm-sdk@23.1.0GraalVM Polyglot API enables embedding and interoperating with multiple programming languages within a single runtime environment. It provides comprehensive polyglot execution capabilities, host-guest interoperability, and fine-grained security controls for multi-language applications.
org.graalvm.polyglot:graalvm-sdk:23.1.5import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.SandboxPolicy;// Create context for multiple languages
try (Context context = Context.create("js", "python")) {
// Evaluate JavaScript code
Value result = context.eval("js", "6 * 7");
System.out.println(result.asInt()); // 42
// Host-guest interoperability
Value bindings = context.getBindings("js");
bindings.putMember("greeting", "Hello from Java!");
context.eval("js", "console.log(greeting + ' Welcome to polyglot programming!')");
}The GraalVM Polyglot API is organized around these core components:
Core lifecycle management for polyglot execution environments.
// Context creation and configuration
public static Context create(String... permittedLanguages);
public static Context.Builder newBuilder(String... permittedLanguages);
// Engine creation and sharing
public static Engine create();
public static Engine create(String... permittedLanguages);
public static Engine.Builder newBuilder(String... permittedLanguages);
// Resource management
public void close();
public void close(boolean cancelIfExecuting);Evaluation and parsing of source code across different programming languages.
// Source code evaluation
public Value eval(Source source);
public Value eval(String languageId, CharSequence source);
// Source parsing without execution
public Value parse(Source source);
public Value parse(String languageId, CharSequence source);
// Source creation
public static Source create(String language, CharSequence source);
public static Source.Builder newBuilder(String language, CharSequence characters, String name);Language Execution and Parsing
Language-agnostic value operations and seamless data exchange between host and guest languages.
// Type checking and conversion
public boolean isNull();
public boolean isNumber();
public boolean isString();
public String asString();
public int asInt();
public double asDouble();
public <T> T as(Class<T> targetType);
// Container operations
public boolean hasArrayElements();
public Value getArrayElement(long index);
public boolean hasMembers();
public Value getMember(String identifier);
// Function execution
public boolean canExecute();
public Value execute(Object... arguments);Value Types and Host-Guest Interoperability
Proxy interfaces enabling host objects to mimic guest language objects with custom behavior.
// Base proxy interface
public interface Proxy {}
// Object-like behavior
public interface ProxyObject extends Proxy {
Object getMember(String key);
void putMember(String key, Value value);
Object getMemberKeys();
}
// Array-like behavior
public interface ProxyArray extends ProxyIterable {
Object get(long index);
void set(long index, Value value);
long getSize();
}
// Function-like behavior
public interface ProxyExecutable extends Proxy {
Object execute(Value... arguments);
}Proxy System for Custom Objects
Comprehensive security policies for controlling host access, polyglot evaluation, and sandboxing.
// Host access policies
public static final HostAccess EXPLICIT; // @Export annotation required
public static final HostAccess ALL; // Full unrestricted access
public static final HostAccess CONSTRAINED; // Limited access for sandboxing
public static final HostAccess UNTRUSTED; // Strict restrictions
// Polyglot access control
public static final PolyglotAccess ALL; // Full cross-language access
public static final PolyglotAccess NONE; // No cross-language access
// Sandbox security levels
public enum SandboxPolicy {
TRUSTED, // No restrictions
CONSTRAINED, // Basic restrictions
ISOLATED, // Enhanced isolation
UNTRUSTED // Maximum restrictions
}Virtual file system abstraction and I/O access control for polyglot contexts.
// I/O access policies
public static final IOAccess ALL; // Full I/O access
public static final IOAccess NONE; // No I/O access
// File system operations
public interface FileSystem {
Path parsePath(String path);
void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions);
SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs);
DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter);
}
// Byte sequence operations
public interface ByteSequence {
int length();
byte byteAt(int index);
byte[] toByteArray();
}I/O and File System Virtualization
Performance monitoring, execution tracking, and resource consumption limits.
// Execution monitoring
public final class ExecutionListener implements AutoCloseable {
public void onEnter(Consumer<ExecutionEvent> listener);
public void onReturn(Consumer<ExecutionEvent> listener);
public void attach(Engine engine);
public void attach(Context context);
}
// Resource limits
public final class ResourceLimits {
public static ResourceLimits.Builder newBuilder();
}
// Exception handling
public final class PolyglotException extends RuntimeException {
public boolean isHostException();
public boolean isGuestException();
public Value getGuestObject();
public SourceSection getSourceLocation();
}Execution Monitoring and Resource Management
// Language metadata
public final class Language {
public String getId();
public String getName();
public Set<String> getMimeTypes();
public boolean isInteractive();
}
// Instrument metadata
public final class Instrument {
public String getId();
public String getName();
public String getVersion();
public <T> T lookup(Class<T> type);
}
// Source location information
public final class SourceSection {
public Source getSource();
public int getStartLine();
public int getEndLine();
public int getStartColumn();
public int getEndColumn();
public CharSequence getCharacters();
}
// Type-safe generic conversion
public abstract class TypeLiteral<T> {
public final Type getType();
public final Class<T> getRawType();
}