11//! `KotlinExt` impl for [`JniExt`].
22//!
3- //! Today's pipeline emits two kinds of Kotlin output:
4- //! 1. One aggregated `JNINative.kt` (interface + data classes + external
5- //! funs). This is currently produced by the existing
6- //! [`crate::kotlin::KotlinInterfaceGenerator`] called separately from
7- //! `build.rs`.
8- //! 2. One `JNI<Stem>Callback.kt` per `impl Fn(args) + Send + Sync + 'static`
9- //! type encountered. These get emitted here via `JniExt::write_kotlin`.
3+ //! [`JniExt::write_kotlin`] is the single entry point for every Kotlin
4+ //! file the JNI back-end emits. Given one `kotlin_root` it writes:
5+ //! * `NativeHandle.kt` (package `io.zenoh.jni`).
6+ //! * One typed-handle class per `kotlin_class` entry without
7+ //! `.suppress_kotlin_code()`.
8+ //! * `JNIWrappers.kt` — top-level safe wrappers for non-promoted fns.
9+ //! * One `JNI<Stem>Callback.kt` per `impl Fn(args) + Send + Sync +
10+ //! 'static` type (package = `kotlin_callback_package`). Callback
11+ //! types overridden via [`JniExt::callback_input`] /
12+ //! [`JniExt::callback_kotlin_name`] are skipped — the override
13+ //! points at a hand-written interface.
1014//!
11- //! The split is deliberate: the per-callback files are the new artifact
12- //! introduced by the rewrite; the aggregated interface remains the
13- //! responsibility of the existing generator and is not touched by JniExt.
15+ //! All emitters route through [`KotlinFile::write`], which translates
16+ //! `package` into a sub-path under `kotlin_root`.
1417
1518use std:: collections:: { BTreeSet , HashSet } ;
1619use std:: path:: { Path , PathBuf } ;
@@ -70,7 +73,7 @@ impl JniExt {
7073 kotlin_root : & Path ,
7174 ) -> Result < Vec < PathBuf > , WriteKotlinError > {
7275 let mut written = Vec :: new ( ) ;
73- written. extend ( self . emit_callback_files ( registry) ?) ;
76+ written. extend ( self . emit_callback_files ( registry, kotlin_root ) ?) ;
7477 written. push ( self . write_native_handle ( kotlin_root) ?) ;
7578
7679 // Build the borrowed `TypedHandle<'_>` view from internal config.
@@ -111,14 +114,15 @@ impl JniExt {
111114 /// registry). Skips writes for `impl Fn(...)` keys whose Kotlin
112115 /// FQN was overridden via [`Self::callback_input`] — the override
113116 /// already points at a hand-maintained callback interface, so the
114- /// auto-stub would be dead code.
117+ /// auto-stub would be dead code. Each emitted file is placed
118+ /// under `kotlin_root/<kotlin_callback_package as path>/`.
115119 pub ( crate ) fn emit_callback_files (
116120 & self ,
117121 registry : & Registry < KotlinMeta > ,
122+ kotlin_root : & Path ,
118123 ) -> Result < Vec < PathBuf > , WriteKotlinError > {
119124 let mut seen: HashSet < TypeKey > = HashSet :: new ( ) ;
120125 let mut written = Vec :: new ( ) ;
121- let target_dir = self . kotlin_callback_dir . clone ( ) ;
122126 for buckets in [ & registry. input_types , & registry. output_types ] {
123127 for bucket in buckets. iter ( ) {
124128 for ( key, slot) in bucket {
@@ -142,10 +146,7 @@ impl JniExt {
142146 continue ;
143147 }
144148 let file = build_callback_kotlin_file ( self , & args, registry) ;
145- std:: fs:: create_dir_all ( & target_dir) ?;
146- let path = target_dir. join ( format ! ( "{}.kt" , file. class_name) ) ;
147- std:: fs:: write ( & path, & file. contents ) ?;
148- written. push ( path) ;
149+ written. push ( file. write ( kotlin_root) ?) ;
149150 }
150151 }
151152 }
0 commit comments