Skip to content

Commit

Permalink
add jtypes macro and test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSchuetz97 committed Oct 12, 2024
1 parent bc4e4c3 commit 7532566
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub unsafe extern "system" fn Java_some_package_ClassName_method(env: JNIEnv, cl
//Variant 1: Variadic up-calls:
//BE CAREFUL, this method is sensitive to difference between i32/i16/i8 etc.
//So always specify the type so that it matches the type of the Java Method.
//Letting the compiler choose the may or may not work!
//Letting the compiler choose the type may or may not work!
//Passing a different argument than what the Java Method has is UB!
//A sealed trait ensures that only parameters that the JVM can understand can be passed here
//So for example accidentally passing a &str to these methods will not compile.
Expand All @@ -245,10 +245,14 @@ pub unsafe extern "system" fn Java_some_package_ClassName_method(env: JNIEnv, cl
//is implemented for all types that can be passed to java as a parameter.
//You could also use a Vec<jtype> to obtain your pointer to the jtype's!
env.CallStaticVoidMethodA(class, meth0, null());
env.CallStaticVoidMethodA(class, meth1, [1i32.into()].as_ptr());
env.CallStaticVoidMethodA(class, meth2, [1i32.into(), 1i32.into()].as_ptr());
env.CallStaticVoidMethodA(class, meth3, [1i32.into(), 1i32.into(), 1i32.into()].as_ptr());
env.CallStaticVoidMethodA(class, meth4, [1i32.into(), 1i32.into(), 1i32.into(), 1i32.into()].as_ptr());
env.CallStaticVoidMethodA(class, meth1, jtypes!(1i32).as_ptr());
env.CallStaticVoidMethodA(class, meth2, jtypes!(1i32, 2i32).as_ptr());
env.CallStaticVoidMethodA(class, meth3, jtypes!(1i32, 2i32, 3i32).as_ptr());
env.CallStaticVoidMethodA(class, meth4, jtypes!(1i32, 2i32, 3i32, 4i32).as_ptr());
//There is no "practical" limit to how large you could make this array/vec.
//For reference the jtypes! macro resolves to this
let macro_result : [jtype; 4] = jtypes!(1i32, 2i32, 3i32, 4i32);
//And is just short for:
let macro_result : [jtype; 4] = [jtype::from(1i32), jtype::from(2i32), jtype::from(3i32), jtype::from(4i32)];
}
```
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,42 @@ pub union jtype {
throwable: jthrowable
}

///
/// This macro is usefull for constructing jtype arrays.
/// This is often needed when making upcalls into the jvm with many arguments using the 'A' type functions:
/// * CallStatic(TYPE)MethodA
/// * CallStaticVoidMethodA
/// * CallStaticIntMethodA
/// * ...
/// * Call(TYPE)MethodA
/// * CallVoidMethodA
/// * ...
/// * NewObjectA
///
/// # Example
/// ```rust
/// use jni_simple::{*};
///
/// unsafe fn test(env: JNIEnv, class: jclass) {
/// //public static void methodWith5Params(int a, int b, long c, long d, boolean e) {}
/// let meth = env.GetStaticMethodID_str(class, "methodWith5Params", "(IIJJZ)V");
/// if meth.is_null() {
/// unimplemented!("handle method not found");
/// }
/// // methodWith5Params(16, 32, 12, 13, false);
/// env.CallStaticVoidMethodA(class, meth, jtypes!(16i32, 64i32, 12i64, 13i64, false).as_ptr());
/// }
/// ```
///
#[macro_export]
macro_rules! jtypes {
( $($x:expr),* ) => {
{
[ $(jtype::from($x)),* ]
}
};
}

impl Debug for jtype {
#[inline(never)]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down
20 changes: 20 additions & 0 deletions tests/macro_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use jni_simple::{jobject, jtype, jtypes};



#[test]
fn test() {
unsafe {
let mut v = vec![64; 0];
let m : jobject = v.as_mut_ptr().cast();
let n= jtypes!(1i32, 2i32, 3i32, 4f64, m);
assert_eq!(n[0].int(), 1);
assert_eq!(n[1].int(), 2);
assert_eq!(n[2].int(), 3);
assert_eq!(n[3].double(), 4f64);
assert_eq!(n[4].object(), m);

assert_ne!(0usize, std::hint::black_box(jtypes!(1i32, 2i32, 3i32, 4f64, m)).as_ptr() as usize)
}

}

0 comments on commit 7532566

Please sign in to comment.