Store WasmFuncType in FuncType (#2365)

This commit updates `wasmtime::FuncType` to exactly store an internal
`WasmFuncType` from the cranelift crates. This allows us to remove a
translation layer when we are given a `FuncType` and want to get an
internal cranelift type out as a result.

The other major change from this commit was changing the constructor and
accessors of `FuncType` to be iterator-based instead of exposing
implementation details.
This commit is contained in:
Alex Crichton
2020-11-05 08:49:03 -06:00
committed by GitHub
parent ea3306e74c
commit a277cf5ee4
13 changed files with 118 additions and 150 deletions

View File

@@ -78,26 +78,26 @@ fn signatures_match() {
let store = Store::default();
let f = Func::wrap(&store, || {});
assert_eq!(f.ty().params(), &[]);
assert_eq!(f.ty().params().collect::<Vec<_>>(), &[]);
assert_eq!(f.param_arity(), 0);
assert_eq!(f.ty().results(), &[]);
assert_eq!(f.ty().results().collect::<Vec<_>>(), &[]);
assert_eq!(f.result_arity(), 0);
let f = Func::wrap(&store, || -> i32 { loop {} });
assert_eq!(f.ty().params(), &[]);
assert_eq!(f.ty().results(), &[ValType::I32]);
assert_eq!(f.ty().params().collect::<Vec<_>>(), &[]);
assert_eq!(f.ty().results().collect::<Vec<_>>(), &[ValType::I32]);
let f = Func::wrap(&store, || -> i64 { loop {} });
assert_eq!(f.ty().params(), &[]);
assert_eq!(f.ty().results(), &[ValType::I64]);
assert_eq!(f.ty().params().collect::<Vec<_>>(), &[]);
assert_eq!(f.ty().results().collect::<Vec<_>>(), &[ValType::I64]);
let f = Func::wrap(&store, || -> f32 { loop {} });
assert_eq!(f.ty().params(), &[]);
assert_eq!(f.ty().results(), &[ValType::F32]);
assert_eq!(f.ty().params().collect::<Vec<_>>(), &[]);
assert_eq!(f.ty().results().collect::<Vec<_>>(), &[ValType::F32]);
let f = Func::wrap(&store, || -> f64 { loop {} });
assert_eq!(f.ty().params(), &[]);
assert_eq!(f.ty().results(), &[ValType::F64]);
assert_eq!(f.ty().params().collect::<Vec<_>>(), &[]);
assert_eq!(f.ty().results().collect::<Vec<_>>(), &[ValType::F64]);
let f = Func::wrap(
&store,
@@ -106,7 +106,7 @@ fn signatures_match() {
},
);
assert_eq!(
f.ty().params(),
f.ty().params().collect::<Vec<_>>(),
&[
ValType::F32,
ValType::F64,
@@ -117,7 +117,7 @@ fn signatures_match() {
ValType::FuncRef,
]
);
assert_eq!(f.ty().results(), &[ValType::F64]);
assert_eq!(f.ty().results().collect::<Vec<_>>(), &[ValType::F64]);
}
#[test]
@@ -283,13 +283,13 @@ fn get_from_wrapper() {
#[test]
fn get_from_signature() {
let store = Store::default();
let ty = FuncType::new(Box::new([]), Box::new([]));
let ty = FuncType::new(None, None);
let f = Func::new(&store, ty, |_, _, _| panic!());
assert!(f.get0::<()>().is_ok());
assert!(f.get0::<i32>().is_err());
assert!(f.get1::<i32, ()>().is_err());
let ty = FuncType::new(Box::new([ValType::I32]), Box::new([ValType::F64]));
let ty = FuncType::new(Some(ValType::I32), Some(ValType::F64));
let f = Func::new(&store, ty, |_, _, _| panic!());
assert!(f.get0::<()>().is_err());
assert!(f.get0::<i32>().is_err());
@@ -434,7 +434,7 @@ fn caller_memory() -> anyhow::Result<()> {
#[test]
fn func_write_nothing() -> anyhow::Result<()> {
let store = Store::default();
let ty = FuncType::new(Box::new([]), Box::new([ValType::I32]));
let ty = FuncType::new(None, Some(ValType::I32));
let f = Func::new(&store, ty, |_, _, _| Ok(()));
let err = f.call(&[]).unwrap_err().downcast::<Trap>()?;
assert!(err
@@ -515,8 +515,8 @@ fn externref_signature_no_reference_types() -> anyhow::Result<()> {
Func::new(
&store,
FuncType::new(
Box::new([ValType::FuncRef, ValType::ExternRef]),
Box::new([ValType::FuncRef, ValType::ExternRef]),
[ValType::FuncRef, ValType::ExternRef].iter().cloned(),
[ValType::FuncRef, ValType::ExternRef].iter().cloned(),
),
|_, _, _| Ok(()),
);

View File

@@ -22,21 +22,17 @@ fn test_import_calling_export() {
let other = Rc::new(RefCell::new(None::<Func>));
let other2 = Rc::downgrade(&other);
let callback_func = Func::new(
&store,
FuncType::new(Box::new([]), Box::new([])),
move |_, _, _| {
other2
.upgrade()
.unwrap()
.borrow()
.as_ref()
.expect("expected a function ref")
.call(&[])
.expect("expected function not to trap");
Ok(())
},
);
let callback_func = Func::new(&store, FuncType::new(None, None), move |_, _, _| {
other2
.upgrade()
.unwrap()
.borrow()
.as_ref()
.expect("expected a function ref")
.call(&[])
.expect("expected function not to trap");
Ok(())
});
let imports = vec![callback_func.into()];
let instance =
@@ -71,7 +67,7 @@ fn test_returns_incorrect_type() -> Result<()> {
let callback_func = Func::new(
&store,
FuncType::new(Box::new([]), Box::new([ValType::I32])),
FuncType::new(None, Some(ValType::I32)),
|_, _, results| {
// Evil! Returns I64 here instead of promised in the signature I32.
results[0] = Val::I64(228);

View File

@@ -20,7 +20,7 @@ fn same_import_names_still_distinct() -> anyhow::Result<()> {
let imports = [
Func::new(
&store,
FuncType::new(Box::new([]), Box::new([ValType::I32])),
FuncType::new(None, Some(ValType::I32)),
|_, params, results| {
assert!(params.is_empty());
assert_eq!(results.len(), 1);
@@ -31,7 +31,7 @@ fn same_import_names_still_distinct() -> anyhow::Result<()> {
.into(),
Func::new(
&store,
FuncType::new(Box::new([]), Box::new([ValType::F32])),
FuncType::new(None, Some(ValType::F32)),
|_, params, results| {
assert!(params.is_empty());
assert_eq!(results.len(), 1);

View File

@@ -13,7 +13,7 @@ fn test_trap_return() -> Result<()> {
"#;
let module = Module::new(store.engine(), wat)?;
let hello_type = FuncType::new(Box::new([]), Box::new([]));
let hello_type = FuncType::new(None, None);
let hello_func = Func::new(&store, hello_type, |_, _, _| Err(Trap::new("test 123")));
let instance = Instance::new(&store, &module, &[hello_func.into()])?;
@@ -86,7 +86,7 @@ fn test_trap_trace_cb() -> Result<()> {
)
"#;
let fn_type = FuncType::new(Box::new([]), Box::new([]));
let fn_type = FuncType::new(None, None);
let fn_func = Func::new(&store, fn_type, |_, _, _| Err(Trap::new("cb throw")));
let module = Module::new(store.engine(), wat)?;
@@ -237,7 +237,7 @@ fn trap_start_function_import() -> Result<()> {
)?;
let module = Module::new(store.engine(), &binary)?;
let sig = FuncType::new(Box::new([]), Box::new([]));
let sig = FuncType::new(None, None);
let func = Func::new(&store, sig, |_, _, _| Err(Trap::new("user trap")));
let err = Instance::new(&store, &module, &[func.into()])
.err()
@@ -267,7 +267,7 @@ fn rust_panic_import() -> Result<()> {
)?;
let module = Module::new(store.engine(), &binary)?;
let sig = FuncType::new(Box::new([]), Box::new([]));
let sig = FuncType::new(None, None);
let func = Func::new(&store, sig, |_, _, _| panic!("this is a panic"));
let instance = Instance::new(
&store,
@@ -311,7 +311,7 @@ fn rust_panic_start_function() -> Result<()> {
)?;
let module = Module::new(store.engine(), &binary)?;
let sig = FuncType::new(Box::new([]), Box::new([]));
let sig = FuncType::new(None, None);
let func = Func::new(&store, sig, |_, _, _| panic!("this is a panic"));
let err = panic::catch_unwind(AssertUnwindSafe(|| {
drop(Instance::new(&store, &module, &[func.into()]));