jni_glue/
lib.rs

1//! Common glue code between Rust and JNI, used in autogenerated jni-bindgen glue code.
2//! 
3//! See also the [Android JNI tips](https://developer.android.com/training/articles/perf-jni) documentation as well as the
4//! [Java Native Interface Specification](https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html).
5
6// Re-export a few things such that we have a consistent name for them in autogenerated glue code wherever we go.
7#[doc(hidden)] pub use ::std as std;
8#[doc(hidden)] pub use ::jni_sys as jni_sys;
9
10use jni_sys::*;
11use lazy_static::*;
12
13use std::ffi::*;
14use std::ptr::*;
15use std::marker::PhantomData;
16use std::ops::Deref;
17use std::sync::*;
18
19pub(crate) mod backends { // XXX: Might expose this to the end user in order to let them choose which backend to use...?
20    use super::*;
21
22    mod single_vm_backend;
23
24    pub(crate) use single_vm_backend::*;
25}
26
27mod refs {
28    use super::*;
29
30    mod argument;
31    mod global;
32    mod local;
33    mod ref_;
34
35    pub use argument::*;
36    pub use global::*;
37    pub use local::*;
38    pub use ref_::*;
39}
40
41mod __jni_bindgen;
42mod array;
43mod as_jvalue;
44mod as_valid_jobject_and_env;
45mod env;
46mod gen_vm;
47mod jchar_;
48mod jni_type;
49mod object_and_env;
50mod string_chars;
51mod throwable_type;
52mod vm;
53
54pub use array::*;
55pub use as_jvalue::*;
56pub use as_valid_jobject_and_env::*;
57pub use env::*;
58pub(crate) use gen_vm::*;
59pub use jchar_::{jchar, *};
60pub use jni_type::JniType;
61pub use object_and_env::*;
62pub use refs::*;
63pub use string_chars::*;
64pub use throwable_type::*;
65pub use vm::*;
66
67
68
69type VmBackend = backends::SingleVmBackend;
70lazy_static! { // RwLock::new is not const
71    static ref VMS : RwLock<VmBackend> = RwLock::new(VmBackend::new());
72}
73
74/// **Disable "unsafe-manual-jni-load-unload", or call from JNI_OnLoad, or there may be soundness issues!**
75#[cfg(feature = "unsafe-manual-jni-load-unload")]
76pub unsafe fn on_load(vm: *const JavaVM, _reserved: *const c_void) -> jint {
77    VMS.write().unwrap().on_load(vm);
78    JNI_VERSION_1_2
79}
80
81/// **Disable "unsafe-manual-jni-load-unload", or call from JNI_OnUnload, or there will be soundness issues!**
82#[cfg(feature = "unsafe-manual-jni-load-unload")]
83pub fn on_unload(vm: *const JavaVM, _reserved: *const c_void) {
84    VMS.write().unwrap().on_unload(vm);
85}
86
87/// **Do not call!**  Automatically invoked by the JVM.  See "unsafe-manual-jni-load-unload" to override this behavior.
88#[no_mangle] #[allow(non_snake_case)] #[cfg(not(feature = "unsafe-manual-jni-load-unload"))]
89pub unsafe extern "system" fn JNI_OnLoad(vm: *const JavaVM, _reserved: *const c_void) -> jint {
90    VMS.write().unwrap().on_load(vm);
91    JNI_VERSION_1_2
92}
93
94/// **Do not call!**  Automatically invoked by the JVM.  See "unsafe-manual-jni-load-unload" to override this behavior.
95#[no_mangle] #[allow(non_snake_case)] #[cfg(not(feature = "unsafe-manual-jni-load-unload"))]
96pub extern "system" fn JNI_OnUnload(vm: *const JavaVM, _reserved: *const c_void) {
97    VMS.write().unwrap().on_unload(vm);
98}