-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e91bb36
commit 917ef57
Showing
11 changed files
with
974 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"rust-analyzer.showUnlinkedFileNotification": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "jnat-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.66" | ||
quote = "1.0.32" | ||
syn = { version = "2.0.27", features = ["full"] } | ||
|
||
[lib] | ||
proc-macro = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use proc_macro::TokenStream; | ||
use quote::ToTokens; | ||
use syn::{parse::Parser, parse_macro_input, punctuated::Punctuated, Ident, ItemFn, Path, Token}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn jnat(args: TokenStream, item: TokenStream) -> TokenStream { | ||
let args = Punctuated::<Path, Token![.]>::parse_terminated | ||
.parse(args) | ||
.unwrap(); | ||
let item = parse_macro_input!(item as ItemFn); | ||
|
||
let method_name = args | ||
.iter() | ||
.map(|x| x.to_token_stream().to_string()) | ||
.collect::<Vec<String>>() | ||
.join("_"); | ||
let method_name = format!("Java_{}_{}", method_name, item.sig.ident.to_string()); | ||
|
||
let item = item.clone(); | ||
let mut native_fn = item.clone(); | ||
|
||
native_fn.sig.ident = Ident::new(&method_name, native_fn.sig.ident.span()); | ||
native_fn.sig.abi = Some(syn::parse_quote!(extern "system")); | ||
|
||
// Changing visibility and params is probably unexpected behavior, just leaving this here for reference | ||
|
||
/* item | ||
.sig | ||
.inputs | ||
.insert(0, syn::parse_quote!(mut env: jnat::jni::JNIEnv)); | ||
item | ||
.sig | ||
.inputs | ||
.insert(1, syn::parse_quote!(class: jnat::jni::objects::JClass)); */ | ||
|
||
/* native_fn.vis = Visibility::Public(Pub { | ||
span: Span::call_site(), | ||
}); */ | ||
|
||
// We want #[no_mangle] and #[allow(non_snake_case)] | ||
|
||
let out = "#[no_mangle]\n#[allow(non_snake_case)]\n".to_string() | ||
+ &native_fn.to_token_stream().to_string() | ||
+ "\n" | ||
+ &item.to_token_stream().to_string(); | ||
|
||
out.parse().unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ mod test; | |
|
||
#[cfg(feature = "jni")] | ||
pub use jni; | ||
|
||
#[cfg(feature = "jnat-macros")] | ||
pub use jnat_macros; |