1- use crate :: { uwrite, uwriteln} ;
2- use anyhow:: Result ;
1+ use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
2+ use std:: fmt:: Write ;
3+
4+ use anyhow:: { bail, Result } ;
35use heck:: * ;
46use js_component_bindgen:: function_bindgen:: {
57 ErrHandling , FunctionBindgen , ResourceData , ResourceMap , ResourceTable ,
68} ;
79use js_component_bindgen:: intrinsics:: { render_intrinsics, Intrinsic } ;
810use js_component_bindgen:: names:: LocalNames ;
911use js_component_bindgen:: source:: Source ;
10- use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
11- use std:: fmt:: Write ;
1212use wit_bindgen_core:: abi:: { self , LiftLower } ;
1313use wit_bindgen_core:: wit_parser:: Resolve ;
1414use wit_bindgen_core:: wit_parser:: {
@@ -19,6 +19,8 @@ use wit_component::StringEncoding;
1919use wit_parser:: abi:: WasmType ;
2020use wit_parser:: abi:: { AbiVariant , WasmSignature } ;
2121
22+ use crate :: { uwrite, uwriteln, Features } ;
23+
2224#[ derive( Debug ) ]
2325pub enum Resource {
2426 None ,
@@ -521,6 +523,9 @@ impl JsBindgen<'_> {
521523 resource_name,
522524 ) ;
523525 }
526+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
527+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
528+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
524529 } ;
525530 }
526531 }
@@ -610,10 +615,14 @@ impl JsBindgen<'_> {
610615 BTreeMap :: < _ , Vec < _ > > :: new ( ) ,
611616 |mut map, ( name, func) | {
612617 map. entry ( match & func. kind {
613- FunctionKind :: Freestanding => None ,
618+ FunctionKind :: Freestanding | FunctionKind :: AsyncFreestanding => {
619+ None
620+ }
614621 FunctionKind :: Method ( ty)
615622 | FunctionKind :: Static ( ty)
616- | FunctionKind :: Constructor ( ty) => Some ( * ty) ,
623+ | FunctionKind :: Constructor ( ty)
624+ | FunctionKind :: AsyncMethod ( ty)
625+ | FunctionKind :: AsyncStatic ( ty) => Some ( * ty) ,
617626 } )
618627 . or_default ( )
619628 . push ( ( name. as_str ( ) , func) ) ;
@@ -731,6 +740,9 @@ impl JsBindgen<'_> {
731740 Resource :: Constructor ( self . resolve . types [ * ty] . name . clone ( ) . unwrap ( ) ) ,
732741 )
733742 }
743+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
744+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
745+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
734746 } ;
735747
736748 // imports are canonicalized as exports because
@@ -786,8 +798,8 @@ impl JsBindgen<'_> {
786798 for ( _, ty) in func. params . iter ( ) {
787799 self . iter_resources ( ty, & mut resource_map) ;
788800 }
789- for ty in func. results . iter_types ( ) {
790- self . iter_resources ( ty, & mut resource_map) ;
801+ if let Some ( ty ) = func. result {
802+ self . iter_resources ( & ty, & mut resource_map) ;
791803 }
792804 resource_map
793805 }
@@ -896,23 +908,27 @@ impl JsBindgen<'_> {
896908 }
897909 }
898910
911+ let err = if get_thrown_type ( self . resolve , func. result )
912+ . is_some_and ( |( _, err_ty) | err_ty. is_some ( ) )
913+ {
914+ match abi {
915+ AbiVariant :: GuestExport => ErrHandling :: ThrowResultErr ,
916+ AbiVariant :: GuestImport => ErrHandling :: ResultCatchHandler ,
917+ AbiVariant :: GuestImportAsync => todo ! ( ) ,
918+ AbiVariant :: GuestExportAsync => todo ! ( ) ,
919+ AbiVariant :: GuestExportAsyncStackful => todo ! ( ) ,
920+ }
921+ } else {
922+ ErrHandling :: None
923+ } ;
924+
899925 let mut f = FunctionBindgen {
900926 is_async : false ,
901927 tracing_prefix : None ,
902928 intrinsics : & mut self . all_intrinsics ,
903929 valid_lifting_optimization : true ,
904930 sizes : & self . sizes ,
905- err : if func. results . throws ( self . resolve ) . is_some ( ) {
906- match abi {
907- AbiVariant :: GuestExport => ErrHandling :: ThrowResultErr ,
908- AbiVariant :: GuestImport => ErrHandling :: ResultCatchHandler ,
909- AbiVariant :: GuestImportAsync => todo ! ( ) ,
910- AbiVariant :: GuestExportAsync => todo ! ( ) ,
911- AbiVariant :: GuestExportAsyncStackful => todo ! ( ) ,
912- }
913- } else {
914- ErrHandling :: None
915- } ,
931+ err,
916932 block_storage : Vec :: new ( ) ,
917933 blocks : Vec :: new ( ) ,
918934 callee,
@@ -976,6 +992,9 @@ impl JsBindgen<'_> {
976992 Resource :: Constructor ( self . resolve . types [ * ty] . name . clone ( ) . unwrap ( ) ) ,
977993 format ! ( "new {callee}" ) ,
978994 ) ,
995+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
996+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
997+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
979998 } ;
980999
9811000 let binding_name = format ! (
@@ -1020,8 +1039,8 @@ impl JsBindgen<'_> {
10201039 CoreFn {
10211040 retsize : if sig. retptr {
10221041 let mut retsize: u32 = 0 ;
1023- for ret_ty in func. results . iter_types ( ) {
1024- retsize += self . sizes . size ( ret_ty) . size_wasm32 ( ) as u32 ;
1042+ if let Some ( ret_ty) = func. result {
1043+ retsize += self . sizes . size ( & ret_ty) . size_wasm32 ( ) as u32 ;
10251044 }
10261045 retsize
10271046 } else {
@@ -1330,3 +1349,20 @@ fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
13301349 None => format ! ( "{func_name}" ) ,
13311350 }
13321351}
1352+
1353+ /// Utility function for deducing whether a type can throw
1354+ pub fn get_thrown_type < ' a > (
1355+ resolve : & ' a Resolve ,
1356+ return_type : Option < Type > ,
1357+ ) -> Option < ( Option < & ' a Type > , Option < & ' a Type > ) > {
1358+ match return_type {
1359+ None => None ,
1360+ Some ( ty) => match ty {
1361+ Type :: Id ( id) => match & resolve. types [ id] . kind {
1362+ TypeDefKind :: Result ( r) => Some ( ( r. ok . as_ref ( ) , r. err . as_ref ( ) ) ) ,
1363+ _ => None ,
1364+ } ,
1365+ _ => None ,
1366+ } ,
1367+ }
1368+ }
0 commit comments