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

@@ -2004,12 +2004,13 @@ fn drop_component_still_works() -> Result<()> {
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, 0);
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)?;
(store, instance)
};
@@ -2216,7 +2217,7 @@ fn lower_then_lift() -> Result<()> {
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, ());
let mut linker = Linker::new(&engine);
linker.root().func_wrap("f", || Ok((2u32,)))?;
linker.root().func_wrap("f", |_, _: ()| Ok((2u32,)))?;
let instance = linker.instantiate(&mut store, &component)?;
let f = instance.get_typed_func::<(), (i32,), _>(&mut store, "f")?;
@@ -2252,7 +2253,7 @@ fn lower_then_lift() -> Result<()> {
let mut store = Store::new(&engine, ());
linker
.root()
.func_wrap("s", |store: StoreContextMut<'_, ()>, x: WasmStr| {
.func_wrap("s", |store: StoreContextMut<'_, ()>, (x,): (WasmStr,)| {
assert_eq!(x.to_str(&store)?, "hello");
Ok(())
})?;
@@ -2292,7 +2293,7 @@ fn lower_then_lift() -> Result<()> {
let mut store = Store::new(&engine, ());
linker
.root()
.func_wrap("s2", |store: StoreContextMut<'_, ()>, x: WasmStr| {
.func_wrap("s2", |store: StoreContextMut<'_, ()>, (x,): (WasmStr,)| {
assert_eq!(x.to_str(&store)?, "hello");
Ok((u32::MAX,))
})?;

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")?;

View File

@@ -95,7 +95,7 @@ fn nested_many_instantiations() -> Result<()> {
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("count", |mut store: StoreContextMut<'_, u32>| {
.func_wrap("count", |mut store: StoreContextMut<'_, u32>, _: ()| {
*store.data_mut() += 1;
Ok(())
})?;
@@ -162,7 +162,7 @@ fn thread_options_through_inner() -> Result<()> {
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("hostfn", |param: u32| Ok((param.to_string(),)))?;
.func_wrap("hostfn", |_, (param,): (u32,)| Ok((param.to_string(),)))?;
let instance = linker.instantiate(&mut store, &component)?;
let result = instance
.get_typed_func::<(u32,), (WasmStr,), _>(&mut store, "run")?

View File

@@ -120,13 +120,14 @@ fn invoke_post_return() -> Result<()> {
let component = Component::new(&engine, component)?;
let mut store = Store::new(&engine, false);
let mut linker = Linker::new(&engine);
linker
.root()
.func_wrap("f", |mut store: StoreContextMut<'_, bool>| -> Result<()> {
linker.root().func_wrap(
"f",
|mut store: StoreContextMut<'_, bool>, _: ()| -> Result<()> {
assert!(!*store.data());
*store.data_mut() = true;
Ok(())
})?;
},
)?;
let instance = linker.instantiate(&mut store, &component)?;
let thunk = instance.get_typed_func::<(), (), _>(&mut store, "thunk")?;

View File

@@ -171,12 +171,13 @@ fn test_roundtrip(engine: &Engine, src: &str, dst: &str) -> Result<()> {
let component = Component::new(engine, &component)?;
let mut store = Store::new(engine, String::new());
let mut linker = Linker::new(engine);
linker
.root()
.func_wrap("host", |store: StoreContextMut<String>, arg: String| {
linker.root().func_wrap(
"host",
|store: StoreContextMut<String>, (arg,): (String,)| {
assert_eq!(*store.data(), arg);
Ok((arg,))
})?;
},
)?;
let instance = linker.instantiate(&mut store, &component)?;
let func = instance.get_typed_func::<(String,), (String,), _>(&mut store, "echo")?;