2024-09-21 20:14:51 +02:00
|
|
|
use proc_macro2::{Span, TokenStream};
|
|
|
|
use quote::TokenStreamExt;
|
2024-09-21 20:46:48 +02:00
|
|
|
use syn::Item;
|
2024-09-21 20:14:51 +02:00
|
|
|
|
2024-09-21 23:03:02 +02:00
|
|
|
use crate::{args::ArgumentOptions, attrs::AttrsOptions, ret::ReturnOptions};
|
2024-09-21 20:14:51 +02:00
|
|
|
|
|
|
|
pub(crate) fn generate_jni_wrapper(attrs: TokenStream, input: TokenStream) -> Result<TokenStream, syn::Error> {
|
|
|
|
let mut out = TokenStream::new();
|
|
|
|
|
|
|
|
let Item::Fn(fn_item) = syn::parse2(input.clone())? else {
|
|
|
|
return Err(syn::Error::new(Span::call_site(), "#[jni] is only supported on functions"));
|
|
|
|
};
|
|
|
|
|
|
|
|
let attrs = AttrsOptions::parse_attr(attrs)?;
|
2024-09-21 23:03:02 +02:00
|
|
|
let ret = ReturnOptions::parse_signature(&fn_item.sig.output)?;
|
2024-09-22 01:37:28 +02:00
|
|
|
let return_expr = if ret.void {
|
2024-09-22 00:09:46 +02:00
|
|
|
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())?;
|
2024-09-21 20:14:51 +02:00
|
|
|
|
|
|
|
|
2024-09-21 23:03:02 +02:00
|
|
|
let return_type = ret.tokens();
|
2024-09-21 20:14:51 +02:00
|
|
|
let name = fn_item.sig.ident.to_string();
|
|
|
|
let name_jni = name.replace("_", "_1");
|
|
|
|
let fn_name_inner = syn::Ident::new(&name, Span::call_site());
|
|
|
|
let fn_name = syn::Ident::new(&format!("Java_{}_{}_{name_jni}", attrs.package, attrs.class), Span::call_site());
|
|
|
|
|
2024-09-21 20:46:48 +02:00
|
|
|
|
2024-09-21 23:03:02 +02:00
|
|
|
let incoming = args.incoming;
|
|
|
|
// V----------------------------------V
|
2024-09-21 20:46:48 +02:00
|
|
|
let header = quote::quote! {
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
pub extern "system" fn #fn_name<'local>(#incoming) #return_type
|
2024-09-21 20:14:51 +02:00
|
|
|
};
|
2024-09-21 23:03:02 +02:00
|
|
|
// ^----------------------------------^
|
2024-09-21 20:14:51 +02:00
|
|
|
|
2024-09-21 23:03:02 +02:00
|
|
|
let env_ident = args.env;
|
|
|
|
let forwarding = args.forwarding;
|
2024-09-22 00:09:46 +02:00
|
|
|
let transforming = args.transforming;
|
2024-09-21 20:46:48 +02:00
|
|
|
let body = if ret.result { // wrap errors
|
2024-09-21 20:14:51 +02:00
|
|
|
if let Some(exception) = attrs.exception {
|
2024-09-21 23:03:02 +02:00
|
|
|
// V----------------------------------V
|
2024-09-21 20:14:51 +02:00
|
|
|
quote::quote! {
|
2024-09-21 20:46:48 +02:00
|
|
|
{
|
2024-09-22 01:37:28 +02:00
|
|
|
use jni_toolbox::{JniToolboxError, FromJava, IntoJava};
|
2024-09-22 00:09:46 +02:00
|
|
|
#transforming
|
2024-09-21 20:14:51 +02:00
|
|
|
match #fn_name_inner(#forwarding) {
|
|
|
|
Ok(ret) => ret,
|
|
|
|
Err(e) => match #env_ident.throw_new(#exception, format!("{e:?}")) {
|
|
|
|
Ok(_) => return #return_expr,
|
|
|
|
Err(e) => panic!("error throwing java exception: {e}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ^----------------------------------^
|
|
|
|
} else {
|
|
|
|
// V----------------------------------V
|
|
|
|
quote::quote! {
|
2024-09-21 20:46:48 +02:00
|
|
|
{
|
2024-09-22 01:37:28 +02:00
|
|
|
use jni_toolbox::{JniToolboxError, FromJava, IntoJava};
|
2024-09-21 23:03:02 +02:00
|
|
|
// NOTE: this should be SAFE! the cloned env reference lives less than the actual one, we just lack a
|
2024-09-21 20:14:51 +02:00
|
|
|
// way to get it back from the called function and thus resort to unsafe cloning
|
|
|
|
let mut env_copy = unsafe { #env_ident.unsafe_clone() };
|
2024-09-22 00:09:46 +02:00
|
|
|
#transforming
|
2024-09-21 20:14:51 +02:00
|
|
|
match #fn_name_inner(#forwarding) {
|
|
|
|
Err(e) => match env_copy.find_class(e.jclass()) {
|
|
|
|
Err(e) => panic!("error throwing Java exception -- failed resolving error class: {e}"),
|
|
|
|
Ok(class) => match env_copy.new_string(format!("{e:?}")) {
|
|
|
|
Err(e) => panic!("error throwing Java exception -- failed creating error string: {e}"),
|
|
|
|
Ok(msg) => match env_copy.new_object(class, "(Ljava/lang/String;)V", &[jni::objects::JValueGen::Object(&msg)]) {
|
|
|
|
Err(e) => panic!("error throwing Java exception -- failed creating object: {e}"),
|
|
|
|
Ok(obj) => match env_copy.throw(jni::objects::JThrowable::from(obj)) {
|
|
|
|
Err(e) => panic!("error throwing Java exception -- failed throwing: {e}"),
|
|
|
|
Ok(_) => return #return_expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-09-22 01:37:28 +02:00
|
|
|
Ok(ret) => match ret.into_java(&mut env_copy) {
|
|
|
|
Ok(fin) => return fin,
|
|
|
|
Err(e) => {
|
|
|
|
// TODO should we panic instead?
|
|
|
|
let _ = env_copy.throw_new("java/lang/RuntimeException", format!("{e:?}"));
|
|
|
|
return #return_expr;
|
|
|
|
}
|
|
|
|
},
|
2024-09-21 20:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// V----------------------------------V
|
|
|
|
quote::quote! {
|
2024-09-21 20:46:48 +02:00
|
|
|
{
|
2024-09-22 01:37:28 +02:00
|
|
|
use jni_toolbox::{JniToolboxError, FromJava, IntoJava};
|
2024-09-22 00:09:46 +02:00
|
|
|
#transforming
|
2024-09-22 01:37:28 +02:00
|
|
|
match #fn_name_inner(#forwarding).into_java(&mut #env_ident) {
|
|
|
|
Ok(res) => return res,
|
|
|
|
Err(e) => {
|
|
|
|
// TODO should we panic instead?
|
|
|
|
let _ = #env_ident.throw_new("java/lang/RuntimeException", format!("{e:?}"));
|
|
|
|
return #return_expr;
|
|
|
|
},
|
|
|
|
}
|
2024-09-21 20:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// ^----------------------------------^
|
|
|
|
};
|
|
|
|
|
|
|
|
out.append_all(input);
|
2024-09-21 20:46:48 +02:00
|
|
|
out.append_all(header);
|
|
|
|
out.append_all(body);
|
2024-09-21 20:14:51 +02:00
|
|
|
Ok(out)
|
|
|
|
}
|