From d2cd1f7efe5358748f6e8e8b82ea75702a894aa2 Mon Sep 17 00:00:00 2001 From: cheme Date: Tue, 14 Aug 2018 17:37:34 +0200 Subject: [PATCH] alternate multiresult implementation --- derive/src/lib.rs | 35 ++++++++++++++++++++-- src/eth/common.rs | 75 ----------------------------------------------- 2 files changed, 32 insertions(+), 78 deletions(-) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 438d7d0..3677c54 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -145,6 +145,12 @@ fn generate_eth_client(client_name: &str, intf: &items::Interface) -> quote::Tok syn::FunctionRetTy::Default => quote!{ let mut result = Vec::new(); }, + syn::FunctionRetTy::Ty(syn::Ty::Tup(ref tys)) => { + let result_len = tys.len() * 32; + quote!{ + let mut result = [0u8; #result_len]; + } + }, syn::FunctionRetTy::Ty(_) => quote!{ let mut result = [0u8; 32]; }, @@ -152,6 +158,14 @@ fn generate_eth_client(client_name: &str, intf: &items::Interface) -> quote::Tok let result_pop = match signature.method_sig.decl.output { syn::FunctionRetTy::Default => None, + syn::FunctionRetTy::Ty(syn::Ty::Tup(ref result_types)) => { + Some(quote!{ + let mut stream = pwasm_abi::eth::Stream::new(&result); + ( + #( stream.pop::<#result_types>().expect("failed decode call output") ),* + ) + }) + }, syn::FunctionRetTy::Ty(_) => Some(quote!{ let mut stream = pwasm_abi::eth::Stream::new(&result); stream.pop().expect("failed decode call output") @@ -262,8 +276,23 @@ fn generate_eth_endpoint(endpoint_name: &str, intf: &items::Interface) -> quote: let ident = &signature.name; let arg_types = signature.arguments.iter().map(|&(_, ref ty)| quote! { #ty }); let check_value_if_payable = if signature.is_payable { quote! {} } else { quote! {#check_value_code} }; - if !signature.return_types.is_empty() { - let return_count_literal = syn::Lit::Int(signature.return_types.len() as u64, syn::IntTy::Usize); + let return_types_len = signature.return_types.len(); + if return_types_len > 1 { + let return_types_it : Vec<_> = (0..return_types_len).into_iter() + .map(|i|syn::Ident::new(i.to_string())).collect(); + Some(quote! { + #hash_literal => { + #check_value_if_payable + let mut stream = pwasm_abi::eth::Stream::new(method_payload); + let result = inner.#ident( + #(stream.pop::<#arg_types>().expect("argument decoding failed")),* + ); + let mut sink = pwasm_abi::eth::Sink::new(#return_types_len); + #( sink.push(result.#return_types_it); )* + sink.finalize_panicking() + } + }) + } else if return_types_len == 1 { Some(quote! { #hash_literal => { #check_value_if_payable @@ -271,7 +300,7 @@ fn generate_eth_endpoint(endpoint_name: &str, intf: &items::Interface) -> quote: let result = inner.#ident( #(stream.pop::<#arg_types>().expect("argument decoding failed")),* ); - let mut sink = pwasm_abi::eth::Sink::new(#return_count_literal); + let mut sink = pwasm_abi::eth::Sink::new(1); sink.push(result); sink.finalize_panicking() } diff --git a/src/eth/common.rs b/src/eth/common.rs index 8665398..0ccb11b 100644 --- a/src/eth/common.rs +++ b/src/eth/common.rs @@ -258,81 +258,6 @@ macro_rules! abi_type_fixed_impl { } } -impl AbiType for (T1, T2) { - fn decode(_stream: &mut Stream) -> Result { - panic!("Tuples allow only encoding, not decoding (for supporting multiple return types)") - } - - fn encode(self, sink: &mut Sink) { - sink.push(self.0); - sink.push(self.1); - } - - const IS_FIXED: bool = true; -} - -impl AbiType for (T1, T2, T3) { - fn decode(_stream: &mut Stream) -> Result { - panic!("Tuples allow only encoding, not decoding (for supporting multiple return types)") - } - - fn encode(self, sink: &mut Sink) { - sink.push(self.0); - sink.push(self.1); - sink.push(self.2); - } - - const IS_FIXED: bool = true; -} - -impl AbiType for (T1, T2, T3, T4) { - fn decode(_stream: &mut Stream) -> Result { - panic!("Tuples allow only encoding, not decoding (for supporting multiple return types)") - } - - fn encode(self, sink: &mut Sink) { - sink.push(self.0); - sink.push(self.1); - sink.push(self.2); - sink.push(self.3); - } - - const IS_FIXED: bool = true; -} - -impl AbiType for (T1, T2, T3, T4, T5) { - fn decode(_stream: &mut Stream) -> Result { - panic!("Tuples allow only encoding, not decoding (for supporting multiple return types)") - } - - fn encode(self, sink: &mut Sink) { - sink.push(self.0); - sink.push(self.1); - sink.push(self.2); - sink.push(self.3); - sink.push(self.4); - } - - const IS_FIXED: bool = true; -} - -impl AbiType for (T1, T2, T3, T4, T5, T6) { - fn decode(_stream: &mut Stream) -> Result { - panic!("Tuples allow only encoding, not decoding (for supporting multiple return types)") - } - - fn encode(self, sink: &mut Sink) { - sink.push(self.0); - sink.push(self.1); - sink.push(self.2); - sink.push(self.3); - sink.push(self.4); - sink.push(self.5); - } - - const IS_FIXED: bool = true; -} - abi_type_fixed_impl!(1); abi_type_fixed_impl!(2); abi_type_fixed_impl!(3);