Skip to content

Instantly share code, notes, and snippets.

@necauqua
Created April 9, 2020 23:38
Show Gist options
  • Save necauqua/28db3d10b2c673b4759663c1af670765 to your computer and use it in GitHub Desktop.
Save necauqua/28db3d10b2c673b4759663c1af670765 to your computer and use it in GitHub Desktop.
Simple macro for nice looking JNI signatures
extern crate proc_macro;
use quote::quote;
use proc_macro::TokenStream;
// this macro is dumb in a lot of ways, this is just a prototype
#[proc_macro_attribute]
pub fn java(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut name = String::new();
name += "Java_";
for tt in attr {
let string = tt.to_string();
if &string == "." {
name.push('_');
} else {
name += &string;
}
}
name += "_";
let input = syn::parse_macro_input!(item as syn::ItemFn);
let mut flag = false;
for ch in input.sig.ident.to_string().chars() {
if ch == '_' {
flag = true;
} else if flag {
flag = false;
name.push(ch.to_ascii_uppercase());
} else {
name.push(ch);
}
}
let name = syn::Ident::new(&name, proc_macro2::Span::call_site());
let params = input.sig.inputs;
let ret = input.sig.output;
let body = input.block;
quote![
#[no_mangle]
pub extern fn #name(env: jni::JNIEnv, this: jni::objects::JObject, #params) #ret {
#body
}
].into()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment