Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use global paths in macro expansions #1060

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions near-sdk-macros/src/core_impl/abi/abi_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ pub fn embed() -> TokenStream2 {
let abi_path = env!("CARGO_NEAR_ABI_PATH");
quote! {
const _: () = {
const __CONTRACT_ABI: &'static [u8] = include_bytes!(#abi_path);
const __CONTRACT_ABI: &'static [u8] = ::std::include_bytes!(#abi_path);
#[no_mangle]
pub extern "C" fn __contract_abi() {
near_sdk::env::value_return(__CONTRACT_ABI);
::near_sdk::env::value_return(__CONTRACT_ABI);
}
};
}
Expand Down
216 changes: 109 additions & 107 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs

Large diffs are not rendered by default.

55 changes: 28 additions & 27 deletions near-sdk-macros/src/core_impl/code_generator/attr_sig_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ impl AttrSigInfo {
);
let attribute = match &self.input_serializer {
SerializerType::JSON => quote! {
#[derive(near_sdk::serde::Serialize)]
#[serde(crate = "near_sdk::serde")]
#[derive(::near_sdk::serde::Serialize)]
#[serde(crate = "::near_sdk::serde")]
},
SerializerType::Borsh => quote! {
#[derive(near_sdk::borsh::BorshSerialize)]
#[derive(::near_sdk::borsh::BorshSerialize)]
},
};
let mut fields = TokenStream2::new();
Expand Down Expand Up @@ -89,11 +89,11 @@ impl AttrSigInfo {
);
let attribute = match &self.input_serializer {
SerializerType::JSON => quote! {
#[derive(near_sdk::serde::Deserialize)]
#[serde(crate = "near_sdk::serde")]
#[derive(::near_sdk::serde::Deserialize)]
#[serde(crate = "::near_sdk::serde")]
},
SerializerType::Borsh => quote! {
#[derive(near_sdk::borsh::BorshDeserialize)]
#[derive(::near_sdk::borsh::BorshDeserialize)]
},
};
let mut fields = TokenStream2::new();
Expand Down Expand Up @@ -222,9 +222,9 @@ impl AttrSigInfo {
BindgenArgType::CallbackArg => {
let error_msg = format!("Callback computation {} was not successful", idx);
let read_data = quote! {
let data: Vec<u8> = match near_sdk::env::promise_result(#idx) {
near_sdk::PromiseResult::Successful(x) => x,
_ => near_sdk::env::panic_str(#error_msg)
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(#idx) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str(#error_msg)
};
};
let invocation = deserialize_data(serializer_ty);
Expand Down Expand Up @@ -255,19 +255,19 @@ impl AttrSigInfo {
// deserialization otherwise.
syn::Type::Tuple(type_tuple) if type_tuple.elems.is_empty() =>
quote! {
near_sdk::PromiseResult::Successful(data) if data.is_empty() =>
Ok(()),
near_sdk::PromiseResult::Successful(data) => Ok(#deserialize)
::near_sdk::PromiseResult::Successful(data) if data.is_empty() =>
::std::result::Result::Ok(()),
::near_sdk::PromiseResult::Successful(data) => ::std::result::Result::Ok(#deserialize)
},
_ =>
quote! {
near_sdk::PromiseResult::Successful(data) => Ok(#deserialize)
::near_sdk::PromiseResult::Successful(data) => ::std::result::Result::Ok(#deserialize)
}
};
let result = quote! {
match near_sdk::env::promise_result(#idx) {
match ::near_sdk::env::promise_result(#idx) {
#deserialization_branch,
near_sdk::PromiseResult::Failed => Err(near_sdk::PromiseError::Failed),
::near_sdk::PromiseResult::Failed => ::std::result::Result::Err(::near_sdk::PromiseError::Failed),
}
};
quote! {
Expand All @@ -290,27 +290,28 @@ impl AttrSigInfo {
let ArgInfo { mutability, ident, ty, .. } = arg;
let invocation = deserialize_data(&arg.serializer_ty);
quote! {
#acc
let #mutability #ident: #ty = (0..near_sdk::env::promise_results_count())
.map(|i| {
let data: Vec<u8> = match near_sdk::env::promise_result(i) {
near_sdk::PromiseResult::Successful(x) => x,
_ => near_sdk::env::panic_str(&format!("Callback computation {} was not successful", i)),
};
#invocation
}).collect();
}
#acc
let #mutability #ident: #ty = ::std::iter::Iterator::collect(::std::iter::Iterator::map(
0..::near_sdk::env::promise_results_count(),
|i| {
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(i) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str(&::std::format!("Callback computation {} was not successful", i)),
};
#invocation
}));
}
})
}
}

pub fn deserialize_data(ty: &SerializerType) -> TokenStream2 {
match ty {
SerializerType::JSON => quote! {
near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON")
::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON")
},
SerializerType::Borsh => quote! {
near_sdk::borsh::BorshDeserialize::try_from_slice(&data).expect("Failed to deserialize callback using Borsh")
::near_sdk::borsh::BorshDeserialize::try_from_slice(&data).expect("Failed to deserialize callback using Borsh")
},
}
}
96 changes: 48 additions & 48 deletions near-sdk-macros/src/core_impl/code_generator/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub(crate) fn generate_ext_structs(
let name = format_ident!("{}Ext", ident);
let mut ext_code = quote! {
/// API for calling this contract's functions in a subsequent execution.
pub fn ext(account_id: near_sdk::AccountId) -> #name {
pub fn ext(account_id: ::near_sdk::AccountId) -> #name {
#name {
account_id,
deposit: 0,
static_gas: near_sdk::Gas(0),
gas_weight: near_sdk::GasWeight::default(),
static_gas: ::near_sdk::Gas(0),
gas_weight: ::near_sdk::GasWeight::default(),
}
}
};
Expand All @@ -34,23 +34,23 @@ pub(crate) fn generate_ext_structs(
quote! {
#[must_use]
pub struct #name {
pub(crate) account_id: near_sdk::AccountId,
pub(crate) deposit: near_sdk::Balance,
pub(crate) static_gas: near_sdk::Gas,
pub(crate) gas_weight: near_sdk::GasWeight,
pub(crate) account_id: ::near_sdk::AccountId,
pub(crate) deposit: ::near_sdk::Balance,
pub(crate) static_gas: ::near_sdk::Gas,
pub(crate) gas_weight: ::near_sdk::GasWeight,
}

impl #name {
pub fn with_attached_deposit(mut self, amount: near_sdk::Balance) -> Self {
pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self {
self.deposit = amount;
self
}
pub fn with_static_gas(mut self, static_gas: near_sdk::Gas) -> Self {
pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self {
self.static_gas = static_gas;
self
}
pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self {
self.gas_weight = near_sdk::GasWeight(gas_weight);
self.gas_weight = ::near_sdk::GasWeight(gas_weight);
self
}
}
Expand Down Expand Up @@ -127,11 +127,11 @@ fn generate_ext_function(attr_signature_info: &AttrSigInfo) -> TokenStream2 {
let Signature { generics, .. } = original_sig;
quote! {
#new_non_bindgen_attrs
pub fn #ident #generics(self, #pat_type_list) -> near_sdk::Promise {
pub fn #ident #generics(self, #pat_type_list) -> ::near_sdk::Promise {
let __args = #serialize;
near_sdk::Promise::new(self.account_id)
::near_sdk::Promise::new(self.account_id)
.function_call_weight(
#ident_str.to_string(),
::std::string::String::from(#ident_str),
__args,
self.deposit,
self.static_gas,
Expand All @@ -157,33 +157,33 @@ mod tests {
let expected = quote!(
#[must_use]
pub struct TestExt {
pub(crate) account_id: near_sdk::AccountId,
pub(crate) deposit: near_sdk::Balance,
pub(crate) static_gas: near_sdk::Gas,
pub(crate) gas_weight: near_sdk::GasWeight,
pub(crate) account_id: ::near_sdk::AccountId,
pub(crate) deposit: ::near_sdk::Balance,
pub(crate) static_gas: ::near_sdk::Gas,
pub(crate) gas_weight: ::near_sdk::GasWeight,
}
impl TestExt {
pub fn with_attached_deposit(mut self, amount: near_sdk::Balance) -> Self {
pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self {
self.deposit = amount;
self
}
pub fn with_static_gas(mut self, static_gas: near_sdk::Gas) -> Self {
pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self {
self.static_gas = static_gas;
self
}
pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self {
self.gas_weight = near_sdk::GasWeight(gas_weight);
self.gas_weight = ::near_sdk::GasWeight(gas_weight);
self
}
}
impl Test {
/// API for calling this contract's functions in a subsequent execution.
pub fn ext(account_id: near_sdk::AccountId) -> TestExt {
pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt {
TestExt {
account_id,
deposit: 0,
static_gas: near_sdk::Gas(0),
gas_weight: near_sdk::GasWeight::default(),
static_gas: ::near_sdk::Gas(0),
gas_weight: ::near_sdk::GasWeight::default(),
}
}
}
Expand All @@ -198,32 +198,32 @@ mod tests {
let expected = quote!(
#[must_use]
pub struct TestExt {
pub(crate) account_id: near_sdk::AccountId,
pub(crate) deposit: near_sdk::Balance,
pub(crate) static_gas: near_sdk::Gas,
pub(crate) gas_weight: near_sdk::GasWeight,
pub(crate) account_id: ::near_sdk::AccountId,
pub(crate) deposit: ::near_sdk::Balance,
pub(crate) static_gas: ::near_sdk::Gas,
pub(crate) gas_weight: ::near_sdk::GasWeight,
}
impl TestExt {
pub fn with_attached_deposit(mut self, amount: near_sdk::Balance) -> Self {
pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self {
self.deposit = amount;
self
}
pub fn with_static_gas(mut self, static_gas: near_sdk::Gas) -> Self {
pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self {
self.static_gas = static_gas;
self
}
pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self {
self.gas_weight = near_sdk::GasWeight(gas_weight);
self.gas_weight = ::near_sdk::GasWeight(gas_weight);
self
}
}
/// API for calling this contract's functions in a subsequent execution.
pub fn ext(account_id: near_sdk::AccountId) -> TestExt {
pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt {
TestExt {
account_id,
deposit: 0,
static_gas: near_sdk::Gas(0),
gas_weight: near_sdk::GasWeight::default(),
static_gas: ::near_sdk::Gas(0),
gas_weight: ::near_sdk::GasWeight::default(),
}
}
);
Expand All @@ -247,10 +247,10 @@ mod tests {
// Note: only whitelisted non-bindgen attributes are forwarded.
let expected = quote! {
#[cfg(target_os = "linux")]
pub fn method (self,) -> near_sdk::Promise {
let __args = vec![];
near_sdk::Promise::new(self.account_id).function_call_weight(
"method".to_string(),
pub fn method (self,) -> ::near_sdk::Promise {
let __args = ::std::vec![];
::near_sdk::Promise::new(self.account_id).function_call_weight(
::std::string::String::from("method"),
__args,
self.deposit,
self.static_gas,
Expand All @@ -270,18 +270,18 @@ mod tests {
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let actual = generate_ext_function(&method_info.attr_signature_info);
let expected = quote!(
pub fn method(self, k: &String,) -> near_sdk::Promise {
let __args = {#[derive(near_sdk :: serde :: Serialize)]
#[serde(crate = "near_sdk::serde")]
pub fn method(self, k: &String,) -> ::near_sdk::Promise {
let __args = {#[derive(::near_sdk :: serde :: Serialize)]
#[serde(crate = "::near_sdk::serde")]
struct Input<'nearinput> {
k: &'nearinput String,
}
let __args = Input { k: &k, };
near_sdk::serde_json::to_vec(&__args)
::near_sdk::serde_json::to_vec(&__args)
.expect("Failed to serialize the cross contract args using JSON.")
};
near_sdk::Promise::new(self.account_id).function_call_weight(
"method".to_string(),
::near_sdk::Promise::new(self.account_id).function_call_weight(
::std::string::String::from("method"),
__args,
self.deposit,
self.static_gas,
Expand All @@ -301,19 +301,19 @@ mod tests {
let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap();
let actual = generate_ext_function(&method_info.attr_signature_info);
let expected = quote!(
pub fn borsh_test(self, a: String,) -> near_sdk::Promise {
pub fn borsh_test(self, a: String,) -> ::near_sdk::Promise {
let __args = {
#[derive(near_sdk :: borsh :: BorshSerialize)]
#[derive(::near_sdk :: borsh :: BorshSerialize)]
struct Input<'nearinput> {
a: &'nearinput String,
}
let __args = Input { a: &a, };
near_sdk::borsh::BorshSerialize::try_to_vec(&__args)
::near_sdk::borsh::BorshSerialize::try_to_vec(&__args)
.expect("Failed to serialize the cross contract args using Borsh.")
};
near_sdk::Promise::new(self.account_id)
::near_sdk::Promise::new(self.account_id)
.function_call_weight(
"borsh_test".to_string(),
::std::string::String::from("borsh_test"),
__args,
self.deposit,
self.static_gas,
Expand Down
Loading