Skip to content

Commit

Permalink
revert 22febc3
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Jul 31, 2024
1 parent 8405364 commit 8f60c9d
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 67 deletions.
133 changes: 66 additions & 67 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 @@ -209,78 +209,77 @@ impl AttrSigInfo {
/// Create code that deserializes arguments that were decorated with `#[callback*]`
pub fn callback_deserialization(&self) -> TokenStream2 {
self.args
.iter()
.filter(|arg| {
matches!(
arg.bindgen_ty,
BindgenArgType::CallbackArg | BindgenArgType::CallbackResultArg
)
})
.enumerate()
.fold(TokenStream2::new(), |acc, (idx, arg)| {
let idx = idx as u64;
let ArgInfo { mutability, ident, ty, bindgen_ty, serializer_ty, .. } = arg;
match &bindgen_ty {
BindgenArgType::CallbackArg => {
let read_data = quote! {
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(#idx) {
::near_sdk::PromiseResult::Successful(x) => x,

_ => ::near_sdk::env::panic_str(concat!("Callback computation ", stringify!(i), " was not successful")),

};
.iter()
.filter(|arg| {
matches!(
arg.bindgen_ty,
BindgenArgType::CallbackArg | BindgenArgType::CallbackResultArg
)
})
.enumerate()
.fold(TokenStream2::new(), |acc, (idx, arg)| {
let idx = idx as u64;
let ArgInfo { mutability, ident, ty, bindgen_ty, serializer_ty, .. } = arg;
match &bindgen_ty {
BindgenArgType::CallbackArg => {
let error_msg = format!("Callback computation {} was not successful", idx);
let read_data = quote! {
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);
quote! {
#acc
#read_data
let #mutability #ident: #ty = #invocation;
}
};
let invocation = deserialize_data(serializer_ty);
quote! {
#acc
#read_data
let #mutability #ident: #ty = #invocation;
}
BindgenArgType::CallbackResultArg => {
let ok_type = if let Some(ok_type) = utils::extract_ok_type(ty) {
ok_type
} else {
return syn::Error::new_spanned(ty, "Function parameters marked with \
#[callback_result] should have type Result<T, PromiseError>").into_compile_error()
};
let deserialize = deserialize_data(serializer_ty);
let deserialization_branch = match ok_type {
// The unit type in this context is a bit special because functions
// without an explicit return type do not serialize their response.
// But when someone tries to refer to their callback result with
// `#[callback_result]` they specify the callback type as
// `Result<(), PromiseError>` which cannot be correctly deserialized from
// an empty byte array.
//
// So instead of going through serde, we consider deserialization to be
// successful if the byte array is empty or try the normal
// deserialization otherwise.
syn::Type::Tuple(type_tuple) if type_tuple.elems.is_empty() =>
quote! {
::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) => ::std::result::Result::Ok(#deserialize)
}
};
let result = quote! {
match ::near_sdk::env::promise_result(#idx) {
#deserialization_branch,
::near_sdk::PromiseResult::Failed => ::std::result::Result::Err(::near_sdk::PromiseError::Failed),
}
BindgenArgType::CallbackResultArg => {
let ok_type = if let Some(ok_type) = utils::extract_ok_type(ty) {
ok_type
} else {
return syn::Error::new_spanned(ty, "Function parameters marked with \
#[callback_result] should have type Result<T, PromiseError>").into_compile_error()
};
let deserialize = deserialize_data(serializer_ty);
let deserialization_branch = match ok_type {
// The unit type in this context is a bit special because functions
// without an explicit return type do not serialize their response.
// But when someone tries to refer to their callback result with
// `#[callback_result]` they specify the callback type as
// `Result<(), PromiseError>` which cannot be correctly deserialized from
// an empty byte array.
//
// So instead of going through serde, we consider deserialization to be
// successful if the byte array is empty or try the normal
// deserialization otherwise.
syn::Type::Tuple(type_tuple) if type_tuple.elems.is_empty() =>
quote! {
::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) => ::std::result::Result::Ok(#deserialize)
}
};
quote! {
#acc
let #mutability #ident: #ty = #result;
};
let result = quote! {
match ::near_sdk::env::promise_result(#idx) {
#deserialization_branch,
::near_sdk::PromiseResult::Failed => ::std::result::Result::Err(::near_sdk::PromiseError::Failed),
}
};
quote! {
#acc
let #mutability #ident: #ty = #result;
}
_ => unreachable!()
}
})
_ => unreachable!()
}
})
}

/// Create code that deserializes arguments that were decorated with `#[callback_vec]`.
Expand All @@ -299,7 +298,7 @@ impl AttrSigInfo {
|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(concat!("Callback computation ", stringify!(i), " was not successful")),
_ => ::near_sdk::env::panic_str(&::std::format!("Callback computation {} was not successful", i)),
};
#invocation
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
assertion_line: 148
expression: pretty_print_syn_str(&actual).unwrap()
---
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
::near_sdk::env::setup_panic_hook();
if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id()
{
::near_sdk::env::panic_str("Method method is private");
}
#[derive(::near_sdk::serde::Deserialize)]
#[serde(crate = "::near_sdk::serde")]
struct Input {
y: ::std::string::String,
}
let Input { y }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
::near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => ::near_sdk::env::panic_str("Expected input since method has arguments."),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(0u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
};
let mut x: u64 = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(1u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"),
};
let z: ::std::vec::Vec<u8> = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut x, y, z);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
assertion_line: 253
expression: pretty_print_syn_str(&actual).unwrap()
---
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
::near_sdk::env::setup_panic_hook();
if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id()
{
::near_sdk::env::panic_str("Method method is private");
}
#[derive(::near_sdk::borsh::BorshDeserialize)]
#[borsh(crate = "::near_sdk::borsh")]
struct Input {
y: ::std::string::String,
}
let Input { y }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
::near_sdk::env::panic_str("Failed to deserialize input from Borsh.")
}
}
}
None => ::near_sdk::env::panic_str("Expected input since method has arguments."),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(0u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
};
let mut x: u64 = match ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => {
::near_sdk::env::panic_str("Failed to deserialize callback using Borsh")
}
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(1u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"),
};
let z: ::std::vec::Vec<u8> = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut x, y, z);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
assertion_line: 159
expression: pretty_print_syn_str(&actual).unwrap()
---
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
::near_sdk::env::setup_panic_hook();
if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id()
{
::near_sdk::env::panic_str("Method method is private");
}
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(0u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"),
};
let mut x: u64 = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let data: ::std::vec::Vec<u8> = match ::near_sdk::env::promise_result(1u64) {
::near_sdk::PromiseResult::Successful(x) => x,
_ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"),
};
let y: ::std::string::String = match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => ::near_sdk::env::panic_str("Failed to deserialize callback using JSON"),
};
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, &mut x, y);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
assertion_line: 181
expression: pretty_print_syn_str(&actual).unwrap()
---
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
::near_sdk::env::setup_panic_hook();
if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id()
{
::near_sdk::env::panic_str("Method method is private");
}
#[derive(::near_sdk::serde::Deserialize)]
#[serde(crate = "::near_sdk::serde")]
struct Input {
y: String,
}
let Input { y }: Input = match ::near_sdk::env::input() {
Some(input) => {
match ::near_sdk::serde_json::from_slice(&input) {
Ok(deserialized) => deserialized,
Err(_) => {
::near_sdk::env::panic_str("Failed to deserialize input from JSON.")
}
}
}
None => ::near_sdk::env::panic_str("Expected input since method has arguments."),
};
let x: Vec<String> = ::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
),
)
}
};
match ::near_sdk::serde_json::from_slice(&data) {
Ok(deserialized) => deserialized,
Err(_) => {
::near_sdk::env::panic_str(
"Failed to deserialize callback using JSON",
)
}
}
},
),
);
let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default();
Hello::method(&contract, x, y);
}

0 comments on commit 8f60c9d

Please sign in to comment.