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 ,
@@ -518,6 +520,9 @@ impl JsBindgen<'_> {
518520 resource_name,
519521 ) ;
520522 }
523+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
524+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
525+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
521526 } ;
522527 }
523528 }
@@ -607,10 +612,14 @@ impl JsBindgen<'_> {
607612 BTreeMap :: < _ , Vec < _ > > :: new ( ) ,
608613 |mut map, ( name, func) | {
609614 map. entry ( match & func. kind {
610- FunctionKind :: Freestanding => None ,
615+ FunctionKind :: Freestanding | FunctionKind :: AsyncFreestanding => {
616+ None
617+ }
611618 FunctionKind :: Method ( ty)
612619 | FunctionKind :: Static ( ty)
613- | FunctionKind :: Constructor ( ty) => Some ( * ty) ,
620+ | FunctionKind :: Constructor ( ty)
621+ | FunctionKind :: AsyncMethod ( ty)
622+ | FunctionKind :: AsyncStatic ( ty) => Some ( * ty) ,
614623 } )
615624 . or_default ( )
616625 . push ( ( name. as_str ( ) , func) ) ;
@@ -728,6 +737,9 @@ impl JsBindgen<'_> {
728737 Resource :: Constructor ( self . resolve . types [ * ty] . name . clone ( ) . unwrap ( ) ) ,
729738 )
730739 }
740+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
741+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
742+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
731743 } ;
732744
733745 // imports are canonicalized as exports because
@@ -783,8 +795,8 @@ impl JsBindgen<'_> {
783795 for ( _, ty) in func. params . iter ( ) {
784796 self . iter_resources ( ty, & mut resource_map) ;
785797 }
786- for ty in func. results . iter_types ( ) {
787- self . iter_resources ( ty, & mut resource_map) ;
798+ if let Some ( ty ) = func. result {
799+ self . iter_resources ( & ty, & mut resource_map) ;
788800 }
789801 resource_map
790802 }
@@ -893,23 +905,27 @@ impl JsBindgen<'_> {
893905 }
894906 }
895907
908+ let err = if get_thrown_type ( self . resolve , func. result )
909+ . is_some_and ( |( _, err_ty) | err_ty. is_some ( ) )
910+ {
911+ match abi {
912+ AbiVariant :: GuestExport => ErrHandling :: ThrowResultErr ,
913+ AbiVariant :: GuestImport => ErrHandling :: ResultCatchHandler ,
914+ AbiVariant :: GuestImportAsync => todo ! ( ) ,
915+ AbiVariant :: GuestExportAsync => todo ! ( ) ,
916+ AbiVariant :: GuestExportAsyncStackful => todo ! ( ) ,
917+ }
918+ } else {
919+ ErrHandling :: None
920+ } ;
921+
896922 let mut f = FunctionBindgen {
897923 is_async : false ,
898924 tracing_prefix : None ,
899925 intrinsics : & mut self . all_intrinsics ,
900926 valid_lifting_optimization : true ,
901927 sizes : & self . sizes ,
902- err : if func. results . throws ( self . resolve ) . is_some ( ) {
903- match abi {
904- AbiVariant :: GuestExport => ErrHandling :: ThrowResultErr ,
905- AbiVariant :: GuestImport => ErrHandling :: ResultCatchHandler ,
906- AbiVariant :: GuestImportAsync => todo ! ( ) ,
907- AbiVariant :: GuestExportAsync => todo ! ( ) ,
908- AbiVariant :: GuestExportAsyncStackful => todo ! ( ) ,
909- }
910- } else {
911- ErrHandling :: None
912- } ,
928+ err,
913929 block_storage : Vec :: new ( ) ,
914930 blocks : Vec :: new ( ) ,
915931 callee,
@@ -973,6 +989,9 @@ impl JsBindgen<'_> {
973989 Resource :: Constructor ( self . resolve . types [ * ty] . name . clone ( ) . unwrap ( ) ) ,
974990 format ! ( "new {callee}" ) ,
975991 ) ,
992+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
993+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
994+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
976995 } ;
977996
978997 let binding_name = format ! (
@@ -1017,8 +1036,8 @@ impl JsBindgen<'_> {
10171036 CoreFn {
10181037 retsize : if sig. retptr {
10191038 let mut retsize: u32 = 0 ;
1020- for ret_ty in func. results . iter_types ( ) {
1021- retsize += self . sizes . size ( ret_ty) . size_wasm32 ( ) as u32 ;
1039+ if let Some ( ret_ty) = func. result {
1040+ retsize += self . sizes . size ( & ret_ty) . size_wasm32 ( ) as u32 ;
10221041 }
10231042 retsize
10241043 } else {
@@ -1327,3 +1346,20 @@ fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
13271346 None => format ! ( "{func_name}" ) ,
13281347 }
13291348}
1349+
1350+ /// Utility function for deducing whether a type can throw
1351+ pub fn get_thrown_type < ' a > (
1352+ resolve : & ' a Resolve ,
1353+ return_type : Option < Type > ,
1354+ ) -> Option < ( Option < & ' a Type > , Option < & ' a Type > ) > {
1355+ match return_type {
1356+ None => None ,
1357+ Some ( ty) => match ty {
1358+ Type :: Id ( id) => match & resolve. types [ id] . kind {
1359+ TypeDefKind :: Result ( r) => Some ( ( r. ok . as_ref ( ) , r. err . as_ref ( ) ) ) ,
1360+ _ => None ,
1361+ } ,
1362+ _ => None ,
1363+ } ,
1364+ }
1365+ }
0 commit comments