Skip to content

Commit 17ba37c

Browse files
committed
component::__internal::InstanceType now owns its contents
Having refs to a pair of arcs made it less expensive to create an InstanceType, but tied its to a borrow of the ComponentInstance (transitively the Store) which makes some new typechecking operations which have an AsContextMut impossible. This has no direct benefit in this PR, but it split out of #10610 because it is more easily reviewed in isolation.
1 parent 930d354 commit 17ba37c

File tree

10 files changed

+72
-78
lines changed

10 files changed

+72
-78
lines changed

crates/component-macro/src/component.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ fn expand_record_for_component_type(
410410
#[inline]
411411
fn typecheck(
412412
ty: &#internal::InterfaceType,
413-
types: &#internal::InstanceType<'_>,
413+
types: &#internal::InstanceType,
414414
) -> #internal::anyhow::Result<()> {
415415
#internal::#typecheck(ty, types, &[#typecheck_argument])
416416
}
@@ -1019,7 +1019,7 @@ impl Expander for ComponentTypeExpander {
10191019
#[inline]
10201020
fn typecheck(
10211021
ty: &#internal::InterfaceType,
1022-
types: &#internal::InstanceType<'_>,
1022+
types: &#internal::InstanceType,
10231023
) -> #internal::anyhow::Result<()> {
10241024
#internal::typecheck_variant(ty, types, &[#case_names_and_checks])
10251025
}
@@ -1080,7 +1080,7 @@ impl Expander for ComponentTypeExpander {
10801080
#[inline]
10811081
fn typecheck(
10821082
ty: &#internal::InterfaceType,
1083-
types: &#internal::InstanceType<'_>,
1083+
types: &#internal::InstanceType,
10841084
) -> #internal::anyhow::Result<()> {
10851085
#internal::typecheck_enum(ty, types, &[#case_names])
10861086
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ impl Component {
382382
self.with_uninstantiated_instance_type(|ty| types::Component::from(self.inner.ty, ty))
383383
}
384384

385-
fn with_uninstantiated_instance_type<R>(&self, f: impl FnOnce(&InstanceType<'_>) -> R) -> R {
385+
fn with_uninstantiated_instance_type<R>(&self, f: impl FnOnce(&InstanceType) -> R) -> R {
386386
let resources = Arc::new(PrimaryMap::new());
387387
f(&InstanceType {
388-
types: self.types(),
389-
resources: &resources,
388+
types: self.types().clone(),
389+
resources,
390390
})
391391
}
392392

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use wasmtime_environ::component::{
1919

2020
pub struct HostFunc {
2121
entrypoint: VMLoweringCallee,
22-
typecheck: Box<dyn (Fn(TypeFuncIndex, &InstanceType<'_>) -> Result<()>) + Send + Sync>,
22+
typecheck: Box<dyn (Fn(TypeFuncIndex, &InstanceType) -> Result<()>) + Send + Sync>,
2323
func: Box<dyn Any + Send + Sync>,
2424
}
2525

@@ -96,7 +96,7 @@ impl HostFunc {
9696
})
9797
}
9898

99-
pub fn typecheck(&self, ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()> {
99+
pub fn typecheck(&self, ty: TypeFuncIndex, types: &InstanceType) -> Result<()> {
100100
(self.typecheck)(ty, types)
101101
}
102102

@@ -109,7 +109,7 @@ impl HostFunc {
109109
}
110110
}
111111

112-
fn typecheck<P, R>(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()>
112+
fn typecheck<P, R>(ty: TypeFuncIndex, types: &InstanceType) -> Result<()>
113113
where
114114
P: ComponentNamedList + Lift,
115115
R: ComponentNamedList + Lower,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl<'a, T> LowerContext<'a, T> {
355355

356356
/// Returns the instance type information corresponding to the instance that
357357
/// this context is lowering into.
358-
pub fn instance_type(&self) -> InstanceType<'_> {
358+
pub fn instance_type(&self) -> InstanceType {
359359
// Note that the unsafety here should be valid given the contract of
360360
// `LowerContext::new`.
361361
InstanceType::new(unsafe { &*self.instance })
@@ -520,7 +520,7 @@ impl<'a> LiftContext<'a> {
520520

521521
/// Returns instance type information for the component instance that is
522522
/// being lifted from.
523-
pub fn instance_type(&self) -> InstanceType<'_> {
523+
pub fn instance_type(&self) -> InstanceType {
524524
// Note that the unsafety here should be valid given the contract of
525525
// `LiftContext::new`.
526526
InstanceType::new(unsafe { &*self.instance })

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

+21-29
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ pub unsafe trait ComponentType {
503503
/// Performs a type-check to see whether this component value type matches
504504
/// the interface type `ty` provided.
505505
#[doc(hidden)]
506-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()>;
506+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()>;
507507
}
508508

509509
#[doc(hidden)]
@@ -687,7 +687,7 @@ macro_rules! forward_type_impls {
687687
const ABI: CanonicalAbiInfo = <$b as ComponentType>::ABI;
688688

689689
#[inline]
690-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
690+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()> {
691691
<$b as ComponentType>::typecheck(ty, types)
692692
}
693693
}
@@ -791,7 +791,7 @@ macro_rules! integers {
791791

792792
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::$abi;
793793

794-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
794+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
795795
match ty {
796796
InterfaceType::$ty => Ok(()),
797797
other => bail!("expected `{}` found `{}`", desc(&InterfaceType::$ty), desc(other))
@@ -909,7 +909,7 @@ macro_rules! floats {
909909

910910
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::$abi;
911911

912-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
912+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
913913
match ty {
914914
InterfaceType::$ty => Ok(()),
915915
other => bail!("expected `{}` found `{}`", desc(&InterfaceType::$ty), desc(other))
@@ -1026,7 +1026,7 @@ unsafe impl ComponentType for bool {
10261026

10271027
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR1;
10281028

1029-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
1029+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
10301030
match ty {
10311031
InterfaceType::Bool => Ok(()),
10321032
other => bail!("expected `bool` found `{}`", desc(other)),
@@ -1084,7 +1084,7 @@ unsafe impl ComponentType for char {
10841084

10851085
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
10861086

1087-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
1087+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
10881088
match ty {
10891089
InterfaceType::Char => Ok(()),
10901090
other => bail!("expected `char` found `{}`", desc(other)),
@@ -1146,7 +1146,7 @@ unsafe impl ComponentType for str {
11461146

11471147
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::POINTER_PAIR;
11481148

1149-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
1149+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
11501150
match ty {
11511151
InterfaceType::String => Ok(()),
11521152
other => bail!("expected `string` found `{}`", desc(other)),
@@ -1437,7 +1437,7 @@ unsafe impl ComponentType for WasmStr {
14371437

14381438
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::POINTER_PAIR;
14391439

1440-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
1440+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
14411441
match ty {
14421442
InterfaceType::String => Ok(()),
14431443
other => bail!("expected `string` found `{}`", desc(other)),
@@ -1476,7 +1476,7 @@ where
14761476

14771477
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::POINTER_PAIR;
14781478

1479-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
1479+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()> {
14801480
match ty {
14811481
InterfaceType::List(t) => T::typecheck(&types.types[*t].element, types),
14821482
other => bail!("expected `list` found `{}`", desc(other)),
@@ -1742,7 +1742,7 @@ unsafe impl<T: ComponentType> ComponentType for WasmList<T> {
17421742

17431743
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::POINTER_PAIR;
17441744

1745-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
1745+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()> {
17461746
<[T] as ComponentType>::typecheck(ty, types)
17471747
}
17481748
}
@@ -1777,8 +1777,8 @@ unsafe impl<T: Lift> Lift for WasmList<T> {
17771777
/// Verify that the given wasm type is a tuple with the expected fields in the right order.
17781778
fn typecheck_tuple(
17791779
ty: &InterfaceType,
1780-
types: &InstanceType<'_>,
1781-
expected: &[fn(&InterfaceType, &InstanceType<'_>) -> Result<()>],
1780+
types: &InstanceType,
1781+
expected: &[fn(&InterfaceType, &InstanceType) -> Result<()>],
17821782
) -> Result<()> {
17831783
match ty {
17841784
InterfaceType::Tuple(t) => {
@@ -1803,8 +1803,8 @@ fn typecheck_tuple(
18031803
/// names.
18041804
pub fn typecheck_record(
18051805
ty: &InterfaceType,
1806-
types: &InstanceType<'_>,
1807-
expected: &[(&str, fn(&InterfaceType, &InstanceType<'_>) -> Result<()>)],
1806+
types: &InstanceType,
1807+
expected: &[(&str, fn(&InterfaceType, &InstanceType) -> Result<()>)],
18081808
) -> Result<()> {
18091809
match ty {
18101810
InterfaceType::Record(index) => {
@@ -1837,10 +1837,10 @@ pub fn typecheck_record(
18371837
/// names.
18381838
pub fn typecheck_variant(
18391839
ty: &InterfaceType,
1840-
types: &InstanceType<'_>,
1840+
types: &InstanceType,
18411841
expected: &[(
18421842
&str,
1843-
Option<fn(&InterfaceType, &InstanceType<'_>) -> Result<()>>,
1843+
Option<fn(&InterfaceType, &InstanceType) -> Result<()>>,
18441844
)],
18451845
) -> Result<()> {
18461846
match ty {
@@ -1881,11 +1881,7 @@ pub fn typecheck_variant(
18811881

18821882
/// Verify that the given wasm type is a enum with the expected cases in the right order and with the right
18831883
/// names.
1884-
pub fn typecheck_enum(
1885-
ty: &InterfaceType,
1886-
types: &InstanceType<'_>,
1887-
expected: &[&str],
1888-
) -> Result<()> {
1884+
pub fn typecheck_enum(ty: &InterfaceType, types: &InstanceType, expected: &[&str]) -> Result<()> {
18891885
match ty {
18901886
InterfaceType::Enum(index) => {
18911887
let names = &types.types[*index].names;
@@ -1912,11 +1908,7 @@ pub fn typecheck_enum(
19121908

19131909
/// Verify that the given wasm type is a flags type with the expected flags in the right order and with the right
19141910
/// names.
1915-
pub fn typecheck_flags(
1916-
ty: &InterfaceType,
1917-
types: &InstanceType<'_>,
1918-
expected: &[&str],
1919-
) -> Result<()> {
1911+
pub fn typecheck_flags(ty: &InterfaceType, types: &InstanceType, expected: &[&str]) -> Result<()> {
19201912
match ty {
19211913
InterfaceType::Flags(index) => {
19221914
let names = &types.types[*index].names;
@@ -1967,7 +1959,7 @@ where
19671959

19681960
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::variant_static(&[None, Some(T::ABI)]);
19691961

1970-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
1962+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()> {
19711963
match ty {
19721964
InterfaceType::Option(t) => T::typecheck(&types.types[*t].ty, types),
19731965
other => bail!("expected `option` found `{}`", desc(other)),
@@ -2096,7 +2088,7 @@ where
20962088

20972089
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::variant_static(&[Some(T::ABI), Some(E::ABI)]);
20982090

2099-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
2091+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()> {
21002092
match ty {
21012093
InterfaceType::Result(r) => {
21022094
let result = &types.types[*r];
@@ -2456,7 +2448,7 @@ macro_rules! impl_component_ty_for_tuples {
24562448

24572449
fn typecheck(
24582450
ty: &InterfaceType,
2459-
types: &InstanceType<'_>,
2451+
types: &InstanceType,
24602452
) -> Result<()> {
24612453
typecheck_tuple(ty, types, &[$($t::typecheck),*])
24622454
}

crates/wasmtime/src/runtime/component/instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl InstanceData {
476476
}
477477

478478
#[inline]
479-
pub fn ty(&self) -> InstanceType<'_> {
479+
pub fn ty(&self) -> InstanceType {
480480
InstanceType::new(self.instance())
481481
}
482482

crates/wasmtime/src/runtime/component/linker.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ impl<T> Linker<T> {
161161
fn typecheck<'a>(&'a self, component: &'a Component) -> Result<TypeChecker<'a>> {
162162
let mut cx = TypeChecker {
163163
engine: &self.engine,
164-
types: component.types(),
165164
strings: &self.strings,
165+
types: &component.types(),
166166
imported_resources: Default::default(),
167167
};
168168

@@ -185,8 +185,8 @@ impl<T> Linker<T> {
185185
Ok(types::Component::from(
186186
component.ty(),
187187
&InstanceType {
188-
types: cx.types,
189-
resources: &cx.imported_resources,
188+
types: cx.types.clone(),
189+
resources: cx.imported_resources.clone(),
190190
},
191191
))
192192
}

crates/wasmtime/src/runtime/component/matching.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ pub struct TypeChecker<'a> {
1919
pub imported_resources: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
2020
}
2121

22-
#[derive(Copy, Clone)]
22+
#[derive(Clone)]
2323
#[doc(hidden)]
24-
pub struct InstanceType<'a> {
25-
pub types: &'a Arc<ComponentTypes>,
26-
pub resources: &'a Arc<PrimaryMap<ResourceIndex, ResourceType>>,
24+
pub struct InstanceType {
25+
pub types: Arc<ComponentTypes>,
26+
pub resources: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
2727
}
2828

2929
impl TypeChecker<'_> {
@@ -164,11 +164,13 @@ impl TypeChecker<'_> {
164164
}
165165

166166
fn func(&self, expected: TypeFuncIndex, actual: &HostFunc) -> Result<()> {
167-
let instance_type = InstanceType {
168-
types: self.types,
169-
resources: &self.imported_resources,
170-
};
171-
actual.typecheck(expected, &instance_type)
167+
actual.typecheck(
168+
expected,
169+
&InstanceType {
170+
types: self.types.clone(),
171+
resources: self.imported_resources.clone(),
172+
},
173+
)
172174
}
173175
}
174176

@@ -183,11 +185,11 @@ impl Definition {
183185
}
184186
}
185187

186-
impl<'a> InstanceType<'a> {
187-
pub fn new(instance: &'a ComponentInstance) -> InstanceType<'a> {
188+
impl InstanceType {
189+
pub fn new(instance: &ComponentInstance) -> InstanceType {
188190
InstanceType {
189-
types: instance.component_types(),
190-
resources: instance.resource_types(),
191+
types: instance.component_types().clone(),
192+
resources: instance.resource_types().clone(),
191193
}
192194
}
193195

crates/wasmtime/src/runtime/component/resources.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ unsafe impl<T: 'static> ComponentType for Resource<T> {
821821

822822
type Lower = <u32 as ComponentType>::Lower;
823823

824-
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
824+
fn typecheck(ty: &InterfaceType, types: &InstanceType) -> Result<()> {
825825
let resource = match ty {
826826
InterfaceType::Own(t) | InterfaceType::Borrow(t) => *t,
827827
other => bail!("expected `own` or `borrow`, found `{}`", desc(other)),
@@ -1108,7 +1108,7 @@ unsafe impl ComponentType for ResourceAny {
11081108

11091109
type Lower = <u32 as ComponentType>::Lower;
11101110

1111-
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
1111+
fn typecheck(ty: &InterfaceType, _types: &InstanceType) -> Result<()> {
11121112
match ty {
11131113
InterfaceType::Own(_) | InterfaceType::Borrow(_) => Ok(()),
11141114
other => bail!("expected `own` or `borrow`, found `{}`", desc(other)),

0 commit comments

Comments
 (0)