@@ -2,6 +2,8 @@ use crate::component::Instance;
22use crate :: component:: func:: { Func , LiftContext , LowerContext } ;
33use crate :: component:: matching:: InstanceType ;
44use crate :: component:: storage:: { storage_as_slice, storage_as_slice_mut} ;
5+ #[ cfg( not( feature = "std" ) ) ]
6+ use crate :: hash_map:: HashMap ;
57use crate :: prelude:: * ;
68use crate :: { AsContextMut , StoreContext , StoreContextMut , ValRaw } ;
79use alloc:: borrow:: Cow ;
@@ -11,8 +13,6 @@ use core::iter;
1113use core:: marker;
1214use core:: mem:: { self , MaybeUninit } ;
1315use core:: str;
14- #[ cfg( not( feature = "std" ) ) ]
15- use crate :: hash_map:: HashMap ;
1616use wasmtime_environ:: component:: {
1717 CanonicalAbiInfo , InterfaceType , MAX_FLAT_PARAMS , MAX_FLAT_RESULTS , OptionsIndex ,
1818 StringEncoding , VariantInfo ,
@@ -21,6 +21,9 @@ use wasmtime_environ::component::{
2121#[ cfg( feature = "component-model-async" ) ]
2222use crate :: component:: concurrent:: { self , AsAccessor , PreparedCall } ;
2323
24+ #[ cfg( feature = "std" ) ]
25+ use wasmtime_environ:: collections:: TryHashMap ;
26+
2427/// A statically-typed version of [`Func`] which takes `Params` as input and
2528/// returns `Return`.
2629///
@@ -2392,6 +2395,77 @@ where
23922395
23932396#[ cfg( feature = "std" ) ]
23942397unsafe impl < K , V > Lift for std:: collections:: HashMap < K , V >
2398+ where
2399+ K : Lift + Eq + Hash ,
2400+ V : Lift ,
2401+ {
2402+ fn linear_lift_from_flat (
2403+ cx : & mut LiftContext < ' _ > ,
2404+ ty : InterfaceType ,
2405+ src : & Self :: Lower ,
2406+ ) -> Result < Self > {
2407+ let try_map =
2408+ <wasmtime_environ:: collections:: TryHashMap < K , V > as Lift >:: linear_lift_from_flat (
2409+ cx, ty, src,
2410+ ) ?;
2411+ Ok ( try_map. into_iter ( ) . collect ( ) )
2412+ }
2413+
2414+ fn linear_lift_from_memory (
2415+ cx : & mut LiftContext < ' _ > ,
2416+ ty : InterfaceType ,
2417+ bytes : & [ u8 ] ,
2418+ ) -> Result < Self > {
2419+ let try_map =
2420+ <wasmtime_environ:: collections:: TryHashMap < K , V > as Lift >:: linear_lift_from_memory (
2421+ cx, ty, bytes,
2422+ ) ?;
2423+ Ok ( try_map. into_iter ( ) . collect ( ) )
2424+ }
2425+ }
2426+
2427+ #[ cfg( feature = "std" ) ]
2428+ unsafe impl < K , V > ComponentType for wasmtime_environ:: collections:: TryHashMap < K , V >
2429+ where
2430+ K : ComponentType ,
2431+ V : ComponentType ,
2432+ {
2433+ type Lower = [ ValRaw ; 2 ] ;
2434+
2435+ const ABI : CanonicalAbiInfo = CanonicalAbiInfo :: POINTER_PAIR ;
2436+
2437+ fn typecheck ( ty : & InterfaceType , types : & InstanceType < ' _ > ) -> Result < ( ) > {
2438+ typecheck_map :: < K , V > ( ty, types)
2439+ }
2440+ }
2441+
2442+ #[ cfg( feature = "std" ) ]
2443+ unsafe impl < K , V > Lower for wasmtime_environ:: collections:: TryHashMap < K , V >
2444+ where
2445+ K : Lower ,
2446+ V : Lower ,
2447+ {
2448+ fn linear_lower_to_flat < U > (
2449+ & self ,
2450+ cx : & mut LowerContext < ' _ , U > ,
2451+ ty : InterfaceType ,
2452+ dst : & mut MaybeUninit < [ ValRaw ; 2 ] > ,
2453+ ) -> Result < ( ) > {
2454+ linear_lower_map_to_flat ( cx, ty, self . len ( ) , self . iter ( ) , dst)
2455+ }
2456+
2457+ fn linear_lower_to_memory < U > (
2458+ & self ,
2459+ cx : & mut LowerContext < ' _ , U > ,
2460+ ty : InterfaceType ,
2461+ offset : usize ,
2462+ ) -> Result < ( ) > {
2463+ linear_lower_map_to_memory ( cx, ty, self . len ( ) , self . iter ( ) , offset)
2464+ }
2465+ }
2466+
2467+ #[ cfg( feature = "std" ) ]
2468+ unsafe impl < K , V > Lift for wasmtime_environ:: collections:: TryHashMap < K , V >
23952469where
23962470 K : Lift + Eq + Hash ,
23972471 V : Lift ,
@@ -2412,7 +2486,7 @@ where
24122486 let ptr = src[ 0 ] . get_u32 ( ) ;
24132487 let len = src[ 1 ] . get_u32 ( ) ;
24142488 let ( ptr, len) = ( usize:: try_from ( ptr) ?, usize:: try_from ( len) ?) ;
2415- lift_std_map ( cx, key_ty, value_ty, ptr, len)
2489+ lift_try_map ( cx, key_ty, value_ty, ptr, len)
24162490 }
24172491
24182492 fn linear_lift_from_memory (
@@ -2432,26 +2506,25 @@ where
24322506 let ptr = u32:: from_le_bytes ( bytes[ ..4 ] . try_into ( ) . unwrap ( ) ) ;
24332507 let len = u32:: from_le_bytes ( bytes[ 4 ..] . try_into ( ) . unwrap ( ) ) ;
24342508 let ( ptr, len) = ( usize:: try_from ( ptr) ?, usize:: try_from ( len) ?) ;
2435- lift_std_map ( cx, key_ty, value_ty, ptr, len)
2509+ lift_try_map ( cx, key_ty, value_ty, ptr, len)
24362510 }
24372511}
24382512
24392513#[ cfg( feature = "std" ) ]
2440- fn lift_std_map < K , V > (
2514+ fn lift_try_map < K , V > (
24412515 cx : & mut LiftContext < ' _ > ,
24422516 key_ty : InterfaceType ,
24432517 value_ty : InterfaceType ,
24442518 ptr : usize ,
24452519 len : usize ,
2446- ) -> Result < std :: collections :: HashMap < K , V > >
2520+ ) -> Result < TryHashMap < K , V > >
24472521where
24482522 K : Lift + Eq + Hash ,
24492523 V : Lift ,
24502524{
2451- let mut result = std :: collections :: HashMap :: with_capacity ( len) ;
2525+ let mut result = TryHashMap :: with_capacity ( len) ? ;
24522526 lift_map_pairs ( cx, key_ty, value_ty, ptr, len, |k, v| {
2453- result. insert ( k, v) ;
2454- Ok ( ( ) )
2527+ result. insert ( k, v) . map ( drop) . map_err ( Into :: into)
24552528 } ) ?;
24562529 Ok ( result)
24572530}
0 commit comments