Skip to content

Commit 24c75e0

Browse files
committed
Deduplicate map typecheck logic
1 parent d48daf5 commit 24c75e0

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

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

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use core::iter;
1111
use core::marker;
1212
use core::mem::{self, MaybeUninit};
1313
use core::str;
14-
use wasmtime_environ::collections::HashMap;
14+
#[cfg(not(feature = "std"))]
15+
use crate::hash_map::HashMap;
1516
use wasmtime_environ::component::{
1617
CanonicalAbiInfo, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, OptionsIndex,
1718
StringEncoding, VariantInfo,
@@ -2091,6 +2092,23 @@ unsafe impl<T: Lift> Lift for WasmList<T> {
20912092
// Maps are represented as `list<tuple<K, V>>` in the canonical ABI, so the
20922093
// lowered form is a (pointer, length) pair just like lists.
20932094

2095+
fn typecheck_map<K, V>(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()>
2096+
where
2097+
K: ComponentType,
2098+
V: ComponentType,
2099+
{
2100+
match ty {
2101+
InterfaceType::Map(t) => {
2102+
let map_ty = &types.types[*t];
2103+
K::typecheck(&map_ty.key, types)?;
2104+
V::typecheck(&map_ty.value, types)?;
2105+
Ok(())
2106+
}
2107+
other => bail!("expected `map` found `{}`", desc(other)),
2108+
}
2109+
}
2110+
2111+
#[cfg(not(feature = "std"))]
20942112
unsafe impl<K, V> ComponentType for HashMap<K, V>
20952113
where
20962114
K: ComponentType,
@@ -2101,18 +2119,11 @@ where
21012119
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::POINTER_PAIR;
21022120

21032121
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
2104-
match ty {
2105-
InterfaceType::Map(t) => {
2106-
let map_ty = &types.types[*t];
2107-
K::typecheck(&map_ty.key, types)?;
2108-
V::typecheck(&map_ty.value, types)?;
2109-
Ok(())
2110-
}
2111-
other => bail!("expected `map` found `{}`", desc(other)),
2112-
}
2122+
typecheck_map::<K, V>(ty, types)
21132123
}
21142124
}
21152125

2126+
#[cfg(not(feature = "std"))]
21162127
unsafe impl<K, V> Lower for HashMap<K, V>
21172128
where
21182129
K: Lower,
@@ -2196,6 +2207,7 @@ where
21962207
Ok((ptr, len))
21972208
}
21982209

2210+
#[cfg(not(feature = "std"))]
21992211
unsafe impl<K, V> Lift for HashMap<K, V>
22002212
where
22012213
K: Lift + Eq + Hash,
@@ -2288,6 +2300,7 @@ where
22882300
Ok(())
22892301
}
22902302

2303+
#[cfg(not(feature = "std"))]
22912304
fn lift_map<K, V>(
22922305
cx: &mut LiftContext<'_>,
22932306
key_ty: InterfaceType,
@@ -2321,15 +2334,7 @@ where
23212334
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::POINTER_PAIR;
23222335

23232336
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
2324-
match ty {
2325-
InterfaceType::Map(t) => {
2326-
let map_ty = &types.types[*t];
2327-
K::typecheck(&map_ty.key, types)?;
2328-
V::typecheck(&map_ty.value, types)?;
2329-
Ok(())
2330-
}
2331-
other => bail!("expected `map` found `{}`", desc(other)),
2332-
}
2337+
typecheck_map::<K, V>(ty, types)
23332338
}
23342339
}
23352340

0 commit comments

Comments
 (0)