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 } ;
3
5
use heck:: * ;
4
6
use js_component_bindgen:: function_bindgen:: {
5
7
ErrHandling , FunctionBindgen , ResourceData , ResourceMap , ResourceTable ,
6
8
} ;
7
9
use js_component_bindgen:: intrinsics:: { render_intrinsics, Intrinsic } ;
8
10
use js_component_bindgen:: names:: LocalNames ;
9
11
use js_component_bindgen:: source:: Source ;
10
- use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
11
- use std:: fmt:: Write ;
12
12
use wit_bindgen_core:: abi:: { self , LiftLower } ;
13
13
use wit_bindgen_core:: wit_parser:: Resolve ;
14
14
use wit_bindgen_core:: wit_parser:: {
@@ -19,6 +19,8 @@ use wit_component::StringEncoding;
19
19
use wit_parser:: abi:: WasmType ;
20
20
use wit_parser:: abi:: { AbiVariant , WasmSignature } ;
21
21
22
+ use crate :: { uwrite, uwriteln, Features } ;
23
+
22
24
#[ derive( Debug ) ]
23
25
pub enum Resource {
24
26
None ,
@@ -521,6 +523,9 @@ impl JsBindgen<'_> {
521
523
resource_name,
522
524
) ;
523
525
}
526
+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
527
+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
528
+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
524
529
} ;
525
530
}
526
531
}
@@ -610,10 +615,14 @@ impl JsBindgen<'_> {
610
615
BTreeMap :: < _ , Vec < _ > > :: new ( ) ,
611
616
|mut map, ( name, func) | {
612
617
map. entry ( match & func. kind {
613
- FunctionKind :: Freestanding => None ,
618
+ FunctionKind :: Freestanding | FunctionKind :: AsyncFreestanding => {
619
+ None
620
+ }
614
621
FunctionKind :: Method ( ty)
615
622
| FunctionKind :: Static ( ty)
616
- | FunctionKind :: Constructor ( ty) => Some ( * ty) ,
623
+ | FunctionKind :: Constructor ( ty)
624
+ | FunctionKind :: AsyncMethod ( ty)
625
+ | FunctionKind :: AsyncStatic ( ty) => Some ( * ty) ,
617
626
} )
618
627
. or_default ( )
619
628
. push ( ( name. as_str ( ) , func) ) ;
@@ -731,6 +740,9 @@ impl JsBindgen<'_> {
731
740
Resource :: Constructor ( self . resolve . types [ * ty] . name . clone ( ) . unwrap ( ) ) ,
732
741
)
733
742
}
743
+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
744
+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
745
+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
734
746
} ;
735
747
736
748
// imports are canonicalized as exports because
@@ -786,8 +798,8 @@ impl JsBindgen<'_> {
786
798
for ( _, ty) in func. params . iter ( ) {
787
799
self . iter_resources ( ty, & mut resource_map) ;
788
800
}
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) ;
791
803
}
792
804
resource_map
793
805
}
@@ -896,23 +908,27 @@ impl JsBindgen<'_> {
896
908
}
897
909
}
898
910
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
+
899
925
let mut f = FunctionBindgen {
900
926
is_async : false ,
901
927
tracing_prefix : None ,
902
928
intrinsics : & mut self . all_intrinsics ,
903
929
valid_lifting_optimization : true ,
904
930
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,
916
932
block_storage : Vec :: new ( ) ,
917
933
blocks : Vec :: new ( ) ,
918
934
callee,
@@ -976,6 +992,9 @@ impl JsBindgen<'_> {
976
992
Resource :: Constructor ( self . resolve . types [ * ty] . name . clone ( ) . unwrap ( ) ) ,
977
993
format ! ( "new {callee}" ) ,
978
994
) ,
995
+ FunctionKind :: AsyncFreestanding => todo ! ( ) ,
996
+ FunctionKind :: AsyncMethod ( _id) => todo ! ( ) ,
997
+ FunctionKind :: AsyncStatic ( _id) => todo ! ( ) ,
979
998
} ;
980
999
981
1000
let binding_name = format ! (
@@ -1020,8 +1039,8 @@ impl JsBindgen<'_> {
1020
1039
CoreFn {
1021
1040
retsize : if sig. retptr {
1022
1041
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 ;
1025
1044
}
1026
1045
retsize
1027
1046
} else {
@@ -1330,3 +1349,20 @@ fn binding_name(func_name: &str, iface_name: &Option<String>) -> String {
1330
1349
None => format ! ( "{func_name}" ) ,
1331
1350
}
1332
1351
}
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