-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.rs
48 lines (44 loc) · 1.35 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use proc_macro2::Span;
use proc_macro_crate::crate_name;
use syn::{Ident, ItemFn};
/// Determines if this block of code was [generated by near_bindgen].
/// Heuristic used is to check for #[no_mangle].
/// TODO: How to make this 100% safe. Discuss with near-sdk team
///
/// [generated by near_bindgen]: https://github.com/near/near-sdk-rs/issues/722
pub(crate) fn is_near_bindgen_wrapped_or_marshall(item: &ItemFn) -> bool {
let pattern1 = "(target_arch = \"wasm32\")";
let pattern2 = "(not(target_arch = \"wasm32\"))";
item.attrs.iter().any(|attr| {
let seq = attr.tokens.to_string();
seq == pattern1 || seq == pattern2
})
}
/// Returns an identifier for the name of the crate which is imported by plugin users.
pub(crate) fn cratename() -> Ident {
Ident::new(
&crate_name("near-plugins").unwrap_or_else(|_| "near_plugins".to_string()),
Span::call_site(),
)
}
/// Injects extra code into a function.
pub(crate) fn add_extra_code_to_fn(
fn_code: &ItemFn,
extra_code: proc_macro2::TokenStream,
) -> proc_macro::TokenStream {
let ItemFn {
attrs,
vis,
sig,
block,
} = fn_code;
let stmts = &block.stmts;
// https://stackoverflow.com/a/66851407
quote::quote! {
#(#attrs)* #vis #sig {
#extra_code
#(#stmts)*
}
}
.into()
}