Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Add first-class type access of `component::Func` (#11943)

Alex Crichton92126c50c6d7
diff --git a/crates/fuzzing/src/oracles.rs b/crates/fuzzing/src/oracles.rs
index 88e880a939..3ab12c1b05 100644
--- a/crates/fuzzing/src/oracles.rs
+++ b/crates/fuzzing/src/oracles.rs
@@ -1109,17 +1109,16 @@ pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbi
let instance = linker.instantiate(&mut store, &component).unwrap();
let func = instance.get_func(&mut store, EXPORT_FUNCTION).unwrap();
- let param_tys = func.params(&store);
- let result_tys = func.results(&store);
+ let ty = func.ty(&store);
while input.arbitrary()? {
- let params = param_tys
- .iter()
- .map(|(_, ty)| component_types::arbitrary_val(ty, input))
+ let params = ty
+ .params()
+ .map(|(_, ty)| component_types::arbitrary_val(&ty, input))
.collect::<arbitrary::Result<Vec<_>>>()?;
- let results = result_tys
- .iter()
- .map(|ty| component_types::arbitrary_val(ty, input))
+ let results = ty
+ .results()
+ .map(|ty| component_types::arbitrary_val(&ty, input))
.collect::<arbitrary::Result<Vec<_>>>()?;
*store.data_mut() = (params.clone(), Some(results.clone()));
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index 32823dc289..f016ef9b70 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -1,7 +1,7 @@
use crate::component::instance::Instance;
use crate::component::matching::InstanceType;
use crate::component::storage::storage_as_slice;
-use crate::component::types::Type;
+use crate::component::types::ComponentFunc;
use crate::component::values::Val;
use crate::prelude::*;
use crate::runtime::vm::component::{ComponentInstance, InstanceFlags, ResourceTables};
@@ -167,7 +167,7 @@ impl Func {
Return: ComponentNamedList + Lift,
{
let cx = InstanceType::new(instance.unwrap_or_else(|| self.instance.id().get(store)));
- let ty = &cx.types[self.ty(store)];
+ let ty = &cx.types[self.ty_index(store)];
Params::typecheck(&InterfaceType::Tuple(ty.params), &cx)
.context("type mismatch with parameters")?;
@@ -177,34 +177,18 @@ impl Func {
Ok(())
}
- /// Get the parameter names and types for this function.
- pub fn params(&self, store: impl AsContext) -> Box<[(String, Type)]> {
- let store = store.as_context();
- let instance = self.instance.id().get(store.0);
- let types = instance.component().types();
- let func_ty = &types[self.ty(store.0)];
- types[func_ty.params]
- .types
- .iter()
- .zip(&func_ty.param_names)
- .map(|(ty, name)| (name.clone(), Type::from(ty, &InstanceType::new(instance))))
- .collect()
+ /// Get the type of this function.
+ pub fn ty(&self, store: impl AsContext) -> ComponentFunc {
+ self.ty_(store.as_context().0)
}
- /// Get the result types for this function.
- pub fn results(&self, store: impl AsContext) -> Box<[Type]> {
- let store = store.as_context();
- let instance = self.instance.id().get(store.0);
- let types = instance.component().types();
- let ty = self.ty(store.0);
- types[types[ty].results]
- .types
- .iter()
- .map(|ty| Type::from(ty, &InstanceType::new(instance)))
- .collect()
+ fn ty_(&self, store: &StoreOpaque) -> ComponentFunc {
+ let cx = InstanceType::new(self.instance.id().get(store));
+ let ty = self.ty_index(store);
+ ComponentFunc::from(ty, &cx)
}
- fn ty(&self, store: &StoreOpaque) -> TypeFuncIndex {
+ fn ty_index(&self, store: &StoreOpaque) -> TypeFuncIndex {
let instance = self.instance.id().get(store);
let (ty, _, _) = instance.component().export_lifted_function(self.index);
ty
@@ -307,21 +291,19 @@ impl Func {
params: &[Val],
results: &mut [Val],
) -> Result<()> {
- let param_tys = self.params(&store);
- if param_tys.len() != params.len() {
+ let ty = self.ty(&store);
+ if ty.params().len() != params.len() {
bail!(
"expected {} argument(s), got {}",
- param_tys.len(),
+ ty.params().len(),
params.len(),
);
}
- let result_tys = self.results(&store);
-
- if result_tys.len() != results.len() {
+ if ty.results().len() != results.len() {
bail!(
"expected {} result(s), got {}",
- result_tys.len(),
+ ty.results().len(),
results.len(),
);
}
diff --git a/crates/wast/src/wast.rs b/crates/wast/src/wast.rs
index 83b834583d..0b4dae88eb 100644
--- a/crates/wast/src/wast.rs
+++ b/crates/wast/src/wast.rs
@@ -302,7 +302,8 @@ impl WastContext {
})
.collect::<Result<Vec<_>>>()?;
- let mut results = vec![component::Val::Bool(false); func.results(&store).len()];
+ let mut results =
+ vec![component::Val::Bool(false); func.ty(&store).results().len()];
let result = match &replace.rt {
Some(rt) => {
rt.block_on(func.call_async(&mut *store, &values, &mut results))
diff --git a/tests/all/component_model/resources.rs b/tests/all/component_model/resources.rs
index 6aca26688c..1df11aba2b 100644
--- a/tests/all/component_model/resources.rs
+++ b/tests/all/component_model/resources.rs
@@ -541,13 +541,13 @@ fn dynamic_type() -> Result<()> {
let b = i.get_func(&mut store, "b").unwrap();
let t2 = i.get_resource(&mut store, "t2").unwrap();
- let a_params = a.params(&store);
+ let aty = a.ty(&store);
assert_eq!(
- a_params[0],
- ("x".to_string(), Type::Own(ResourceType::host::<MyType>()))
+ aty.params().next().unwrap(),
+ ("x", Type::Own(ResourceType::host::<MyType>()))
);
- let b_params = b.params(&store);
- match &b_params[0] {
+ let bty = b.ty(&store);
+ match bty.params().next().unwrap() {
(name, Type::Tuple(t)) => {
assert_eq!(name, "x");
assert_eq!(t.types().len(), 1);