prov_cosmwasm_derive/
lib.rs

1#[macro_use]
2extern crate syn;
3
4use proc_macro::TokenStream;
5use std::str::FromStr;
6
7/// This attribute macro generates the boilerplate required to call into the
8/// contract-specific logic from the entry-points to the Wasm module.
9///
10/// It should be added to the contract's init, handle, migrate and query implementations
11/// like this:
12/// ```
13/// # use prov_cosmwasm_std::{
14/// #     Storage, Api, Querier, DepsMut, Deps, entry_point, Env, StdError, MessageInfo,
15/// #     Response, QueryResponse,
16/// # };
17/// #
18/// # type InstantiateMsg = ();
19/// # type ExecuteMsg = ();
20/// # type QueryMsg = ();
21///
22/// #[entry_point]
23/// pub fn instantiate(
24///     deps: DepsMut,
25///     env: Env,
26///     info: MessageInfo,
27///     msg: InstantiateMsg,
28/// ) -> Result<Response, StdError> {
29/// #   Ok(Default::default())
30/// }
31///
32/// #[entry_point]
33/// pub fn handle(
34///     deps: DepsMut,
35///     env: Env,
36///     info: MessageInfo,
37///     msg: ExecuteMsg,
38/// ) -> Result<Response, StdError> {
39/// #   Ok(Default::default())
40/// }
41///
42/// #[entry_point]
43/// pub fn query(
44///     deps: Deps,
45///     env: Env,
46///     msg: QueryMsg,
47/// ) -> Result<QueryResponse, StdError> {
48/// #   Ok(Default::default())
49/// }
50/// ```
51///
52/// where `InstantiateMsg`, `ExecuteMsg`, and `QueryMsg` are contract defined
53/// types that implement `DeserializeOwned + JsonSchema`.
54#[proc_macro_attribute]
55pub fn entry_point(_attr: TokenStream, mut item: TokenStream) -> TokenStream {
56    let cloned = item.clone();
57    let function = parse_macro_input!(cloned as syn::ItemFn);
58    let name = function.sig.ident.to_string();
59    // The first argument is `deps`, the rest is region pointers
60    let args = function.sig.inputs.len() - 1;
61
62    // E.g. "ptr0: u32, ptr1: u32, ptr2: u32, "
63    let typed_ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{}: u32, ", acc, i));
64    // E.g. "ptr0, ptr1, ptr2, "
65    let ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{}, ", acc, i));
66
67    let new_code = format!(
68        r##"
69        #[cfg(target_arch = "wasm32")]
70        mod __wasm_export_{name} {{ // new module to avoid conflict of function name
71            #[no_mangle]
72            extern "C" fn {name}({typed_ptrs}) -> u32 {{
73                prov_cosmwasm_std::do_{name}(&super::{name}, {ptrs})
74            }}
75        }}
76    "##,
77        name = name,
78        typed_ptrs = typed_ptrs,
79        ptrs = ptrs
80    );
81    let entry = TokenStream::from_str(&new_code).unwrap();
82    item.extend(entry);
83    item
84}