Issue #1337: nikaia bind java: a Java 22 binding over the FFM API from the ledger (ADR-284 D34)

Open and answer this issue on GitHub.

blocked, solver · opened 2026-11-07 · updated 2026-11-17 · 5 comments

ADR-284 D34 (decided November 7, 2026): nikaia bind java writes Java source over java.lang.foreign (FFM, JEP 454), Java 22 or later. Part of #599. Builds on the solver as a C library (#1336), but is tested first on examples/c-library.

Goal

A Java 22 program calls a Nikaia library through a generated binding. There is no JNI, no C shim and no hand-written glue. The first user is the solver: a JVM caller hands a whole SMT-LIB query to the native solver in one call.

To build

  1. nikaia bind java: a generator written in Nikaia (crates/nikaia-std/src/tools/bind_java.nika or tools/bind_java.nika, behind a bind subcommand). It reads the ledger of a c-library build, as the header generator does. nikaia bind does not exist yet, so this item also makes the subcommand. The subcommand’s dispatch is the only Rust allowed, as for the other tool subcommands.
    • It writes <pkg>/<Pkg>.java (the class with a MethodHandle per entry point, made once through Linker.nativeLinker().downcallHandle), the exception classes, one class per handle, and one record per extern struct.
    • It follows ADR-284 D34’s table. What a host cannot express is decided in the binding’s table (D29), not per program.
  2. Part III 15.1: the nikaia bind java line beside python, node and js (docs/specification/30-nikaia-tooling.md, lines 37 and 714-721). The supervisor raises the version when it pushes (the builder does not touch the version lines).
  3. Test on examples/c-library (wordtally): examples/c-library/java/Use.java, mirroring use.c. It builds the library, runs nikaia bind java, then compiles and runs the Java file with javac/java 22+. Its output equals use.c’s. Where CI lacks a JDK 22+, the CI step installs one through the package manager or actions/setup-java. The test runs in CI; this machine has only Java 21 (FFM there is a preview with other names), so locally the test is skipped with a message, never faked.
  4. The solver from Java: once the solver library exists, packages/solver/java/Check.java reads a .smt2 file, calls check, and prints the verdict and why. Its verdicts equal the C caller’s on tests/solver/pre.
  5. The figure, recorded here: the cost of one crossing from Java (a no-op entry point called N times, warmed). It is counted, not timed (docs/measuring.md). If that file has no rule for a JVM caller, add one: callgrind over the warmed loop of N calls minus the loop of 0 calls, divided by N, with the JIT flags named. ADR-284 D34 expects tens of nanoseconds’ worth of instructions. A figure far above that is a finding.

Done when

nikaia bind java writes a binding that compiles with javac --release 22 without warnings. Use.java prints what use.c prints, in CI. The crossing figure is recorded here, and Part III names nikaia bind java.

Files and limits

Ruled out (ADR-284 D34)


Generated by Claude Code

Comments

keywan-ghadami, 2026-11-17

Brief queued: issue-1337


agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —

Issue #1337

Your work order is issue #1337, inlined below with its comments. There is no brief written around it: plan your own steps from its To build list, build them in order, commit after each finished step, and end when its Done when holds (or at the limits of builder.md, reporting what is left). The facts in your prompt are Core of docs/builder-facts.md; more on a topic: nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.

Measure the issue against docs/issues.md while you work: what it lacked, what was wrong in it. That answer goes into the report’s Issue: field. A finding, a changed plan or a finished item also goes into the issue as a comment (gh api repos/Nikaia-Foundation/Nikaia/issues/1337/comments -F body=@file, ending with the Claude Code footer), rule I13.

Rules (CLAUDE.md; shared checkout)

Report (at most 15 lines, to exactly /home/user/Nikaia/docs/briefs/solver/issue-1337.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Comment (#N <text>, what to say on another issue; the driver posts it, one line each), Friction, Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-11-17

Run: issue-1337, Result: partial

Tokens: input 282, cache read 10317647, cache write 269040, output 97720 Cost: 6.57 USD, time 36 min 11 s, turns 141

Report

Result: partial Files: crates/nikaia-std/src/compiler/cexport_java.nika crates/nikaia-std/src/compiler/cexport.nika crates/nikaia-std/src/compiler/cli_commands.nika crates/nikaia-std/src/compiler/project_drive.nika crates/nikaia-std/src/compiler/lowered.rs crates/nikaia-std/src/compiler/lowered/cexport.rs crates/nikaia-std/src/compiler/lowered/cexport_java.rs crates/nikaia-std/src/compiler/lowered/cli_commands.rs crates/nikaia-std/src/compiler/lowered/project_drive.rs crates/nikaia/src/cexport.rs crates/nikaia/src/project.rs crates/nikaia/tests/it/c_library.rs examples/c-library/java/Use.java scripts/java_binding.nika .github/workflows/suite.yml docs/specification/30-nikaia-tooling.md docs/measuring.md tests/compiler/tests/bind_java_writes_a_class_per_handle_and_a_record_per_struct.nika tests/compiler/tests/bind_java_writes_a_class_per_handle_and_a_record_per_struct.stdout Commit: nikaia bind java: a Java 22 binding over java.lang.foreign, written from the ledger Body: cexport_java.nika (new, Nikaia) writes target/nikaia/c-library/java//.java from the ledger: a static method per function, an AutoCloseable class per handle, a record per extern struct, a sealed interface of records per extern enum, a Java enum per plain one, a functional interface per callback, Optional for T?, an exception per variant. `bind java` dispatches to it (cli_commands, project_drive). Rust added is plumbing only: Exported.java and Lowered.java carry the text (cexport.rs, project.rs). The c_library.rs refusal assertion was changed only to keep it passing. scripts/java_binding.nika checks that examples/c-library/java/Use.java prints what use.c prints with javac --release 22 -Xlint:all -Werror, and skips with a message below JDK 22. CI runs it on shard ledgers with temurin 22. Part III 15.1 and the line on bind names `nikaia bind java`. measuring.md gets the JVM crossing rule and its first figure. Part: #1337 Tests: bind_java_writes_a_class_per_handle_and_a_record_per_struct passes under --both-settings and fails on the unfixed compiler (bind java refused); it c_library:: 6 passed; clippy and fmt clean; pre-push green; java_binding.nika says skipped on this host's javac 21. By hand on JDK 21 --enable-preview, Use.java output is identical to use.c, and the calc LIBRARY of c_library.rs ran right in every case (scalars, text, bytes, enums, handles, records with arrays, callbacks with a throw, exceptions with codes, MemorySegment shift). Pushed: local 12af7060968d905725729cceb663c55a7ca8166d Gate: green Deviation: Rust beyond the dispatch: an Exported.java and Lowered.java field in cexport.rs and project.rs, to carry the generated text the way python/node do. `_async` entries (CompletableFuture) and streams (Iterator) are not written yet; Part III 15.1 says so. The crossing figure was counted on JDK 21 --enable-preview, since this host has no JDK 22. Found: a string literal in a returned tuple is refused when another arm holds an owned String: `fn f(x: String) -> (String, i64) { return match 1 { 0 => ("a", 1), else => (f"b", 2) } }` gives NK1104 "returning `(ref String, ?)`", and with literals in both arms it compiles. It should compile, as `return "a"` does for a String result. Suspected in the checker's tuple unification of match arms (check_*); a check of `("a", 1)` alone in a `-> (String, i64)` would settle it. Found: comparing an optional enum field after a null check makes rustc refuse: `enum Hold { Shared, Alone }` / `struct E { r: Hold? }` / `fn f(e: ref E) -> bool { return e.r != null && e.r == Hold::Alone }` gives rustc "can't compare `&Hold` with `Hold`" (exit 101), and with the parameter form `r: Hold?` it compiles. It should compile (checker accepted it). Suspected in emit: the narrowed field lowers to `as_ref().unwrap() == X` with no deref. Found: indexing cli::args() passes the checker but rustc refuses it: `use std::cli` / `fn main() throws { let args = cli::args(); let mut at = 0; while at < args.len() { if args[at] == "--exe" { println("exe") }; at += 1 } }` gives NK1343 "mismatched types" at args.len() and "`nikaia_std::cli::Args: Get<_>` is not satisfied" at args[at]. It should run or be refused by the checker. Traced: cli.rs `pub fn args() -> Args` returns an iterator while the checker types it as a list. `for arg in cli::args()` works. Needs: #1336 Changelog: nikaia bind java — `nikaia bind java` writes a Java 22 binding over java.lang.foreign (FFM, JEP 454) beside the C library: classes, records, sealed interfaces, Optional and exceptions, with no JNI and no glue code. Issue: fits I1-I13; item 4 (packages/solver/java/Check.java) waits for Solver as a C library (#1336), and the JDK 22 run and the Java 22 crossing figure are CI's. Comment: #1337 Built: bind java, Use.java, the CI step (temurin 22, shard ledgers), Part III 15.1 and the measuring rule. Crossing on OpenJDK 21.0.12 --enable-preview, flags -Xbatch -XX:-TieredCompilation -XX:+UseSerialGC -XX:CICompilerCount=1, 20 000-call warm-up, callgrind --smc-check=all: (882,972,451 - 822,513,063) / 200,000 = about 302 Ir per call to a no-op entry. Left: Check.java (needs #1336), the first CI run on JDK 22, the figure on JDK 22, `_async` and streams. Friction: the host has only JDK 21, so the Java 22 check could only be done with --enable-preview by hand; `nikaia test` takes a filter only as --filter, and `nikaia run` refuses a test file. Owner: none Disk freed: target/nikaia-project-tests removed (under 3 GB free), / 38G used and 1.4G free after it; no new cache entry over 100 MB Attribution: Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UVxJDwk9ubq9teP7QCErff Reviewed: 4e84bace022dbfeec502485aaddd80092b847e860b03d74fc5fe494c8614077d

Debrief

  1. Most cost: checking the generated Java took the most turns. Without JDK 22 I had to compile it as a Java 21 preview, patch it by hand and rebuild the compiler for each mistake: default methods in interfaces, members that should have been public, ?: typed as Object inside invokeExact, and a cast that -Xlint calls redundant. Next came two compiler defects (NK1104 on a string literal in a tuple, and rustc refusing a narrowed optional field), each needing a probe. I worked around both in the generator instead of fixing them, which the rules forbid.
  2. Looked up: how the Python and Node bindings carry their text (Exported/Lowered in cexport.rs and project.rs, the 5-tuple digest), where bind is refused (cli_commands_bind_refusal), that nikaia test filters with --filter, that text::parse_i64 is a free function, and that cli::args() cannot be indexed.
  3. Checks: the cycle, the new test (blessed, both settings, failing on the old binary), the c_library:: tests, clippy, fmt, pre-push, hand runs of wordtally and calc on Java 21, and callgrind. Wasted: the first java_binding.nika runs, which failed on errors that check would have caught first, and one early calc run on hand-patched output.
  4. One change: a builder.md line saying “a feature whose target toolchain is missing locally: check it under the nearest available version (Java 21 --enable-preview) and say so in Tests:”. It would have settled the approach in one turn.
  5. Issue gaps:
    • broke I3 (or whichever rule asks for Locations): it did not name the Python/Node plumbing through the Rust files, which bind java must copy.
    • rules-gap: it did not say the dispatch alone cannot carry the text without Rust fields, so “no new Rust except the dispatch” was not reachable as written.
    • rules-gap: it did not note that the host lacks JDK 22.
  6. Traps for the next builder:
    • Fact: Core | nikaia test takes no positional filter, only --filter TEXT, and nikaia run refuses a file under tests/.
    • Fact: syntax gaps | cli::args() is an iterator: index it and rustc refuses (NK1343); read it with for arg in cli::args().
    • Fact: syntax gaps | there is no String.parse_i64(); use text::parse_i64(s) ?? default.
    • Fact: syntax gaps | a string literal in a returned tuple next to an owned-String arm draws NK1104; open defect, not to be worked around.
    • Fact: Core | a JVM writes "Picked up JAVA_TOOL_OPTIONS" to stderr on this host; filter it before any empty-stderr check.

Generated by Claude Code

keywan-ghadami, 2026-11-17

From the report of the run of issue-1337 (#1337):

Built: bind java, Use.java, the CI step (temurin 22, shard ledgers), Part III 15.1 and the measuring rule. Crossing on OpenJDK 21.0.12 –enable-preview, flags -Xbatch -XX:-TieredCompilation -XX:+UseSerialGC -XX:CICompilerCount=1, 20 000-call warm-up, callgrind –smc-check=all: (882,972,451 - 822,513,063) / 200,000 = about 302 Ir per call to a no-op entry. Left: Check.java (needs #1336), the first CI run on JDK 22, the figure on JDK 22, _async and streams.


Generated by Claude Code

keywan-ghadami, 2026-11-17

Driver: not queued, this issue waits for #1336, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

keywan-ghadami, 2026-11-17

Driver: not queued, this issue waits for #1336, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

Back to Open Issues