|
| 1 | +use crate::env::JniEnv; |
| 2 | +use crate::error::{JniError, Result}; |
| 3 | +use crate::objects::{ |
| 4 | + JArray, JBooleanArray, JByteArray, JCharArray, JClass, JDoubleArray, JFloatArray, JIntArray, |
| 5 | + JLongArray, JObject, JObjectArray, JShortArray, JString, JThrowable, JWeak, |
| 6 | +}; |
| 7 | +use crate::string::JniString; |
| 8 | + |
| 9 | +use std::ffi::c_void; |
| 10 | + |
| 11 | +use jni_sys::{jboolean, jbyte, jchar, jdouble, jfloat, jint, jlong, jshort}; |
| 12 | + |
| 13 | +pub struct JniNativeMethod { |
| 14 | + name: JniString, |
| 15 | + signature: JniString, |
| 16 | + fn_ptr: *mut c_void, |
| 17 | +} |
| 18 | + |
| 19 | +impl JniNativeMethod { |
| 20 | + pub fn from<F, N, S>(name: N, signature: S, fn_ptr: F) -> Self |
| 21 | + where |
| 22 | + F: JniMethod, |
| 23 | + N: Into<JniString>, |
| 24 | + S: Into<JniString>, |
| 25 | + { |
| 26 | + Self { |
| 27 | + name: name.into(), |
| 28 | + signature: signature.into(), |
| 29 | + fn_ptr: fn_ptr.raw(), |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + fn raw(&self) -> jni_sys::JNINativeMethod { |
| 34 | + jni_sys::JNINativeMethod { |
| 35 | + name: self.name.as_cstr().as_ptr().cast_mut(), |
| 36 | + signature: self.signature.as_cstr().as_ptr().cast_mut(), |
| 37 | + fnPtr: self.fn_ptr.cast(), |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub trait JniMethod { |
| 43 | + fn raw(self) -> *mut c_void; |
| 44 | +} |
| 45 | + |
| 46 | +macro_rules! impl_jni_method { |
| 47 | + ( |
| 48 | + $(($($params:ident),*)),* $(,)? |
| 49 | + ) => { |
| 50 | + $( |
| 51 | + impl<R, $($params),*> JniMethod for fn(JniEnv, JObject, $($params),*) -> R |
| 52 | + where |
| 53 | + R: JniMethodReturnType, |
| 54 | + $($params: JniMethodParameter),* |
| 55 | + { |
| 56 | + fn raw(self) -> *mut c_void { |
| 57 | + self as _ |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + impl<R, $($params),*> JniMethod for fn(JniEnv, JClass, $($params),*) -> R |
| 62 | + where |
| 63 | + R: JniMethodReturnType, |
| 64 | + $($params: JniMethodParameter),* |
| 65 | + { |
| 66 | + fn raw(self) -> *mut c_void { |
| 67 | + self as _ |
| 68 | + } |
| 69 | + } |
| 70 | + )* |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl_jni_method!( |
| 75 | + (), |
| 76 | + (T), |
| 77 | + (T, U), |
| 78 | + (T, U, V), |
| 79 | + (T, U, V, W), |
| 80 | + (T, U, V, W, X), |
| 81 | + (T, U, V, W, X, Y), |
| 82 | + (T, U, V, W, X, Y, Z), |
| 83 | + (T, U, V, W, X, Y, Z, A), |
| 84 | + (T, U, V, W, X, Y, Z, A, B), |
| 85 | + (T, U, V, W, X, Y, Z, A, B, C), |
| 86 | + (T, U, V, W, X, Y, Z, A, B, C, D), |
| 87 | + (T, U, V, W, X, Y, Z, A, B, C, D, E), |
| 88 | + (T, U, V, W, X, Y, Z, A, B, C, D, E, F), |
| 89 | + (T, U, V, W, X, Y, Z, A, B, C, D, E, F, G), |
| 90 | +); |
| 91 | + |
| 92 | +pub trait JniMethodParameter: sealed::Sealed {} |
| 93 | +macro_rules! params { |
| 94 | + ($($ty:ident),* $(,)*) => { |
| 95 | + $( |
| 96 | + impl JniMethodParameter for $ty {} |
| 97 | + impl sealed::Sealed for $ty {} |
| 98 | + )* |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +params!( |
| 103 | + jbyte, |
| 104 | + jchar, |
| 105 | + jdouble, |
| 106 | + jfloat, |
| 107 | + jint, |
| 108 | + jlong, |
| 109 | + jshort, |
| 110 | + jboolean, |
| 111 | + JObject, |
| 112 | + JClass, |
| 113 | + JThrowable, |
| 114 | + JString, |
| 115 | + JWeak, |
| 116 | + JArray, |
| 117 | + JBooleanArray, |
| 118 | + JByteArray, |
| 119 | + JCharArray, |
| 120 | + JShortArray, |
| 121 | + JIntArray, |
| 122 | + JLongArray, |
| 123 | + JFloatArray, |
| 124 | + JDoubleArray, |
| 125 | + JObjectArray, |
| 126 | +); |
| 127 | + |
| 128 | +pub trait JniMethodReturnType: sealed::Sealed {} |
| 129 | + |
| 130 | +impl<T> JniMethodReturnType for T where T: JniMethodParameter {} |
| 131 | + |
| 132 | +impl JniMethodReturnType for () {} |
| 133 | + |
| 134 | +mod sealed { |
| 135 | + pub(super) trait Sealed {} |
| 136 | + impl Sealed for () {} |
| 137 | +} |
| 138 | + |
1 | 139 | impl super::JniEnv { |
2 | | - // TODO: RegisterNatives |
| 140 | + /// Registers native methods with the class specified by the `clazz` argument. |
| 141 | + /// |
| 142 | + /// ## PARAMETERS |
| 143 | + /// |
| 144 | + /// `clazz`: a Java class object. |
| 145 | + /// `methods`: the native methods in the class. |
| 146 | + /// |
| 147 | + /// ## THROWS |
| 148 | + /// |
| 149 | + /// `NoSuchMethodError`: if a specified method cannot be found or if the method is not native. |
| 150 | + pub fn register_natives(&self, clazz: JClass, methods: &[JniNativeMethod]) -> Result<()> { |
| 151 | + assert!(!self.raw().is_null()); |
| 152 | + |
| 153 | + let methods = methods.iter().map(JniNativeMethod::raw).collect::<Vec<_>>(); |
| 154 | + |
| 155 | + let ret; |
| 156 | + unsafe { |
| 157 | + let invoke_interface = self.as_native_interface(); |
| 158 | + ret = ((*invoke_interface).RegisterNatives)( |
| 159 | + self.0.cast::<jni_sys::JNIEnv>(), |
| 160 | + clazz.raw(), |
| 161 | + methods.as_ptr(), |
| 162 | + methods.len() as jint, |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + if self.exception_check() { |
| 167 | + return Err(JniError::ExceptionThrown); |
| 168 | + } |
| 169 | + |
| 170 | + if ret != 0 { |
| 171 | + return Err(JniError::Unknown); |
| 172 | + } |
| 173 | + |
| 174 | + Ok(()) |
| 175 | + } |
3 | 176 | // TODO: UnregisterNatives |
4 | 177 | } |
0 commit comments