office: convert JNI surface to shared jni_fn! macro
- Commit
398450dc1dc15ba25e34372fb75caf48d2f4eb27- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:11 UTC (4 days ago)
- Parents
9473c16
Phase 5 pilot for the JNI boilerplate audit (499 Java_ functions in 78 Rust files; only e2ee-openpgp had a jni_fn! macro, private to its pgp_jni.rs). Pilot crate is office_engine, chosen after surveying voxels (already 1-3 line bodies, a macro would inflate it), e2ee-p2p (already factored into run_pair/run_bytes helpers) and rust-mls (repetitive, but communicate-dev is excluded from the structure line count, so the gain would not register). Every one of office's 16 JNI functions repeated the identical EnvUnowned/with_env/ LogErrorAndDefault scaffold with no prior factoring. New shared macro: jni_fn! in a new rlib-only crate, library/jni-bridge (package jni_bridge), following the jni-http precedent - workspace member, no Gradle module, linked into consumers' cdylibs. It depends only on the workspace jni crate and re-exports it (pub use jni) so expansions resolve through $crate::jni and consumers gain no new transitive dependency. Placement in an existing shared crate was rejected: no existing Rust crate is a dependency of all JNI crates. The macro takes the exported name, optional return type, the full typed argument list (receiver first, so JObject instance methods work too) and the body block, with the caller naming the Env binding (|env|) because macro_rules hygiene prevents a macro-defined closure parameter from binding caller-written uses. The JNI surface is frozen and preserved by construction: the generated function carries the caller's identifier verbatim under #[no_mangle]; the body runs in the same with_env closure resolved with the same LogErrorAndDefault policy, so errors and panics map to the default return (null/0/false) exactly as before and no new exception reaches Java; handles, argument types and every null check stay in the bodies, which are verbatim (only de-indented). Converted all 16 office_engine JNI functions in office/src/main/rust/src/lib.rs. The two doc comments on converted functions became plain comments: rustdoc warns that it cannot document a macro invocation. Measured lines (structure_check.py lines): office module 58,985 -> 58,841 (-144); the JNI file itself 389 -> 245 (-37.0%). The shared macro crate adds 74 counted lines under library/, so the workspace net is -70 (1,658,179 -> 1,658,109). The macro pays for itself after ~8 converted functions at office's ~9 lines saved per function; already-factored crates (e2ee-p2p, voxels) would save only ~2-4 lines per function, so a workspace rollout should expect materially less than 9 per function on average. Cargo.lock also drops tile_build's stale png/ruzstd entries: its manifest no longer declares them, and the manifest change forced a re-resolve that garbage-collected the stale lock lines. Verified: nm -D on liboffice_engine.so before/after - exported symbol set identical (16/16, all Java_ OfficeNative symbols); cargo test -p office_engine -p jni_bridge: 105 passed; targeted JVM bridge tests :office:testDevUnitTestNative Slice4SheetsTest: 24/24 passed (the run excludes :library:e2ee-p2p:rustFileLength, which fails on the pre-existing 666-line c_bridge_tests.rs, untouched by this change); cargo fmt --check clean for both crates; cargo check --offline clean; structure_check lines pass.
Diff
Cargo.lock
diff --git a/Cargo.lock b/Cargo.lockindex 9157679386..96edb24821 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2947,6 +2947,13 @@ dependencies = [ "syn 2.0.119", ] +[[package]]+name = "jni_bridge"+version = "0.1.0"+dependencies = [+ "jni",+]+ [[package]] name = "jni_http" version = "0.1.0"@@ -3953,6 +3960,7 @@ name = "office_engine" version = "0.1.0" dependencies = [ "jni",+ "jni_bridge", "quick-xml", "serde", "serde_json",
Cargo.toml
diff --git a/Cargo.toml b/Cargo.tomlindex 9e4e4d9609..7bc046271d 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -15,6 +15,7 @@ members = [ "library/e2ee-p2p/src/main/rust", "library/e2ee-openpgp/src/main/rust", "library/jni-http/src/main/rust",+ "library/jni-bridge/src/main/rust", "library/map/src/main/rust", "library/ml/src/main/rust", "library/vision/src/main/rust",
library/jni-bridge/src/main/rust/Cargo.toml
diff --git a/library/jni-bridge/src/main/rust/Cargo.toml b/library/jni-bridge/src/main/rust/Cargo.tomlnew file mode 100644index 0000000000..d70b4dcd7d--- /dev/null+++ b/library/jni-bridge/src/main/rust/Cargo.toml@@ -0,0 +1,14 @@+[package]+name = "jni_bridge"+version = "0.1.0"+edition = "2021"+description = "Shared jni_fn! macro for Rust JNI surfaces — one scaffold for the EnvUnowned/with_env/LogErrorAndDefault pattern every cdylib repeats"+license = "GPL-2.0-only"++[lib]+name = "jni_bridge"+# rlib only: this is linked into other crates' cdylibs, it is not a library of its own.+crate-type = ["rlib"]++[dependencies]+jni.workspace = true
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.rsnew file mode 100644index 0000000000..263214e68e--- /dev/null+++ b/library/jni-bridge/src/main/rust/src/lib.rs@@ -0,0 +1,74 @@+//! Shared scaffold for Rust JNI surfaces.+//!+//! Every cdylib in this repo repeats the same native-method scaffold:+//! `#[no_mangle] pub extern "system" fn Java_…(mut env: EnvUnowned, _class:+//! JClass, …)` wrapping a body in `env.with_env(…)` and resolving it with+//! `LogErrorAndDefault`. [`jni_fn!`] generates exactly that scaffold, so a+//! JNI file states only what differs per method: the exported name, the+//! argument list, the return type, and the body.+//!+//! The JNI surface is frozen, and the macro preserves it by construction:+//!+//! * the generated function carries the caller's identifier verbatim under+//! `#[no_mangle]`, so the exported symbol name is unchanged;+//! * the body runs inside the same `with_env` closure and is resolved with+//! the same `LogErrorAndDefault` policy, so a `jni::errors::Err` or a panic+//! is logged and mapped to the return type's default (`null`, `0`, `false`)+//! exactly as in the hand-written form — no exception reaches Java that did+//! not reach it before;+//! * arguments (handles, strings, byte arrays, …) are passed through with+//! the caller's declared types and names, so handle/pointer conventions+//! and every null check stay in the body, unchanged.+//!+//! The receiver (`_class: JClass<'l>` for the static methods of a Kotlin+//! `object`, or `_this: JObject<'l>` for instance methods) is simply the+//! first declared argument, so both shapes use the same macro.+//!+//! The caller names the `&mut Env` binding lent by `with_env` (written+//! `|env|` above, as in the hand-written closure — a macro-defined name+//! 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.++pub use jni;++/// Generates one JNI native method with the repo-standard scaffold.+///+/// Valued form:+///+/// ```ignore+/// jni_fn! { Java_com_example_Native_answer -> jint; (_class: JClass<'l>, x: jint) |env| {+/// let _ = env;+/// Ok(x + 1)+/// } }+/// ```+///+/// Void form (no `-> Ret`): the body evaluates to `Result<()>` and the+/// generated method returns nothing.+///+/// The lifetime `'l` is the JNI frame lifetime introduced by the generated+/// function; argument types such as `JString<'l>` refer to it, as in the+/// hand-written form.+#[macro_export]+macro_rules! jni_fn {+ ($name:ident -> $ret:ty; ($($arg:ident: $arg_ty:ty),* $(,)?) |$env:ident| $body:block) => {+ #[no_mangle]+ pub 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>()+ }+ };+ ($name:ident; ($($arg:ident: $arg_ty:ty),* $(,)?) |$env:ident| $body:block) => {+ #[no_mangle]+ pub 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>()+ }+ };+}
office/src/main/rust/Cargo.toml
diff --git a/office/src/main/rust/Cargo.toml b/office/src/main/rust/Cargo.tomlindex c7f4510c80..6aed23a38a 100644--- a/office/src/main/rust/Cargo.toml+++ b/office/src/main/rust/Cargo.toml@@ -11,6 +11,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] jni.workspace = true+jni_bridge = { path = "../../../../library/jni-bridge/src/main/rust" } serde.workspace = true serde_json.workspace = true quick-xml.workspace = true
office/src/main/rust/src/lib.rs
diff --git a/office/src/main/rust/src/lib.rs b/office/src/main/rust/src/lib.rsindex 1316b91c14..07be5499fc 100644--- a/office/src/main/rust/src/lib.rs+++ b/office/src/main/rust/src/lib.rs@@ -38,7 +38,8 @@ mod jni_bindings { use super::*; use jni::objects::{JClass, JString}; use jni::sys::{jboolean, jdouble, jint, jlong, jstring};- use jni::{Env, EnvUnowned};+ use jni::Env;+ use jni_bridge::jni_fn; fn read_string(env: &Env, s: &JString) -> Option<String> { s.try_to_string(env).ok()@@ -53,337 +54,192 @@ mod jni_bindings { // --- Document tree CRDT --- - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtNew<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- device: JString<'l>,- ) -> jlong {- env.with_env(|env| -> jni::errors::Result<jlong> {- let dev = match read_string(env, &device) {- Some(v) => v,- None => return Ok(0),- };- let h = next_handle();- crdt_registry().lock().unwrap().insert(h, DocumentTreeCrdt::new(dev));- Ok(h)- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtLoadState<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- json: JString<'l>,- ) {- env.with_env(|env| -> jni::errors::Result<()> {- let json = match read_string(env, &json) {- Some(v) => v,- None => return Ok(()),- };- let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- if let Some(c) = crdt_registry().lock().unwrap().get_mut(&handle) {- c.load_state(&json);- }- }));- Ok(())- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtSerialize<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- crdt_registry().lock().unwrap().get(&handle).map(|c| c.serialize())- }))- .unwrap_or(None);- match out {- Some(s) => Ok(out_string(env, s)),- None => Ok(std::ptr::null_mut()),+ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtNew -> jlong; (_class: JClass<'l>, device: JString<'l>) |env| {+ let dev = match read_string(env, &device) {+ Some(v) => v,+ None => return Ok(0),+ };+ let h = next_handle();+ crdt_registry().lock().unwrap().insert(h, DocumentTreeCrdt::new(dev));+ Ok(h)+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtLoadState; (_class: JClass<'l>, handle: jlong, json: JString<'l>) |env| {+ let json = match read_string(env, &json) {+ Some(v) => v,+ None => return Ok(()),+ };+ let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ if let Some(c) = crdt_registry().lock().unwrap().get_mut(&handle) {+ c.load_state(&json); }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtApply<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- ops_json: JString<'l>,- ) {- env.with_env(|env| -> jni::errors::Result<()> {- let ops_json = match read_string(env, &ops_json) {- Some(v) => v,- None => return Ok(()),- };- let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- let ops: Vec<crdt::Node> = match serde_json::from_str(&ops_json) {- Ok(v) => v,- Err(_) => return,- };- if let Some(c) = crdt_registry().lock().unwrap().get_mut(&handle) {- c.apply(&ops);- }- }));- Ok(())- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtUpdate<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- xml: JString<'l>,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let xml = match read_string(env, &xml) {- Some(v) => v,- None => return Ok(std::ptr::null_mut()),+ }));+ Ok(())+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtSerialize -> jstring; (_class: JClass<'l>, handle: jlong) |env| {+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ crdt_registry().lock().unwrap().get(&handle).map(|c| c.serialize())+ }))+ .unwrap_or(None);+ match out {+ Some(s) => Ok(out_string(env, s)),+ None => Ok(std::ptr::null_mut()),+ }+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtApply; (_class: JClass<'l>, handle: jlong, ops_json: JString<'l>) |env| {+ let ops_json = match read_string(env, &ops_json) {+ Some(v) => v,+ None => return Ok(()),+ };+ let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ let ops: Vec<crdt::Node> = match serde_json::from_str(&ops_json) {+ Ok(v) => v,+ Err(_) => return, };- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- crdt_registry().lock().unwrap().get_mut(&handle).map(|c| {- let ops = c.update(&xml);- serde_json::to_string(&ops).unwrap_or_else(|_| "[]".to_string())- })- }))- .unwrap_or(None);- match out {- Some(s) => Ok(out_string(env, s)),- None => Ok(std::ptr::null_mut()),+ if let Some(c) = crdt_registry().lock().unwrap().get_mut(&handle) {+ c.apply(&ops); }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtRender<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- crdt_registry().lock().unwrap().get(&handle).map(|c| c.render())- }))- .unwrap_or(None);- match out {- Some(s) => Ok(out_string(env, s)),- None => Ok(std::ptr::null_mut()),- }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtToStateNodesJson<- 'l,- >(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- crdt_registry().lock().unwrap().get(&handle).map(|c| c.to_state_nodes_json())- }))- .unwrap_or(None);- match out {- Some(s) => Ok(out_string(env, s)),- None => Ok(std::ptr::null_mut()),- }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ }));+ Ok(())+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtUpdate -> jstring; (_class: JClass<'l>, handle: jlong, xml: JString<'l>) |env| {+ let xml = match read_string(env, &xml) {+ Some(v) => v,+ None => return Ok(std::ptr::null_mut()),+ };+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ crdt_registry().lock().unwrap().get_mut(&handle).map(|c| {+ let ops = c.update(&xml);+ serde_json::to_string(&ops).unwrap_or_else(|_| "[]".to_string())+ })+ }))+ .unwrap_or(None);+ match out {+ Some(s) => Ok(out_string(env, s)),+ None => Ok(std::ptr::null_mut()),+ }+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtRender -> jstring; (_class: JClass<'l>, handle: jlong) |env| {+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ crdt_registry().lock().unwrap().get(&handle).map(|c| c.render())+ }))+ .unwrap_or(None);+ match out {+ Some(s) => Ok(out_string(env, s)),+ None => Ok(std::ptr::null_mut()),+ }+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtToStateNodesJson -> jstring; (_class: JClass<'l>, handle: jlong) |env| {+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ crdt_registry().lock().unwrap().get(&handle).map(|c| c.to_state_nodes_json())+ }))+ .unwrap_or(None);+ match out {+ Some(s) => Ok(out_string(env, s)),+ None => Ok(std::ptr::null_mut()),+ }+ } } - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_crdtFree<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- ) {- env.with_env(|env| -> jni::errors::Result<()> {- let _ = env;- crdt_registry().lock().unwrap().remove(&handle);- Ok(())- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_crdtFree; (_class: JClass<'l>, handle: jlong) |env| {+ let _ = env;+ crdt_registry().lock().unwrap().remove(&handle);+ Ok(())+ } } // --- ODF formula engine --- - #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_nativeCreateWorkbook<- 'l,- >(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- json: JString<'l>,- now_millis: jlong,- ) -> jlong {- env.with_env(|env| -> jni::errors::Result<jlong> {- let json = match read_string(env, &json) {- Some(v) => v,- None => return Ok(0),- };- let wb = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- Workbook::from_json(&json, now_millis)- }))- .unwrap_or(None);- match wb {- Some(w) => {- let h = next_handle();- wb_registry().lock().unwrap().insert(h, w);- Ok(h)- }- None => Ok(0),- }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_nativeDisplayValue<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- sheet_idx: jint,- row: jint,- col: jint,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- wb_registry()- .lock()- .unwrap()- .get(&handle)- .map(|w| w.display_value(sheet_idx as usize, row, col))- }))- .unwrap_or(None);- match out {- Some(s) => Ok(out_string(env, s)),- None => Ok(std::ptr::null_mut()),+ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_nativeCreateWorkbook -> jlong; (_class: JClass<'l>, json: JString<'l>, now_millis: jlong) |env| {+ let json = match read_string(env, &json) {+ Some(v) => v,+ None => return Ok(0),+ };+ let wb = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ Workbook::from_json(&json, now_millis)+ }))+ .unwrap_or(None);+ match wb {+ Some(w) => {+ let h = next_handle();+ wb_registry().lock().unwrap().insert(h, w);+ Ok(h) }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_nativeIsNumeric<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- sheet_idx: jint,- row: jint,- col: jint,- ) -> jboolean {- env.with_env(|env| -> jni::errors::Result<jboolean> {- let _ = env;- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- wb_registry()- .lock()- .unwrap()- .get(&handle)- .map(|w| w.is_numeric(sheet_idx as usize, row, col))- }))- .unwrap_or(None);- match out {- Some(true) => Ok(true),- _ => Ok(false),- }- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_nativeFree<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- handle: jlong,- ) {- env.with_env(|env| -> jni::errors::Result<()> {- let _ = env;- wb_registry().lock().unwrap().remove(&handle);- Ok(())- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_nativeFormatValue<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- value: jdouble,- number_format_json: JString<'l>,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let nf_json = read_string(env, &number_format_json).unwrap_or_default();- let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {- formula::format_value_json(value, &nf_json)- }))- .unwrap_or_else(|_| String::new());- Ok(out_string(env, out))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- /// Excel number-format code → the ODF number-style model, as JSON. `null` (a JSON `null`)- /// means "General"/text, i.e. no explicit style — matching `ExcelNumFmt.parse` returning null.- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_numFmtParse<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- code: JString<'l>,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let Some(code) = read_string(env, &code) else {- return Ok(out_string(env, "null".to_string()));- };- let json = crate::numfmt::parse(&code)- .map(|f| serde_json::to_string(&f).unwrap_or_else(|_| "null".into()))- .unwrap_or_else(|| "null".into());- Ok(out_string(env, json))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- /// Builtin `numFmtId` → the ODF number-style model, as JSON, or `null`.- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_numFmtForBuiltin<'l>(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- id: jint,- ) -> jstring {- env.with_env(|env| -> jni::errors::Result<jstring> {- let json = crate::numfmt::for_builtin(id)- .map(|f| serde_json::to_string(&f).unwrap_or_else(|_| "null".into()))- .unwrap_or_else(|| "null".into());- Ok(out_string(env, json))- })- .resolve::<jni::errors::LogErrorAndDefault>()- }-- #[no_mangle]- pub extern "system" fn Java_com_vayunmathur_office_util_OfficeNative_numFmtIsDateTimeBuiltin<- 'l,- >(- mut env: EnvUnowned<'l>,- _class: JClass<'l>,- id: jint,- ) -> jboolean {- env.with_env(|env| -> jni::errors::Result<jboolean> {- let _ = env;- Ok(crate::numfmt::is_date_time_builtin(id) as jboolean)- })- .resolve::<jni::errors::LogErrorAndDefault>()- }+ None => Ok(0),+ }+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_nativeDisplayValue -> jstring; (_class: JClass<'l>, handle: jlong, sheet_idx: jint, row: jint, col: jint) |env| {+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ wb_registry()+ .lock()+ .unwrap()+ .get(&handle)+ .map(|w| w.display_value(sheet_idx as usize, row, col))+ }))+ .unwrap_or(None);+ match out {+ Some(s) => Ok(out_string(env, s)),+ None => Ok(std::ptr::null_mut()),+ }+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_nativeIsNumeric -> jboolean; (_class: JClass<'l>, handle: jlong, sheet_idx: jint, row: jint, col: jint) |env| {+ let _ = env;+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ wb_registry()+ .lock()+ .unwrap()+ .get(&handle)+ .map(|w| w.is_numeric(sheet_idx as usize, row, col))+ }))+ .unwrap_or(None);+ match out {+ Some(true) => Ok(true),+ _ => Ok(false),+ }+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_nativeFree; (_class: JClass<'l>, handle: jlong) |env| {+ let _ = env;+ wb_registry().lock().unwrap().remove(&handle);+ Ok(())+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_nativeFormatValue -> jstring; (_class: JClass<'l>, value: jdouble, number_format_json: JString<'l>) |env| {+ let nf_json = read_string(env, &number_format_json).unwrap_or_default();+ let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ formula::format_value_json(value, &nf_json)+ }))+ .unwrap_or_else(|_| String::new());+ Ok(out_string(env, out))+ } }++ // Excel number-format code → the ODF number-style model, as JSON. `null` (a JSON `null`)+ // means "General"/text, i.e. no explicit style — matching `ExcelNumFmt.parse` returning null.+ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_numFmtParse -> jstring; (_class: JClass<'l>, code: JString<'l>) |env| {+ let Some(code) = read_string(env, &code) else {+ return Ok(out_string(env, "null".to_string()));+ };+ let json = crate::numfmt::parse(&code)+ .map(|f| serde_json::to_string(&f).unwrap_or_else(|_| "null".into()))+ .unwrap_or_else(|| "null".into());+ Ok(out_string(env, json))+ } }++ // Builtin `numFmtId` → the ODF number-style model, as JSON, or `null`.+ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_numFmtForBuiltin -> jstring; (_class: JClass<'l>, id: jint) |env| {+ let json = crate::numfmt::for_builtin(id)+ .map(|f| serde_json::to_string(&f).unwrap_or_else(|_| "null".into()))+ .unwrap_or_else(|| "null".into());+ Ok(out_string(env, json))+ } }++ jni_fn! { Java_com_vayunmathur_office_util_OfficeNative_numFmtIsDateTimeBuiltin -> jboolean; (_class: JClass<'l>, id: jint) |env| {+ let _ = env;+ Ok(crate::numfmt::is_date_time_builtin(id) as jboolean)+ } } }