Skip to content

Commit 1d6ca26

Browse files
committed
Clean up lift_try_map: use drop, move TryHashMap import to module scope
1 parent 9f860ff commit 1d6ca26

1 file changed

Lines changed: 82 additions & 9 deletions

File tree

  • crates/wasmtime/src/runtime/component/func

crates/wasmtime/src/runtime/component/func/typed.rs

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::component::Instance;
22
use crate::component::func::{Func, LiftContext, LowerContext};
33
use crate::component::matching::InstanceType;
44
use crate::component::storage::{storage_as_slice, storage_as_slice_mut};
5+
#[cfg(not(feature = "std"))]
6+
use crate::hash_map::HashMap;
57
use crate::prelude::*;
68
use crate::{AsContextMut, StoreContext, StoreContextMut, ValRaw};
79
use alloc::borrow::Cow;
@@ -11,8 +13,6 @@ use core::iter;
1113
use core::marker;
1214
use core::mem::{self, MaybeUninit};
1315
use core::str;
14-
#[cfg(not(feature = "std"))]
15-
use crate::hash_map::HashMap;
1616
use 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")]
2222
use 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")]
23942397
unsafe 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>
23952469
where
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>>
24472521
where
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

Comments
 (0)