feat: automatic FromJava conversions

This commit is contained in:
əlemi 2024-09-22 00:09:46 +02:00
parent 4daaad5faf
commit 95eb528549
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 145 additions and 33 deletions

View file

@ -4,6 +4,7 @@ use syn::Ident;
pub(crate) struct ArgumentOptions { pub(crate) struct ArgumentOptions {
pub(crate) incoming: TokenStream, pub(crate) incoming: TokenStream,
pub(crate) transforming: TokenStream,
pub(crate) forwarding: TokenStream, pub(crate) forwarding: TokenStream,
pub(crate) env: Ident, pub(crate) env: Ident,
} }
@ -47,7 +48,7 @@ fn type_equals(ty: Box<syn::Type>, search: impl AsRef<str>) -> bool {
} }
impl ArgumentOptions { impl ArgumentOptions {
pub(crate) fn parse_args(fn_item: &syn::ItemFn) -> Result<Self, syn::Error> { pub(crate) fn parse_args(fn_item: &syn::ItemFn, ret_expr: TokenStream) -> Result<Self, syn::Error> {
let mut arguments = Vec::new(); let mut arguments = Vec::new();
let mut pass_env = false; let mut pass_env = false;
let mut pass_class = false; let mut pass_class = false;
@ -62,29 +63,10 @@ impl ArgumentOptions {
pat: syn::Ident::new(&pat.to_string(), Span::call_site()), pat: syn::Ident::new(&pat.to_string(), Span::call_site()),
ty: ty.ty.clone(), ty: ty.ty.clone(),
}); });
// if env.is_none() {
// if type_equals(ty.ty.clone(), "JNIEnv") {
// env = Some(syn::Ident::new(&pat.to_string(), Span::call_site()));
// } else {
// let envv = ;
// incoming.append_all(quote::quote!( #envv: jni::JNIEnv<'local>,));
// env = Some(envv);
// }
// }
// if class.is_none() && !type_equals(ty.ty.clone(), "JNIEnv") {
// if type_equals(ty.ty.clone(), "JClass") {
// class = Some(syn::Ident::new(&pat.to_string(), Span::call_site()));
// } else {
// let classs = syn::Ident::new("class", Span::call_site());
// incoming.append_all(quote::quote!( #classs: jni::objects::JClass<'local>,));
// class = Some(classs);
// }
// }
// incoming.append_all(quote::quote!( #ty , ));
// forwarding.append_all(quote::quote! ( #pat, ));
} }
let mut incoming = TokenStream::new(); let mut incoming = TokenStream::new();
let mut transforming = TokenStream::new();
let mut forwarding = TokenStream::new(); let mut forwarding = TokenStream::new();
let env = if pass_env { let env = if pass_env {
@ -115,12 +97,23 @@ impl ArgumentOptions {
for arg in args_iter { for arg in args_iter {
let pat = arg.pat; let pat = arg.pat;
let new_pat = syn::Ident::new(&format!("{pat}_new"), Span::call_site());
let ty = arg.ty; let ty = arg.ty;
incoming.append_all(quote::quote!( mut #pat: #ty,)); transforming.append_all(quote::quote!{
forwarding.append_all(quote::quote!( #pat,)); let #new_pat = match jni_toolbox::from_java_static::<#ty>(&mut #env, #pat) {
Ok(x) => x,
Err(e) => {
// TODO should we panic here instead?
let _ = #env.throw_new("java/lang/RuntimeException", format!("{e:?}"));
return #ret_expr;
},
};
});
incoming.append_all(quote::quote!( mut #pat: <#ty as jni_toolbox::FromJava<'local>>::T,));
forwarding.append_all(quote::quote!( #new_pat,));
} }
Ok(Self { incoming, forwarding, env }) Ok(Self { incoming, transforming, forwarding, env })
} }
} }

View file

@ -12,8 +12,17 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re
}; };
let attrs = AttrsOptions::parse_attr(attrs)?; let attrs = AttrsOptions::parse_attr(attrs)?;
let args = ArgumentOptions::parse_args(&fn_item)?;
let ret = ReturnOptions::parse_signature(&fn_item.sig.output)?; let ret = ReturnOptions::parse_signature(&fn_item.sig.output)?;
let return_expr = if ret.ty.is_none() {
quote::quote!( () )
} else if attrs.return_pointer {
quote::quote!( std::ptr::null_mut() )
} else {
quote::quote!( 0 )
};
// TODO a bit ugly passing the return expr down... we should probably manage returns here
let args = ArgumentOptions::parse_args(&fn_item, return_expr.clone())?;
let return_type = ret.tokens(); let return_type = ret.tokens();
@ -32,20 +41,16 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re
}; };
// ^----------------------------------^ // ^----------------------------------^
let return_expr = if attrs.return_pointer {
quote::quote!( std::ptr::null_mut() )
} else {
quote::quote!( 0 )
};
let env_ident = args.env; let env_ident = args.env;
let forwarding = args.forwarding; let forwarding = args.forwarding;
let transforming = args.transforming;
let body = if ret.result { // wrap errors let body = if ret.result { // wrap errors
if let Some(exception) = attrs.exception { if let Some(exception) = attrs.exception {
// V----------------------------------V // V----------------------------------V
quote::quote! { quote::quote! {
{ {
use jni_toolbox::JniToolboxError; use jni_toolbox::{JniToolboxError, FromJava};
#transforming
match #fn_name_inner(#forwarding) { match #fn_name_inner(#forwarding) {
Ok(ret) => ret, Ok(ret) => ret,
Err(e) => match #env_ident.throw_new(#exception, format!("{e:?}")) { Err(e) => match #env_ident.throw_new(#exception, format!("{e:?}")) {
@ -60,10 +65,11 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re
// V----------------------------------V // V----------------------------------V
quote::quote! { quote::quote! {
{ {
use jni_toolbox::JniToolboxError; use jni_toolbox::{JniToolboxError, FromJava};
// NOTE: this should be SAFE! the cloned env reference lives less than the actual one, we just lack a // NOTE: this should be SAFE! the cloned env reference lives less than the actual one, we just lack a
// way to get it back from the called function and thus resort to unsafe cloning // way to get it back from the called function and thus resort to unsafe cloning
let mut env_copy = unsafe { #env_ident.unsafe_clone() }; let mut env_copy = unsafe { #env_ident.unsafe_clone() };
#transforming
match #fn_name_inner(#forwarding) { match #fn_name_inner(#forwarding) {
Err(e) => match env_copy.find_class(e.jclass()) { Err(e) => match env_copy.find_class(e.jclass()) {
Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"), Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"),
@ -87,6 +93,8 @@ pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Re
// V----------------------------------V // V----------------------------------V
quote::quote! { quote::quote! {
{ {
use jni_toolbox::{JniToolboxError, FromJava};
#transforming
#fn_name_inner(#forwarding) #fn_name_inner(#forwarding)
} }
} }

View file

@ -1,3 +1,4 @@
use jni::objects::JString;
pub use jni_toolbox_macro::jni; pub use jni_toolbox_macro::jni;
pub trait JniToolboxError: std::error::Error { pub trait JniToolboxError: std::error::Error {
@ -15,3 +16,113 @@ impl JniToolboxError for jni::errors::JniError {
"java/lang/RuntimeException".to_string() "java/lang/RuntimeException".to_string()
} }
} }
pub fn from_java_static<'j, T: FromJava<'j>>(env: &mut jni::JNIEnv<'j>, val: T::T) -> Result<T, jni::errors::Error> {
T::from_java(env, val)
}
pub trait FromJava<'j> : Sized {
type T : Sized;
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error>;
}
impl<'j> FromJava<'j> for bool {
type T = jni::sys::jboolean;
#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(value == 1)
}
}
impl<'j> FromJava<'j> for i64 {
type T = jni::sys::jlong;
#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(value)
}
}
impl<'j> FromJava<'j> for i32 {
type T = jni::sys::jint;
#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(value)
}
}
impl<'j> FromJava<'j> for i16 {
type T = jni::sys::jshort;
#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(value)
}
}
impl<'j> FromJava<'j> for f32 {
type T = jni::sys::jfloat;
#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(value)
}
}
impl<'j> FromJava<'j> for f64 {
type T = jni::sys::jdouble;
#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
Ok(value)
}
}
impl<'j> FromJava<'j> for String {
type T = jni::objects::JString<'j>;
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error> {
if value.is_null() { return Err(jni::errors::Error::NullPtr("string can't be null")) };
Ok(env.get_string(&value)?.into())
}
}
impl<'j> FromJava<'j> for Option<String> {
type T = jni::objects::JString<'j>;
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error> {
if value.is_null() { return Ok(None) };
Ok(Some(String::from_java(env, value)?))
}
}
pub trait IntoJava<'j> {
type T;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error>;
}
impl<'j> IntoJava<'j> for String {
type T = JString<'j>;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
env.new_string(self)
}
}