Skip to content

Commit 92126c5

Browse files
authored
Add first-class type access of component::Func (#11943)
When `Func:::{params,results}` was added we didn't have `types::ComponentFunc` at the time. Now that `ComponentFunc` exists it's cheaper and easier to access and work with that instead of the one-off accessors on `Func`.
1 parent 87d1730 commit 92126c5

4 files changed

Lines changed: 29 additions & 47 deletions

File tree

crates/fuzzing/src/oracles.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,17 +1109,16 @@ pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbi
11091109

11101110
let instance = linker.instantiate(&mut store, &component).unwrap();
11111111
let func = instance.get_func(&mut store, EXPORT_FUNCTION).unwrap();
1112-
let param_tys = func.params(&store);
1113-
let result_tys = func.results(&store);
1112+
let ty = func.ty(&store);
11141113

11151114
while input.arbitrary()? {
1116-
let params = param_tys
1117-
.iter()
1118-
.map(|(_, ty)| component_types::arbitrary_val(ty, input))
1115+
let params = ty
1116+
.params()
1117+
.map(|(_, ty)| component_types::arbitrary_val(&ty, input))
11191118
.collect::<arbitrary::Result<Vec<_>>>()?;
1120-
let results = result_tys
1121-
.iter()
1122-
.map(|ty| component_types::arbitrary_val(ty, input))
1119+
let results = ty
1120+
.results()
1121+
.map(|ty| component_types::arbitrary_val(&ty, input))
11231122
.collect::<arbitrary::Result<Vec<_>>>()?;
11241123

11251124
*store.data_mut() = (params.clone(), Some(results.clone()));

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::component::instance::Instance;
22
use crate::component::matching::InstanceType;
33
use crate::component::storage::storage_as_slice;
4-
use crate::component::types::Type;
4+
use crate::component::types::ComponentFunc;
55
use crate::component::values::Val;
66
use crate::prelude::*;
77
use crate::runtime::vm::component::{ComponentInstance, InstanceFlags, ResourceTables};
@@ -167,7 +167,7 @@ impl Func {
167167
Return: ComponentNamedList + Lift,
168168
{
169169
let cx = InstanceType::new(instance.unwrap_or_else(|| self.instance.id().get(store)));
170-
let ty = &cx.types[self.ty(store)];
170+
let ty = &cx.types[self.ty_index(store)];
171171

172172
Params::typecheck(&InterfaceType::Tuple(ty.params), &cx)
173173
.context("type mismatch with parameters")?;
@@ -177,34 +177,18 @@ impl Func {
177177
Ok(())
178178
}
179179

180-
/// Get the parameter names and types for this function.
181-
pub fn params(&self, store: impl AsContext) -> Box<[(String, Type)]> {
182-
let store = store.as_context();
183-
let instance = self.instance.id().get(store.0);
184-
let types = instance.component().types();
185-
let func_ty = &types[self.ty(store.0)];
186-
types[func_ty.params]
187-
.types
188-
.iter()
189-
.zip(&func_ty.param_names)
190-
.map(|(ty, name)| (name.clone(), Type::from(ty, &InstanceType::new(instance))))
191-
.collect()
180+
/// Get the type of this function.
181+
pub fn ty(&self, store: impl AsContext) -> ComponentFunc {
182+
self.ty_(store.as_context().0)
192183
}
193184

194-
/// Get the result types for this function.
195-
pub fn results(&self, store: impl AsContext) -> Box<[Type]> {
196-
let store = store.as_context();
197-
let instance = self.instance.id().get(store.0);
198-
let types = instance.component().types();
199-
let ty = self.ty(store.0);
200-
types[types[ty].results]
201-
.types
202-
.iter()
203-
.map(|ty| Type::from(ty, &InstanceType::new(instance)))
204-
.collect()
185+
fn ty_(&self, store: &StoreOpaque) -> ComponentFunc {
186+
let cx = InstanceType::new(self.instance.id().get(store));
187+
let ty = self.ty_index(store);
188+
ComponentFunc::from(ty, &cx)
205189
}
206190

207-
fn ty(&self, store: &StoreOpaque) -> TypeFuncIndex {
191+
fn ty_index(&self, store: &StoreOpaque) -> TypeFuncIndex {
208192
let instance = self.instance.id().get(store);
209193
let (ty, _, _) = instance.component().export_lifted_function(self.index);
210194
ty
@@ -307,21 +291,19 @@ impl Func {
307291
params: &[Val],
308292
results: &mut [Val],
309293
) -> Result<()> {
310-
let param_tys = self.params(&store);
311-
if param_tys.len() != params.len() {
294+
let ty = self.ty(&store);
295+
if ty.params().len() != params.len() {
312296
bail!(
313297
"expected {} argument(s), got {}",
314-
param_tys.len(),
298+
ty.params().len(),
315299
params.len(),
316300
);
317301
}
318302

319-
let result_tys = self.results(&store);
320-
321-
if result_tys.len() != results.len() {
303+
if ty.results().len() != results.len() {
322304
bail!(
323305
"expected {} result(s), got {}",
324-
result_tys.len(),
306+
ty.results().len(),
325307
results.len(),
326308
);
327309
}

crates/wast/src/wast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ impl WastContext {
302302
})
303303
.collect::<Result<Vec<_>>>()?;
304304

305-
let mut results = vec![component::Val::Bool(false); func.results(&store).len()];
305+
let mut results =
306+
vec![component::Val::Bool(false); func.ty(&store).results().len()];
306307
let result = match &replace.rt {
307308
Some(rt) => {
308309
rt.block_on(func.call_async(&mut *store, &values, &mut results))

tests/all/component_model/resources.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,13 @@ fn dynamic_type() -> Result<()> {
541541
let b = i.get_func(&mut store, "b").unwrap();
542542
let t2 = i.get_resource(&mut store, "t2").unwrap();
543543

544-
let a_params = a.params(&store);
544+
let aty = a.ty(&store);
545545
assert_eq!(
546-
a_params[0],
547-
("x".to_string(), Type::Own(ResourceType::host::<MyType>()))
546+
aty.params().next().unwrap(),
547+
("x", Type::Own(ResourceType::host::<MyType>()))
548548
);
549-
let b_params = b.params(&store);
550-
match &b_params[0] {
549+
let bty = b.ty(&store);
550+
match bty.params().next().unwrap() {
551551
(name, Type::Tuple(t)) => {
552552
assert_eq!(name, "x");
553553
assert_eq!(t.types().len(), 1);

0 commit comments

Comments
 (0)