Skip to content

Commit e4955ff

Browse files
authored
Merge pull request #459 from thisisthepy/feat/tp-458-aot-kotlin
TypedPython AOT tier K, PR3b: generated Kotlin wrappers and a desktop JVM fixture with no CPython
2 parents 34b9e79 + fcb6bf7 commit e4955ff

13 files changed

Lines changed: 1786 additions & 7 deletions

File tree

‎.github/workflows/typedpython.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,27 @@ jobs:
176176
print("skipped: " + ", ".join(skipped) if skipped else "no TypedPythonKotlinCallTest results")
177177
sys.exit(1)
178178
PY
179+
180+
181+
# #458: generated Kotlin wrappers of a TypedPython AOT library, called from the desktop JVM with no CPython.
182+
# This fixture does not need macOS (its test JVM loads no libpython); it runs here only because this job
183+
# already has JDK 21 and the host Python. A separate Gradle invocation from the app fixture's (AGENTS.md 8).
184+
- name: AOT library fixture tests
185+
run: |
186+
rm -rf python-multiplatform-ksp/fixtures/aot/build/test-results/desktopTest
187+
./gradlew :python-multiplatform-ksp:fixtures:aot:desktopTest --rerun --console=plain \
188+
-PtypedpythonPython="$PWD/python-multiplatform-ksp/.venv/bin/python"
189+
190+
- name: The AOT library tests ran (none skipped)
191+
run: |
192+
python3 - <<'PY'
193+
import glob, sys, xml.etree.ElementTree as ET
194+
cases = [c for p in glob.glob("python-multiplatform-ksp/fixtures/aot/build/test-results/desktopTest/*.xml")
195+
for c in ET.parse(p).getroot().iter("testcase")
196+
if c.get("classname") == "fixture.aot.AotLibraryCallTest"]
197+
skipped = [c.get("name") for c in cases if c.find("skipped") is not None]
198+
print(f"AotLibraryCallTest: {len(cases)} test(s), {len(skipped)} skipped")
199+
if not cases or skipped:
200+
print("skipped: " + ", ".join(skipped) if skipped else "no AotLibraryCallTest results")
201+
sys.exit(1)
202+
PY

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ generators emit. Run each fixture module as a **separate** Gradle invocation (ru
352352
| `:python-multiplatform-ksp:fixtures:app:desktopTest` | Upcall and proxy runtime |
353353
| `:python-multiplatform-compose:fixtures:compose:desktopTest` | Compose bindings and render proofs |
354354
| `:python-multiplatform-gradle-plugin-fixtures:artifact:desktopTest` | Artifact walker and `.pyi` stubs |
355+
| `:python-multiplatform-ksp:fixtures:aot:desktopTest` | Generated Kotlin wrappers of a TypedPython AOT library on the desktop JVM, with no CPython (bit-exact results, exceptions, lists, threads); needs a host Python, a skip is a failure in CI |
355356

356357
Where each lives: `python-multiplatform-ksp/fixtures/{library,app,android}` (KSP processor),
357358
`python-multiplatform-gradle-plugin/fixtures/{artifact,artifact-valueclass,klib-artifact}` (artifact

‎docs/design/typedpython-aot.md‎

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,52 @@ earlier proposal of one library lock around each exported call.
335335

336336
### 8.3 Kotlin calling Python (the downcall)
337337

338-
The Gradle plugin, which already generates Kotlin source for bindings, generates one typed
339-
Kotlin wrapper per exported function from the description of 8.1, and per platform the binding the
340-
platform rules require: on desktop JVM a downcall handle invoked with `invokeExact`, pointers as
341-
`JAVA_LONG`; on Android registration through `RegisterNatives` with only primitive types crossing; on
342-
Kotlin/Native (iOS, androidNative) a cinterop definition over the generated header; wasm when the
343-
target is taken up. The app then calls `module.function(args)` as ordinary typed Kotlin, with no
344-
`PyObject`, no interpreter start-up and no `PYTHONHOME`.
338+
Provenance. That the library output exists is owner decision D5 (2026-11-16). That the desktop JVM
339+
comes first and that the generated Kotlin is self-contained (it reaches `java.lang.foreign` by
340+
reflection once at link time, uses pointers as `JAVA_LONG` and calls through `invokeExact`, and does
341+
not depend on the CPython-embedding library) are leader decisions of 2026-11-16, not owner-confirmed.
342+
Everything below about the shape of the generated API is an agent proposal, not owner-confirmed.
343+
344+
**What exists (#458, desktop JVM).** `GenerateAotLibraryBindingsTask` in the Gradle plugin reads the
345+
`<prefix>.json` of 8.1 (`abi_version` must be 1; another version, or a type outside the vocabulary,
346+
fails the task and names the function) and writes four Kotlin files into one package:
347+
348+
- `AotFfm.kt`, the same text for every library: `TypedPythonAotException(className, message)` and a
349+
small runtime over `java.lang.foreign` (JDK 21 with `--enable-preview`, or JDK 22 and later). It
350+
resolves the API reflectively once, binds each symbol to a downcall handle whose type is made of
351+
`long`, `double`, `byte` and `int` only, and reads and writes native memory through handles bound once.
352+
Nothing outside `java.base`. A call afterwards is `invokeExact` and nothing else.
353+
- `<Object>.kt`: an object with `load(libraryPath)` (idempotent, thread-safe: loads the library,
354+
binds every symbol, calls `<prefix>_init`) and one function per export. Names and parameter names
355+
are the Python names as they are (snake_case kept; a Kotlin hard keyword is backticked). Types:
356+
`Long`, `Double`, `Boolean`, the two list classes, no result type for None, and a generated data
357+
class `<Name>Result(v0, v1, ...)` for a tuple (also for two elements, so there is one rule). A failed
358+
call (status -1) throws `TypedPythonAotException` built from `<prefix>_last_error` on the same thread.
359+
- `F64List.kt`, `I64List.kt`: counted native handles with `size`, `get`/`set` (bounds checked on the
360+
Kotlin side), `toDoubleArray()`/`toLongArray()`, `of(array)`, and `close()` (releases once; use after
361+
close throws `IllegalStateException`). A list an exported function returns is wrapped as an owned
362+
handle; the same Kotlin list passed for two parameters is the same handle, so the parameters alias as
363+
in Python. An unclosed list is not freed by the garbage collector.
364+
365+
Out pointers are per-thread scratch memory: one 128-byte block per thread, allocated on first use from
366+
an automatic arena that the garbage collector frees once the thread is gone. A call costs no
367+
allocation and two threads never share a slot. A function may have at most 16 results.
368+
The helper and the list classes have fixed names, so each library takes its own package.
369+
370+
**How the fixture uses it.** `python-multiplatform-ksp/fixtures/aot` declares the bindings plugin with
371+
`apply false` and registers the task class itself, so no KSP, no `PYTHONHOME` staging and no dependency
372+
on `python-multiplatform` come with it. A front-end-only Python step writes the JSON, the task
373+
generates Kotlin from it into the test source set, and `python -m typedpython aot-lib` links the
374+
library as a dependency of the test task only. The test (`fixture.aot.AotLibraryCallTest`) calls
375+
the `nbody` kernels and a small library of failures and list functions, compares floats bit for bit
376+
with CPython, checks exception classes and messages, runs two threads, and asserts that no class of
377+
the CPython-embedding library is on the classpath and that the process loads no libpython (Linux: the
378+
process's maps; macOS: `vmmap` when it can examine the JVM, otherwise the classpath check only).
379+
380+
**Not done.** Other platforms (Android `RegisterNatives`, Kotlin/Native cinterop, wasm); packaging of
381+
the library and the generated Kotlin into an app (waits for the owner decision of #25 step 3); the
382+
GraalVM native image (#456: the descriptors are not yet declared for native-image registration, so the
383+
generated wrappers are not shown to work there, and tier K stays open until they are, AGENTS.md 13).
345384

346385
### 8.4 Python calling Kotlin (the upcall)
347386

0 commit comments

Comments
 (0)