unsplat component::Linker::func_wrap args (#5065)

* component::Linker::func_wrap: replace IntoComponentFunc with directly accepting a closure

We find that this makes the Linker::func_wrap type signature much easier
to read. The IntoComponentFunc abstraction was adding a lot of weight to
"splat" a set of arguments from a tuple of types into individual
arguments to the closure. Additionally, making the StoreContextMut
argument optional, or the Result<return> optional, wasn't very
worthwhile.

* Fixes for the new style of closure required by component::Linker::func_wrap

* fix fuzzing generator
This commit is contained in:
Pat Hickey
2022-10-18 07:24:14 -07:00
committed by GitHub
parent 32a7593c94
commit 78ecc17d0f
10 changed files with 107 additions and 176 deletions

View File

@@ -127,7 +127,7 @@ fn simple() -> Result<()> {
let mut linker = Linker::new(&engine);
linker.root().func_wrap(
"",
|mut store: StoreContextMut<'_, Option<String>>, arg: WasmStr| -> Result<_> {
|mut store: StoreContextMut<'_, Option<String>>, (arg,): (WasmStr,)| -> Result<_> {
let s = arg.to_str(&store)?.to_string();
assert!(store.data().is_none());
*store.data_mut() = Some(s);
@@ -239,12 +239,14 @@ fn attempt_to_leave_during_malloc() -> Result<()> {
let engine = super::engine();
let mut linker = Linker::new(&engine);
linker.root().func_wrap("thunk", |_, _: ()| -> Result<()> {
panic!("should not get here")
})?;
linker
.root()
.func_wrap("thunk", || -> Result<()> { panic!("should not get here") })?;
linker
.root()
.func_wrap("ret-string", || -> Result<_> { Ok(("hello".to_string(),)) })?;
.func_wrap("ret-string", |_, _: ()| -> Result<_> {
Ok(("hello".to_string(),))
})?;
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, ());
@@ -338,7 +340,7 @@ fn attempt_to_reenter_during_host() -> Result<()> {
let mut linker = Linker::new(&engine);
linker.root().func_wrap(
"thunk",
|mut store: StoreContextMut<'_, StaticState>| -> Result<()> {
|mut store: StoreContextMut<'_, StaticState>, _: ()| -> Result<()> {
let func = store.data_mut().func.take().unwrap();
let trap = func.call(&mut store, ()).unwrap_err();
assert!(
@@ -530,14 +532,16 @@ fn stack_and_heap_args_and_rets() -> Result<()> {
// First, test the static API
let mut linker = Linker::new(&engine);
linker.root().func_wrap("f1", |x: u32| -> Result<(u32,)> {
assert_eq!(x, 1);
Ok((2,))
})?;
linker
.root()
.func_wrap("f1", |_, (x,): (u32,)| -> Result<(u32,)> {
assert_eq!(x, 1);
Ok((2,))
})?;
linker.root().func_wrap(
"f2",
|cx: StoreContextMut<'_, ()>,
arg: (
(arg,): ((
WasmStr,
WasmStr,
WasmStr,
@@ -547,7 +551,7 @@ fn stack_and_heap_args_and_rets() -> Result<()> {
WasmStr,
WasmStr,
WasmStr,
)|
),)|
-> Result<(u32,)> {
assert_eq!(arg.0.to_str(&cx).unwrap(), "abc");
Ok((3,))
@@ -555,14 +559,14 @@ fn stack_and_heap_args_and_rets() -> Result<()> {
)?;
linker
.root()
.func_wrap("f3", |arg: u32| -> Result<(String,)> {
.func_wrap("f3", |_, (arg,): (u32,)| -> Result<(String,)> {
assert_eq!(arg, 8);
Ok(("xyz".to_string(),))
})?;
linker.root().func_wrap(
"f4",
|cx: StoreContextMut<'_, ()>,
arg: (
(arg,): ((
WasmStr,
WasmStr,
WasmStr,
@@ -572,7 +576,7 @@ fn stack_and_heap_args_and_rets() -> Result<()> {
WasmStr,
WasmStr,
WasmStr,
)|
),)|
-> Result<(String,)> {
assert_eq!(arg.0.to_str(&cx).unwrap(), "abc");
Ok(("xyz".to_string(),))
@@ -703,12 +707,13 @@ fn bad_import_alignment() -> Result<()> {
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("unaligned-retptr", || -> Result<(String,)> {
.func_wrap("unaligned-retptr", |_, _: ()| -> Result<(String,)> {
Ok((String::new(),))
})?;
linker.root().func_wrap(
"unaligned-argptr",
|_: (
|_,
_: ((
WasmStr,
WasmStr,
WasmStr,
@@ -718,7 +723,7 @@ fn bad_import_alignment() -> Result<()> {
WasmStr,
WasmStr,
WasmStr,
)|
),)|
-> Result<()> { unreachable!() },
)?;
let component = Component::new(&engine, component)?;
@@ -775,12 +780,13 @@ fn no_actual_wasm_code() -> Result<()> {
// First, test the static API
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("f", |mut store: StoreContextMut<'_, u32>| -> Result<()> {
linker.root().func_wrap(
"f",
|mut store: StoreContextMut<'_, u32>, _: ()| -> Result<()> {
*store.data_mut() += 1;
Ok(())
})?;
},
)?;
let instance = linker.instantiate(&mut store, &component)?;
let thunk = instance.get_typed_func::<(), (), _>(&mut store, "thunk")?;