11pub use wit_parser:: abi:: { AbiVariant , WasmSignature , WasmType } ;
22use wit_parser:: {
3- ElementInfo , Enum , Flags , FlagsRepr , Function , Handle , Int , Record , Resolve , Result_ , Results ,
3+ ElementInfo , Enum , Flags , FlagsRepr , Function , Handle , Int , Record , Resolve , Result_ ,
44 SizeAlign , Tuple , Type , TypeDefKind , TypeId , Variant ,
55} ;
66
@@ -505,7 +505,7 @@ def_instruction! {
505505 CallInterface {
506506 func: & ' a Function ,
507507 async_: bool ,
508- } : [ func. params. len( ) ] => [ if * async_ { 1 } else { func. results . len ( ) } ] ,
508+ } : [ func. params. len( ) ] => [ if * async_ { 1 } else { usize :: from ( func. result . is_some ( ) ) } ] ,
509509
510510 /// Returns `amt` values on the stack. This is always the last
511511 /// instruction.
@@ -573,7 +573,7 @@ def_instruction! {
573573 ///
574574 /// For example, this might include task management for the
575575 /// future/promise/task returned by the call made for `CallInterface`.
576- AsyncPostCallInterface { func: & ' a Function } : [ 1 ] => [ func. results . len ( ) + 1 ] ,
576+ AsyncPostCallInterface { func: & ' a Function } : [ 1 ] => [ usize :: from ( func. result . is_some ( ) ) + 1 ] ,
577577
578578 /// Call `task.return` for an async-lifted export once the task returned
579579 /// by `CallInterface` and managed by `AsyncPostCallInterface`
@@ -815,9 +815,9 @@ pub fn post_return(resolve: &Resolve, func: &Function, bindgen: &mut impl Bindge
815815/// This is used when the return value contains a memory allocation such as
816816/// a list or a string primarily.
817817pub fn guest_export_needs_post_return ( resolve : & Resolve , func : & Function ) -> bool {
818- func. results
819- . iter_types ( )
820- . any ( |t| needs_post_return ( resolve , t ) )
818+ func. result
819+ . map ( |t| needs_post_return ( resolve , & t ) )
820+ . unwrap_or ( false )
821821}
822822
823823fn needs_post_return ( resolve : & Resolve , ty : & Type ) -> bool {
@@ -977,7 +977,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
977977 let dealloc_size_align =
978978 if let Some ( ( params_size, params_align) ) = params_size_align {
979979 let ElementInfo { size, align } =
980- self . bindgen . sizes ( ) . record ( func. results . iter_types ( ) ) ;
980+ self . bindgen . sizes ( ) . record ( func. result . iter ( ) ) ;
981981 self . emit ( & Instruction :: AsyncMalloc {
982982 size : size. size_wasm32 ( ) ,
983983 align : align. align_wasm32 ( ) ,
@@ -995,7 +995,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
995995 Some ( ( size, align) )
996996 } else {
997997 if self . variant == AbiVariant :: GuestImport && sig. retptr {
998- let info = self . bindgen . sizes ( ) . params ( func. results . iter_types ( ) ) ;
998+ let info = self . bindgen . sizes ( ) . params ( & func. result ) ;
999999 let ptr = self
10001000 . bindgen
10011001 . return_pointer ( info. size . size_wasm32 ( ) , info. align . align_wasm32 ( ) ) ;
@@ -1015,7 +1015,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
10151015 // With no return pointer in use we can simply lift the
10161016 // result(s) of the function from the result of the core
10171017 // wasm function.
1018- for ty in func. results . iter_types ( ) {
1018+ if let Some ( ty ) = & func. result {
10191019 self . lift ( ty)
10201020 }
10211021 } else {
@@ -1042,9 +1042,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
10421042 }
10431043 } ;
10441044
1045- self . read_results_from_memory ( & func. results , ptr. clone ( ) , 0 ) ;
1045+ self . read_results_from_memory ( & func. result , ptr. clone ( ) , 0 ) ;
10461046 self . emit ( & Instruction :: Flush {
1047- amt : func. results . len ( ) ,
1047+ amt : usize :: from ( func. result . is_some ( ) ) ,
10481048 } ) ;
10491049
10501050 if let Some ( ( size, align) ) = dealloc_size_align {
@@ -1058,7 +1058,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
10581058
10591059 self . emit ( & Instruction :: Return {
10601060 func,
1061- amt : func. results . len ( ) ,
1061+ amt : usize :: from ( func. result . is_some ( ) ) ,
10621062 } ) ;
10631063 }
10641064 LiftLower :: LiftArgsLowerResults => {
@@ -1109,7 +1109,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11091109 self . emit ( & Instruction :: AsyncPostCallInterface { func } ) ;
11101110
11111111 let mut results = Vec :: new ( ) ;
1112- for ty in func. results . iter_types ( ) {
1112+ if let Some ( ty ) = & func. result {
11131113 self . resolve . push_flat ( ty, & mut results) ;
11141114 }
11151115 ( results. len ( ) > MAX_FLAT_PARAMS , Some ( results) )
@@ -1137,12 +1137,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11371137 if !lower_to_memory {
11381138 // With no return pointer in use we simply lower the
11391139 // result(s) and return that directly from the function.
1140- let results = self
1141- . stack
1142- . drain ( self . stack . len ( ) - func. results . len ( ) ..)
1143- . collect :: < Vec < _ > > ( ) ;
1144- for ( ty, result) in func. results . iter_types ( ) . zip ( results) {
1145- self . stack . push ( result) ;
1140+ if let Some ( ty) = & func. result {
11461141 self . lower ( ty) ;
11471142 }
11481143 } else {
@@ -1158,7 +1153,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11581153 nth : sig. params . len ( ) - 1 ,
11591154 } ) ;
11601155 let ptr = self . stack . pop ( ) . unwrap ( ) ;
1161- self . write_params_to_memory ( func. results . iter_types ( ) , ptr, 0 ) ;
1156+ self . write_params_to_memory ( & func. result , ptr, 0 ) ;
11621157 }
11631158
11641159 // For a guest import this is a function defined in
@@ -1167,11 +1162,11 @@ impl<'a, B: Bindgen> Generator<'a, B> {
11671162 // (statically) and then write the result into that
11681163 // memory, returning the pointer at the end.
11691164 AbiVariant :: GuestExport => {
1170- let info = self . bindgen . sizes ( ) . params ( func. results . iter_types ( ) ) ;
1165+ let info = self . bindgen . sizes ( ) . params ( & func. result ) ;
11711166 let ptr = self
11721167 . bindgen
11731168 . return_pointer ( info. size . size_wasm32 ( ) , info. align . align_wasm32 ( ) ) ;
1174- self . write_params_to_memory ( func. results . iter_types ( ) , ptr. clone ( ) , 0 ) ;
1169+ self . write_params_to_memory ( & func. result , ptr. clone ( ) , 0 ) ;
11751170 self . stack . push ( ptr) ;
11761171 }
11771172
@@ -1222,11 +1217,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
12221217
12231218 self . emit ( & Instruction :: GetArg { nth : 0 } ) ;
12241219 let addr = self . stack . pop ( ) . unwrap ( ) ;
1225- for ( offset, ty) in self
1226- . bindgen
1227- . sizes ( )
1228- . field_offsets ( func. results . iter_types ( ) )
1229- {
1220+ for ( offset, ty) in self . bindgen . sizes ( ) . field_offsets ( & func. result ) {
12301221 let offset = offset. size_wasm32 ( ) ;
12311222 let offset = i32:: try_from ( offset) . unwrap ( ) ;
12321223 self . deallocate ( ty, addr. clone ( ) , offset) ;
@@ -1779,7 +1770,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
17791770
17801771 fn write_params_to_memory < ' b > (
17811772 & mut self ,
1782- params : impl IntoIterator < Item = & ' b Type > + ExactSizeIterator ,
1773+ params : impl IntoIterator < Item = & ' b Type , IntoIter : ExactSizeIterator > ,
17831774 addr : B :: Operand ,
17841775 offset : i32 ,
17851776 ) {
@@ -1827,10 +1818,11 @@ impl<'a, B: Bindgen> Generator<'a, B> {
18271818
18281819 fn write_fields_to_memory < ' b > (
18291820 & mut self ,
1830- tys : impl IntoIterator < Item = & ' b Type > + ExactSizeIterator ,
1821+ tys : impl IntoIterator < Item = & ' b Type , IntoIter : ExactSizeIterator > ,
18311822 addr : B :: Operand ,
18321823 offset : i32 ,
18331824 ) {
1825+ let tys = tys. into_iter ( ) ;
18341826 let fields = self
18351827 . stack
18361828 . drain ( self . stack . len ( ) - tys. len ( ) ..)
@@ -1966,8 +1958,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
19661958 }
19671959 }
19681960
1969- fn read_results_from_memory ( & mut self , results : & Results , addr : B :: Operand , offset : i32 ) {
1970- self . read_fields_from_memory ( results . iter_types ( ) , addr, offset)
1961+ fn read_results_from_memory ( & mut self , result : & Option < Type > , addr : B :: Operand , offset : i32 ) {
1962+ self . read_fields_from_memory ( result , addr, offset)
19711963 }
19721964
19731965 fn read_variant_arms_from_memory < ' b > (
0 commit comments