1
1
pub use wit_parser:: abi:: { AbiVariant , WasmSignature , WasmType } ;
2
2
use 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_ ,
4
4
SizeAlign , Tuple , Type , TypeDefKind , TypeId , Variant ,
5
5
} ;
6
6
@@ -505,7 +505,7 @@ def_instruction! {
505
505
CallInterface {
506
506
func: & ' a Function ,
507
507
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 ( ) ) } ] ,
509
509
510
510
/// Returns `amt` values on the stack. This is always the last
511
511
/// instruction.
@@ -573,7 +573,7 @@ def_instruction! {
573
573
///
574
574
/// For example, this might include task management for the
575
575
/// 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 ] ,
577
577
578
578
/// Call `task.return` for an async-lifted export once the task returned
579
579
/// by `CallInterface` and managed by `AsyncPostCallInterface`
@@ -815,9 +815,9 @@ pub fn post_return(resolve: &Resolve, func: &Function, bindgen: &mut impl Bindge
815
815
/// This is used when the return value contains a memory allocation such as
816
816
/// a list or a string primarily.
817
817
pub 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 )
821
821
}
822
822
823
823
fn needs_post_return ( resolve : & Resolve , ty : & Type ) -> bool {
@@ -977,7 +977,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
977
977
let dealloc_size_align =
978
978
if let Some ( ( params_size, params_align) ) = params_size_align {
979
979
let ElementInfo { size, align } =
980
- self . bindgen . sizes ( ) . record ( func. results . iter_types ( ) ) ;
980
+ self . bindgen . sizes ( ) . record ( func. result . iter ( ) ) ;
981
981
self . emit ( & Instruction :: AsyncMalloc {
982
982
size : size. size_wasm32 ( ) ,
983
983
align : align. align_wasm32 ( ) ,
@@ -995,7 +995,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
995
995
Some ( ( size, align) )
996
996
} else {
997
997
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 ) ;
999
999
let ptr = self
1000
1000
. bindgen
1001
1001
. return_pointer ( info. size . size_wasm32 ( ) , info. align . align_wasm32 ( ) ) ;
@@ -1015,7 +1015,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1015
1015
// With no return pointer in use we can simply lift the
1016
1016
// result(s) of the function from the result of the core
1017
1017
// wasm function.
1018
- for ty in func. results . iter_types ( ) {
1018
+ if let Some ( ty ) = & func. result {
1019
1019
self . lift ( ty)
1020
1020
}
1021
1021
} else {
@@ -1042,9 +1042,9 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1042
1042
}
1043
1043
} ;
1044
1044
1045
- self . read_results_from_memory ( & func. results , ptr. clone ( ) , 0 ) ;
1045
+ self . read_results_from_memory ( & func. result , ptr. clone ( ) , 0 ) ;
1046
1046
self . emit ( & Instruction :: Flush {
1047
- amt : func. results . len ( ) ,
1047
+ amt : usize :: from ( func. result . is_some ( ) ) ,
1048
1048
} ) ;
1049
1049
1050
1050
if let Some ( ( size, align) ) = dealloc_size_align {
@@ -1058,7 +1058,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1058
1058
1059
1059
self . emit ( & Instruction :: Return {
1060
1060
func,
1061
- amt : func. results . len ( ) ,
1061
+ amt : usize :: from ( func. result . is_some ( ) ) ,
1062
1062
} ) ;
1063
1063
}
1064
1064
LiftLower :: LiftArgsLowerResults => {
@@ -1109,7 +1109,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1109
1109
self . emit ( & Instruction :: AsyncPostCallInterface { func } ) ;
1110
1110
1111
1111
let mut results = Vec :: new ( ) ;
1112
- for ty in func. results . iter_types ( ) {
1112
+ if let Some ( ty ) = & func. result {
1113
1113
self . resolve . push_flat ( ty, & mut results) ;
1114
1114
}
1115
1115
( results. len ( ) > MAX_FLAT_PARAMS , Some ( results) )
@@ -1137,12 +1137,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1137
1137
if !lower_to_memory {
1138
1138
// With no return pointer in use we simply lower the
1139
1139
// 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 {
1146
1141
self . lower ( ty) ;
1147
1142
}
1148
1143
} else {
@@ -1158,7 +1153,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1158
1153
nth : sig. params . len ( ) - 1 ,
1159
1154
} ) ;
1160
1155
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 ) ;
1162
1157
}
1163
1158
1164
1159
// For a guest import this is a function defined in
@@ -1167,11 +1162,11 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1167
1162
// (statically) and then write the result into that
1168
1163
// memory, returning the pointer at the end.
1169
1164
AbiVariant :: GuestExport => {
1170
- let info = self . bindgen . sizes ( ) . params ( func. results . iter_types ( ) ) ;
1165
+ let info = self . bindgen . sizes ( ) . params ( & func. result ) ;
1171
1166
let ptr = self
1172
1167
. bindgen
1173
1168
. 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 ) ;
1175
1170
self . stack . push ( ptr) ;
1176
1171
}
1177
1172
@@ -1222,11 +1217,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1222
1217
1223
1218
self . emit ( & Instruction :: GetArg { nth : 0 } ) ;
1224
1219
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 ) {
1230
1221
let offset = offset. size_wasm32 ( ) ;
1231
1222
let offset = i32:: try_from ( offset) . unwrap ( ) ;
1232
1223
self . deallocate ( ty, addr. clone ( ) , offset) ;
@@ -1779,7 +1770,7 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1779
1770
1780
1771
fn write_params_to_memory < ' b > (
1781
1772
& mut self ,
1782
- params : impl IntoIterator < Item = & ' b Type > + ExactSizeIterator ,
1773
+ params : impl IntoIterator < Item = & ' b Type , IntoIter : ExactSizeIterator > ,
1783
1774
addr : B :: Operand ,
1784
1775
offset : i32 ,
1785
1776
) {
@@ -1827,10 +1818,11 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1827
1818
1828
1819
fn write_fields_to_memory < ' b > (
1829
1820
& mut self ,
1830
- tys : impl IntoIterator < Item = & ' b Type > + ExactSizeIterator ,
1821
+ tys : impl IntoIterator < Item = & ' b Type , IntoIter : ExactSizeIterator > ,
1831
1822
addr : B :: Operand ,
1832
1823
offset : i32 ,
1833
1824
) {
1825
+ let tys = tys. into_iter ( ) ;
1834
1826
let fields = self
1835
1827
. stack
1836
1828
. drain ( self . stack . len ( ) - tys. len ( ) ..)
@@ -1966,8 +1958,8 @@ impl<'a, B: Bindgen> Generator<'a, B> {
1966
1958
}
1967
1959
}
1968
1960
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)
1971
1963
}
1972
1964
1973
1965
fn read_variant_arms_from_memory < ' b > (
0 commit comments