io.github.ih0r-d:polyglot-build-tools

Build tooling modules for the Polyglot Adapter


License
Apache-2.0

Documentation

polyglot-adapter

Build Java GraalVM Maven Central codecov License


πŸš€ Overview

polyglot-adapter is a lightweight Java SDK providing a unified executor-based API for executing and embedding multi-language code (Python, JavaScript) via GraalVM Polyglot. It simplifies context creation, host access management, and interlanguage communication while preserving full control over GraalVM configuration.

βœ… Focused on developer experience β€” predictable, fast, fully composable.


✨ Key Features

  • Unified BaseExecutor API with native Value interop (Value.as(...))
  • Automatic host-to-guest binding via Java interfaces (bind())
  • Composable Context.Builder API through .apply(...)
  • Extensible HostAccess with .extendHostAccess(...)
  • Built-in type mappings (Value β†’ Path, user-extendable)
  • Virtual File System (VFS) integration for GraalPy
  • Fully dependency-free (no frameworks)
  • 100% compatible with GraalVM 25.x+

🧩 Architecture

polyglot-adapter/
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ context/
β”‚   β”‚   β”œβ”€β”€ Language.java
β”‚   β”‚   β”œβ”€β”€ PolyglotContextFactory.java
β”‚   β”‚   └── ResourcesProvider.java
β”‚   └── executors/
β”‚       β”œβ”€β”€ BaseExecutor.java
β”‚       β”œβ”€β”€ PyExecutor.java
β”‚       └── JsExecutor.java
β”œβ”€β”€ exceptions/
β”‚   └── EvaluationException.java
└── utils/
    β”œβ”€β”€ CommonUtils.java
    └── StringCaseConverter.java

βš™οΈ Requirements

  • JDK 25+
  • Maven 3.9+
  • GraalVM 25.x+

πŸ“¦ Installation

<dependency>
  <groupId>io.github.ih0r-d</groupId>
  <artifactId>polyglot-adapter</artifactId>
  <version>0.1.0</version>
</dependency>

🧩 Optional Language Runtimes (Maven)

Add only the runtimes you actually use. Marked as optional to avoid transitive pulls.

🐍 GraalPy

<dependency>
  <groupId>org.graalvm.python</groupId>
  <artifactId>python-embedding</artifactId>
  <version>25.0.1</version>
  <optional>true</optional>
</dependency>
<dependency>
  <groupId>org.graalvm.python</groupId>
  <artifactId>python-launcher</artifactId>
  <version>25.0.1</version>
  <optional>true</optional>
</dependency>

πŸ•Έ GraalJS

<dependency>
  <groupId>org.graalvm.js</groupId>
  <artifactId>js</artifactId>
  <version>25.0.1</version>
  <type>pom</type>
  <optional>true</optional>
</dependency>

🧠 Usage Example (Python)

try (var executor = PyExecutor.createDefault()) {
    MyApi api = executor.bind(MyApi.class);
    System.out.println(api.add(3, 5)); // 8
}

Python side:

class MyApi:
    def add(self, a, b): return a + b

polyglot.export_value("MyApi", MyApi)

βš™οΈ Configuration

Defaults

.allowAllAccess(true)
.allowExperimentalOptions(true)

Full access for interop, experimental options for latest GraalPy / GraalJS engines.
These defaults follow Oracle’s embedding best practices for SDKs (not sandboxes).

Option Purpose
allowAllAccess(true) Enables complete Java ↔ guest interoperability (IO, threads, polyglot bridge).
allowExperimentalOptions(true) Activates all evolving GraalVM engine flags.

If sandboxing is needed:

.apply(b -> b.allowAllAccess(false))
.apply(b -> b.allowIO(false))
.hostAccess(HostAccess.NONE);

🧱 Default Type Mappings (LOW precedence)

The SDK adds safe default mappings for convenience:

builder.targetTypeMapping(
    Value.class, Path.class,
    Value::isString, v -> Path.of(v.asString()),
    HostAccess.TargetMappingPrecedence.LOW
);

Users can extend or override mappings freely via:

.extendHostAccess(b -> b.targetTypeMapping(
    Value.class, Instant.class,
    Value::isString, v -> Instant.parse(v.asString())
));

🧰 Builder Quick Reference

Method Description
apply(Consumer<Context.Builder>) Direct low-level context configuration.
extendHostAccess(Consumer<HostAccess.Builder>) Extend or override SDK default mappings.
withSafePythonDefaults() Disable GraalPy C API, hide warnings, redirect logs.
withNodeSupport() Enable Node.js compatibility for GraalJS.
option(String, String) Add single engine option.
options(Map<String,String>) Add multiple engine options.

πŸ§ͺ Testing

Run all tests (JUnit 5):

mvn clean test

Includes:

  • Context creation tests (Python / JS)
  • Executor binding and async evaluation tests

πŸ§‘β€πŸ’» Development

Command Action
mvn clean verify Build & run tests
mvn deploy -P release Publish to Maven Central
task bump TYPE=minor Version bump
task release VERSION=X.Y.Z Tag & release version

πŸ“œ License

Licensed under the Apache License 2.0.
See LICENSE for details.