jni-bridge: add unsafe jni_fn! arms and direct/destroy companions
- Commit
fa131d6a37134b46f7e5ae4427acfaefb2c5b690- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 20:07 UTC (4 days ago)
- Parents
850b620
Phase 5 follow-up for library/ml, whose JNI surface the jni_fn! rollout skipped: half its methods are pub unsafe extern and half ignore their EnvUnowned entirely, so neither fit the existing macro. Three additive changes, no existing expansion changes: - jni_fn! gains unsafe arms: writing unsafe before the name generates pub unsafe extern with the same with_env / LogErrorAndDefault scaffold, preserving the hand-written function item type exactly. - jni_direct! (new) generates the direct form for methods that never touch their environment (handle getters, fd-based constructors): same #[no_mangle] pub (unsafe) extern shape, same ignored _env parameter, caller body verbatim. Routing these through jni_fn! would have added env adaptation and error resolution they never had, so exception behaviour is unchanged by construction. - jni_destroy! (new) also generates the guard-and-drop body every destroy* method repeats (return on 0 handle, else drop the boxed handle); the per-handle SAFETY comment stays at the call site. Verified: cargo fmt --check -p jni_bridge clean; first user is library/ml (next commit): cargo test -p jni_bridge unchanged (it has no tests), modelrunner suite green with parity checks detailed there.
Diff
library/jni-bridge/src/main/rust/src/lib.rs
diff --git a/library/jni-bridge/src/main/rust/src/lib.rs b/library/jni-bridge/src/main/rust/src/lib.rsindex ff4cb81758..001ad14b1c 100644--- a/library/jni-bridge/src/main/rust/src/lib.rs+++ b/library/jni-bridge/src/main/rust/src/lib.rs@@ -29,6 +29,14 @@ //! could not bind caller-written uses, by `macro_rules` hygiene), and the //! body must evaluate to `jni::errors::Result<Ret>` (or `Result<()>` for //! the void form), exactly like the closure it replaces.+//!+//! Methods whose hand-written form is `pub unsafe extern` keep that+//! qualifier by writing `unsafe` before the name. Methods that ignore+//! their environment entirely (handle getters, fd-based constructors)+//! use the companion [`jni_direct!`], which generates the direct form+//! without adding the `with_env`/`LogErrorAndDefault` layer those methods+//! never had; pure handle destructors use [`jni_destroy!`], which also+//! generates their repeated guard-and-drop body. pub use jni; @@ -76,4 +84,110 @@ macro_rules! jni_fn { .resolve::<$crate::jni::errors::LogErrorAndDefault>() } };+ // Same scaffold for a method whose hand-written form is `pub unsafe extern`:+ // the qualifier is preserved verbatim, so the generated function item has+ // exactly the type the hand-written one had. The body runs in the same+ // `with_env` closure either way, so exception behaviour is unchanged.+ ($(#[$attr:meta])* unsafe $name:ident -> $ret:ty; ($($arg:ident: $arg_ty:ty),* $(,)?) |$env:ident| $body:block) => {+ $(#[$attr])*+ #[no_mangle]+ pub unsafe extern "system" fn $name<'l>(+ mut env: $crate::jni::EnvUnowned<'l>,+ $($arg: $arg_ty),*+ ) -> $ret {+ env.with_env(|$env| -> $crate::jni::errors::Result<$ret> { $body })+ .resolve::<$crate::jni::errors::LogErrorAndDefault>()+ }+ };+ ($(#[$attr:meta])* unsafe $name:ident; ($($arg:ident: $arg_ty:ty),* $(,)?) |$env:ident| $body:block) => {+ $(#[$attr])*+ #[no_mangle]+ pub unsafe extern "system" fn $name<'l>(+ mut env: $crate::jni::EnvUnowned<'l>,+ $($arg: $arg_ty),*+ ) {+ env.with_env(|$env| -> $crate::jni::errors::Result<()> { $body })+ .resolve::<$crate::jni::errors::LogErrorAndDefault>()+ }+ };+}++/// Generates one JNI native method that never touches its environment.+///+/// A second family of hand-written surfaces takes `_env: EnvUnowned` only to+/// ignore it: handle destructors, capacity/position getters, and fd-based+/// constructors that log and return `0`/`-1` on failure. Routing those+/// through [`jni_fn!`] would *add* behaviour — adapting the env and resolving+/// with `LogErrorAndDefault` clears/logs around a body that today runs bare —+/// so this macro generates the direct form instead: the same+/// `#[no_mangle] pub (unsafe) extern "system"` function, the same ignored+/// `_env` parameter, and the caller's body verbatim. No exception can be+/// thrown, cleared, or logged by the expansion that the hand-written form+/// did not throw, clear, or log, and the exported symbol is unchanged.+///+/// Write `unsafe` before the name when the hand-written form is+/// `pub unsafe extern`, as with [`jni_fn!`].+#[macro_export]+macro_rules! jni_direct {+ ($(#[$attr:meta])* $name:ident -> $ret:ty; ($($arg:ident: $arg_ty:ty),* $(,)?) $body:block) => {+ $(#[$attr])*+ #[no_mangle]+ pub extern "system" fn $name<'l>(+ _env: $crate::jni::EnvUnowned<'l>,+ $($arg: $arg_ty),*+ ) -> $ret $body+ };+ ($(#[$attr:meta])* $name:ident; ($($arg:ident: $arg_ty:ty),* $(,)?) $body:block) => {+ $(#[$attr])*+ #[no_mangle]+ pub extern "system" fn $name<'l>(+ _env: $crate::jni::EnvUnowned<'l>,+ $($arg: $arg_ty),*+ ) $body+ };+ ($(#[$attr:meta])* unsafe $name:ident -> $ret:ty; ($($arg:ident: $arg_ty:ty),* $(,)?) $body:block) => {+ $(#[$attr])*+ #[no_mangle]+ pub unsafe extern "system" fn $name<'l>(+ _env: $crate::jni::EnvUnowned<'l>,+ $($arg: $arg_ty),*+ ) -> $ret $body+ };+ ($(#[$attr:meta])* unsafe $name:ident; ($($arg:ident: $arg_ty:ty),* $(,)?) $body:block) => {+ $(#[$attr])*+ #[no_mangle]+ pub unsafe extern "system" fn $name<'l>(+ _env: $crate::jni::EnvUnowned<'l>,+ $($arg: $arg_ty),*+ ) $body+ };+}++/// Generates one handle destructor: the guard-and-drop shape every+/// `destroy*` native method repeats.+///+/// The expansion is exactly the hand-written form — ignore the env, return+/// on a `0` handle, otherwise reconstruct and drop the boxed handle — so,+/// as with [`jni_direct!`], no exception behaviour changes: the method+/// never touched its environment and still does not. The caller's+/// per-handle `// SAFETY:` comment stays at the call site, above the+/// invocation, where it names the create function the handle came from.+///+/// Syntax: `jni_destroy! { unsafe Java_…_destroyX; (_class: JClass<'l>, handle: jlong): XHandle }`.+#[macro_export]+macro_rules! jni_destroy {+ ($(#[$attr:meta])* unsafe $name:ident; ($class:ident: $class_ty:ty, $handle:ident: $handle_ty:ty): $ty:ty) => {+ $(#[$attr])*+ #[no_mangle]+ pub unsafe extern "system" fn $name<'l>(+ _env: $crate::jni::EnvUnowned<'l>,+ $class: $class_ty,+ $handle: $handle_ty,+ ) {+ if $handle == 0 {+ return;+ }+ drop(unsafe { Box::from_raw($handle as *mut $ty) });+ }+ }; }