Optimize Func::call and its C API (#3319)

* Optimize `Func::call` and its C API

This commit is an alternative to #3298 which achieves effectively the
same goal of optimizing the `Func::call` API as well as its C API
sibling of `wasmtime_func_call`. The strategy taken here is different
than #3298 though where a new API isn't created, rather a small tweak to
an existing API is done. Specifically this commit handles the major
sources of slowness with `Func::call` with:

* Looking up the type of a function, to typecheck the arguments with and
  use to guide how the results should be loaded, no longer hits the
  rwlock in the `Engine` but instead each `Func` contains its own
  `FuncType`. This can be an unnecessary allocation for funcs not used
  with `Func::call`, so this is a downside of this implementation
  relative to #3298. A mitigating factor, though, is that instance
  exports are loaded lazily into the `Store` and in theory not too many
  funcs are active in the store as `Func` objects.

* Temporary storage is amortized with a long-lived `Vec` in the `Store`
  rather than allocating a new vector on each call. This is basically
  the same strategy as #3294 only applied to different types in
  different places. Specifically `wasmtime::Store` now retains a
  `Vec<u128>` for `Func::call`, and the C API retains a `Vec<Val>` for
  calling `Func::call`.

* Finally, an API breaking change is made to `Func::call` and its type
  signature (as well as `Func::call_async`). Instead of returning
  `Box<[Val]>` as it did before this function now takes a
  `results: &mut [Val]` parameter. This allows the caller to manage the
  allocation and we can amortize-remove it in `wasmtime_func_call` by
  using space after the parameters in the `Vec<Val>` we're passing in.
  This change is naturally a breaking change and we'll want to consider
  it carefully, but mitigating factors are that most embeddings are
  likely using `TypedFunc::call` instead and this signature taking a
  mutable slice better aligns with `Func::new` which receives a mutable
  slice for the results.

Overall this change, in the benchmark of "call a nop function from the C
API" is not quite as good as #3298. It's still a bit slower, on the
order of 15ns, because there's lots of capacity checks around vectors
and the type checks are slightly less optimized than before. Overall
though this is still significantly better than today because allocations
and the rwlock to acquire the type information are both avoided. I
personally feel that this change is the best to do because it has less
of an API impact than #3298.

* Rebase issues
This commit is contained in:
Alex Crichton
2021-09-21 14:07:05 -05:00
committed by GitHub
parent 38463d11ed
commit bcf3544924
26 changed files with 399 additions and 253 deletions

View File

@@ -9,8 +9,8 @@ fn async_store() -> Store<()> {
}
fn run_smoke_test(store: &mut Store<()>, func: Func) {
run(func.call_async(&mut *store, &[])).unwrap();
run(func.call_async(&mut *store, &[])).unwrap();
run(func.call_async(&mut *store, &[], &mut [])).unwrap();
run(func.call_async(&mut *store, &[], &mut [])).unwrap();
}
fn run_smoke_typed_test(store: &mut Store<()>, func: Func) {
@@ -159,7 +159,9 @@ fn recursive_call() {
FuncType::new(None, None),
move |mut caller, _params, _results| {
Box::new(async move {
async_wasm_func.call_async(&mut caller, &[]).await?;
async_wasm_func
.call_async(&mut caller, &[], &mut [])
.await?;
Ok(())
})
},
@@ -184,7 +186,7 @@ fn recursive_call() {
run(async {
let instance = Instance::new_async(&mut store, &module, &[func2.into()]).await?;
let func = instance.get_func(&mut store, "").unwrap();
func.call_async(&mut store, &[]).await
func.call_async(&mut store, &[], &mut []).await
})
.unwrap();
}
@@ -209,7 +211,7 @@ fn suspend_while_suspending() {
&mut store,
FuncType::new(None, None),
move |mut caller, _params, _results| {
run(async_thunk.call_async(&mut caller, &[]))?;
run(async_thunk.call_async(&mut caller, &[], &mut []))?;
Ok(())
},
);
@@ -249,7 +251,7 @@ fn suspend_while_suspending() {
)
.await?;
let func = instance.get_func(&mut store, "").unwrap();
func.call_async(&mut store, &[]).await
func.call_async(&mut store, &[], &mut []).await
})
.unwrap();
}
@@ -277,7 +279,7 @@ fn cancel_during_run() {
// Create our future, but as per async conventions this still doesn't
// actually do anything. No wasm or host function has been called yet.
let mut future = Pin::from(Box::new(async_thunk.call_async(&mut store, &[])));
let mut future = Pin::from(Box::new(async_thunk.call_async(&mut store, &[], &mut [])));
// Push the future forward one tick, which actually runs the host code in
// our async func. Our future is designed to be pending once, however.
@@ -608,7 +610,7 @@ fn resume_separate_thread3() {
// restored even though the asynchronous execution is suspended.
Err::<(), _>(wasmtime::Trap::new(""))
});
assert!(f.call(&mut store, &[]).is_err());
assert!(f.call(&mut store, &[], &mut []).is_err());
}
#[test]
@@ -636,7 +638,7 @@ fn recursive_async() -> Result<()> {
Ok(())
})
});
run(f2.call_async(&mut store, &[]))?;
run(f2.call_async(&mut store, &[], &mut []))?;
Ok(())
}

View File

@@ -31,6 +31,7 @@ fn call_wrapped_func() -> Result<(), Error> {
f.call(
&mut store,
&[Val::I32(1), Val::I64(2), 3.0f32.into(), 4.0f64.into()],
&mut [],
)?;
// One switch from vm to host to call f, another in return from f.
@@ -85,6 +86,7 @@ async fn call_wrapped_async_func() -> Result<(), Error> {
f.call_async(
&mut store,
&[Val::I32(1), Val::I64(2), 3.0f32.into(), 4.0f64.into()],
&mut [],
)
.await?;
@@ -154,7 +156,7 @@ fn call_linked_func() -> Result<(), Error> {
.into_func()
.expect("export is func");
export.call(&mut store, &[])?;
export.call(&mut store, &[], &mut [])?;
// One switch from vm to host to call f, another in return from f.
assert_eq!(store.data().calls_into_host, 1);
@@ -225,7 +227,7 @@ async fn call_linked_func_async() -> Result<(), Error> {
.into_func()
.expect("export is func");
export.call_async(&mut store, &[]).await?;
export.call_async(&mut store, &[], &mut []).await?;
// One switch from vm to host to call f, another in return from f.
assert_eq!(store.data().calls_into_host, 1);
@@ -333,7 +335,7 @@ fn recursion() -> Result<(), Error> {
// Recursion depth:
let n: usize = 10;
export.call(&mut store, &[Val::I32(n as i32)])?;
export.call(&mut store, &[Val::I32(n as i32)], &mut [])?;
// Recurse down to 0: n+1 calls
assert_eq!(store.data().calls_into_host, n + 1);
@@ -423,6 +425,7 @@ fn trapping() -> Result<(), Error> {
let r = export.call(
&mut store,
&[Val::I32(action), Val::I32(if recur { 1 } else { 0 })],
&mut [],
);
(store.into_data(), r.err())
};

View File

@@ -114,16 +114,24 @@ fn cross_store() -> anyhow::Result<()> {
let s1_f = s1_inst.get_func(&mut store1, "f").unwrap();
let s2_f = s2_inst.get_func(&mut store2, "f").unwrap();
assert!(s1_f.call(&mut store1, &[Val::FuncRef(None)]).is_ok());
assert!(s2_f.call(&mut store2, &[Val::FuncRef(None)]).is_ok());
assert!(s1_f.call(&mut store1, &[Some(s1_f.clone()).into()]).is_ok());
assert!(s1_f
.call(&mut store1, &[Some(s2_f.clone()).into()])
.call(&mut store1, &[Val::FuncRef(None)], &mut [])
.is_ok());
assert!(s2_f
.call(&mut store2, &[Val::FuncRef(None)], &mut [])
.is_ok());
assert!(s1_f
.call(&mut store1, &[Some(s1_f.clone()).into()], &mut [])
.is_ok());
assert!(s1_f
.call(&mut store1, &[Some(s2_f.clone()).into()], &mut [])
.is_err());
assert!(s2_f
.call(&mut store2, &[Some(s1_f.clone()).into()])
.call(&mut store2, &[Some(s1_f.clone()).into()], &mut [])
.is_err());
assert!(s2_f.call(&mut store2, &[Some(s2_f.clone()).into()]).is_ok());
assert!(s2_f
.call(&mut store2, &[Some(s2_f.clone()).into()], &mut [])
.is_ok());
let s1_f_t = s1_f.typed::<Option<Func>, (), _>(&store1)?;
let s2_f_t = s2_f.typed::<Option<Func>, (), _>(&store2)?;

View File

@@ -193,10 +193,12 @@ fn import_works() -> Result<()> {
f.as_ref().unwrap().data().downcast_ref::<String>().unwrap(),
"hello"
);
assert_eq!(
g.as_ref().unwrap().call(&mut caller, &[]).unwrap()[0].unwrap_i32(),
42
);
let mut results = [Val::I32(0)];
g.as_ref()
.unwrap()
.call(&mut caller, &[], &mut results)
.unwrap();
assert_eq!(results[0].unwrap_i32(), 42);
assert_eq!(HITS.fetch_add(1, SeqCst), 3);
},
)
@@ -211,6 +213,7 @@ fn import_works() -> Result<()> {
Val::ExternRef(Some(ExternRef::new("hello".to_string()))),
funcref,
],
&mut [],
)?;
assert_eq!(HITS.load(SeqCst), 4);
Ok(())
@@ -222,7 +225,10 @@ fn trap_smoke() -> Result<()> {
let f = Func::wrap(&mut store, || -> Result<(), Trap> {
Err(Trap::new("test"))
});
let err = f.call(&mut store, &[]).unwrap_err().downcast::<Trap>()?;
let err = f
.call(&mut store, &[], &mut [])
.unwrap_err()
.downcast::<Trap>()?;
assert!(err.to_string().contains("test"));
assert!(err.i32_exit_status().is_none());
Ok(())
@@ -347,31 +353,29 @@ fn call_wrapped_func() -> Result<()> {
f.call(
&mut store,
&[Val::I32(1), Val::I64(2), 3.0f32.into(), 4.0f64.into()],
&mut [],
)?;
f.typed::<(i32, i64, f32, f64), (), _>(&store)?
.call(&mut store, (1, 2, 3.0, 4.0))?;
let mut results = [Val::I32(0)];
let f = Func::wrap(&mut store, || 1i32);
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_i32(), 1);
assert_eq!(f.typed::<(), i32, _>(&store)?.call(&mut store, ())?, 1);
let f = Func::wrap(&mut store, || 2i64);
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_i64(), 2);
assert_eq!(f.typed::<(), i64, _>(&store)?.call(&mut store, ())?, 2);
let f = Func::wrap(&mut store, || 3.0f32);
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_f32(), 3.0);
assert_eq!(f.typed::<(), f32, _>(&store)?.call(&mut store, ())?, 3.0);
let f = Func::wrap(&mut store, || 4.0f64);
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_f64(), 4.0);
assert_eq!(f.typed::<(), f64, _>(&store)?.call(&mut store, ())?, 4.0);
Ok(())
@@ -385,7 +389,7 @@ fn caller_memory() -> anyhow::Result<()> {
assert!(c.get_export("y").is_none());
assert!(c.get_export("z").is_none());
});
f.call(&mut store, &[])?;
f.call(&mut store, &[], &mut [])?;
let f = Func::wrap(&mut store, |mut c: Caller<'_, ()>| {
assert!(c.get_export("x").is_none());
@@ -447,7 +451,10 @@ fn func_write_nothing() -> anyhow::Result<()> {
let mut store = Store::<()>::default();
let ty = FuncType::new(None, Some(ValType::I32));
let f = Func::new(&mut store, ty, |_, _, _| Ok(()));
let err = f.call(&mut store, &[]).unwrap_err().downcast::<Trap>()?;
let err = f
.call(&mut store, &[], &mut [Val::I32(0)])
.unwrap_err()
.downcast::<Trap>()?;
assert!(err
.to_string()
.contains("function attempted to return an incompatible value"));
@@ -479,7 +486,7 @@ fn return_cross_store_value() -> anyhow::Result<()> {
let instance = Instance::new(&mut store1, &module, &[return_cross_store_func.into()])?;
let run = instance.get_func(&mut store1, "run").unwrap();
let result = run.call(&mut store1, &[]);
let result = run.call(&mut store1, &[], &mut [Val::I32(0)]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("cross-`Store`"));
@@ -500,7 +507,11 @@ fn pass_cross_store_arg() -> anyhow::Result<()> {
// Using regular `.call` fails with cross-Store arguments.
assert!(store1_func
.call(&mut store1, &[Val::FuncRef(Some(store2_func.clone()))])
.call(
&mut store1,
&[Val::FuncRef(Some(store2_func.clone()))],
&mut []
)
.is_err());
// And using `.get` followed by a function call also fails with cross-Store
@@ -553,7 +564,7 @@ fn trampolines_always_valid() -> anyhow::Result<()> {
drop(module2);
// ... and no segfaults! right? right? ...
func.call(&mut store, &[])?;
func.call(&mut store, &[], &mut [])?;
Ok(())
}
@@ -616,7 +627,7 @@ fn trap_doesnt_leak() -> anyhow::Result<()> {
Err(Trap::new(""))
});
assert!(f1.typed::<(), (), _>(&store)?.call(&mut store, ()).is_err());
assert!(f1.call(&mut store, &[]).is_err());
assert!(f1.call(&mut store, &[], &mut []).is_err());
// test that `Func::new` is correct
let canary2 = Canary::default();
@@ -626,7 +637,7 @@ fn trap_doesnt_leak() -> anyhow::Result<()> {
Err(Trap::new(""))
});
assert!(f2.typed::<(), (), _>(&store)?.call(&mut store, ()).is_err());
assert!(f2.call(&mut store, &[]).is_err());
assert!(f2.call(&mut store, &[], &mut []).is_err());
// drop everything and ensure dtors are run
drop(store);
@@ -651,15 +662,18 @@ fn wrap_multiple_results() -> anyhow::Result<()> {
+ Sync,
{
let f = Func::wrap(&mut *store, move || t);
assert_eq!(f.typed::<(), T, _>(&store,)?.call(&mut *store, ())?, t);
assert!(t.eq_values(&f.call(&mut *store, &[])?));
let mut results = vec![Val::I32(0); f.ty(&store).results().len()];
assert_eq!(f.typed::<(), T, _>(&store)?.call(&mut *store, ())?, t);
f.call(&mut *store, &[], &mut results)?;
assert!(t.eq_values(&results));
let module = Module::new(store.engine(), &T::gen_wasm())?;
let instance = Instance::new(&mut *store, &module, &[f.into()])?;
let f = instance.get_func(&mut *store, "foo").unwrap();
assert_eq!(f.typed::<(), T, _>(&store)?.call(&mut *store, ())?, t);
assert!(t.eq_values(&f.call(&mut *store, &[])?));
f.call(&mut *store, &[], &mut results)?;
assert!(t.eq_values(&results));
Ok(())
}
@@ -820,7 +834,7 @@ fn trampoline_for_declared_elem() -> anyhow::Result<()> {
let g = instance.get_typed_func::<(), Option<Func>, _>(&mut store, "g")?;
let func = g.call(&mut store, ())?;
func.unwrap().call(&mut store, &[])?;
func.unwrap().call(&mut store, &[], &mut [])?;
Ok(())
}

View File

@@ -20,8 +20,12 @@ fn pass_funcref_in_and_out_of_wasm() -> anyhow::Result<()> {
// Pass in a non-null funcref.
{
let results = func.call(&mut store, &[Val::FuncRef(Some(func.clone()))])?;
assert_eq!(results.len(), 1);
let mut results = [Val::I32(0)];
func.call(
&mut store,
&[Val::FuncRef(Some(func.clone()))],
&mut results,
)?;
// Can't compare `Func` for equality, so this is the best we can do here.
let result_func = results[0].unwrap_funcref().unwrap();
@@ -30,9 +34,8 @@ fn pass_funcref_in_and_out_of_wasm() -> anyhow::Result<()> {
// Pass in a null funcref.
{
let results = func.call(&mut store, &[Val::FuncRef(None)])?;
assert_eq!(results.len(), 1);
let mut results = [Val::I32(0)];
func.call(&mut store, &[Val::FuncRef(None)], &mut results)?;
let result_func = results[0].unwrap_funcref();
assert!(result_func.is_none());
}
@@ -42,9 +45,11 @@ fn pass_funcref_in_and_out_of_wasm() -> anyhow::Result<()> {
let other_instance = Instance::new(&mut store, &module, &[])?;
let other_instance_func = other_instance.get_func(&mut store, "func").unwrap();
let results = func.call(
let mut results = [Val::I32(0)];
func.call(
&mut store,
&[Val::FuncRef(Some(other_instance_func.clone()))],
&mut results,
)?;
assert_eq!(results.len(), 1);
@@ -61,7 +66,9 @@ fn pass_funcref_in_and_out_of_wasm() -> anyhow::Result<()> {
.get_func(&mut other_store, "f")
.unwrap();
assert!(func.call(&mut store, &[Val::FuncRef(Some(f))]).is_err());
assert!(func
.call(&mut store, &[Val::FuncRef(Some(f))], &mut [Val::I32(0)])
.is_err());
}
Ok(())
@@ -82,9 +89,8 @@ fn receive_null_funcref_from_wasm() -> anyhow::Result<()> {
let instance = Instance::new(&mut store, &module, &[])?;
let get_null = instance.get_func(&mut store, "get-null").unwrap();
let results = get_null.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
let mut results = [Val::I32(0)];
get_null.call(&mut store, &[], &mut results)?;
let result_func = results[0].unwrap_funcref();
assert!(result_func.is_none());
@@ -101,7 +107,7 @@ fn wrong_store() -> anyhow::Result<()> {
let set = SetOnDrop(dropped.clone());
let f1 = Func::wrap(&mut store1, move || drop(&set));
let f2 = Func::wrap(&mut store2, move || Some(f1.clone()));
assert!(f2.call(&mut store2, &[]).is_err());
assert!(f2.call(&mut store2, &[], &mut []).is_err());
}
assert!(dropped.load(SeqCst));
@@ -133,7 +139,7 @@ fn func_new_returns_wrong_store() -> anyhow::Result<()> {
Ok(())
},
);
assert!(f2.call(&mut store2, &[]).is_err());
assert!(f2.call(&mut store2, &[], &mut [Val::I32(0)]).is_err());
}
assert!(dropped.load(SeqCst));

View File

@@ -47,7 +47,7 @@ fn smoke_test_gc() -> anyhow::Result<()> {
let r = ExternRef::new(SetFlagOnDrop(inner_dropped.clone()));
{
let args = [Val::I32(5), Val::ExternRef(Some(r.clone()))];
func.call(&mut store, &args)?;
func.call(&mut store, &args, &mut [Val::I32(0)])?;
}
// Still held alive by the `VMExternRefActivationsTable` (potentially in
@@ -88,7 +88,7 @@ fn wasm_dropping_refs() -> anyhow::Result<()> {
for _ in 0..4096 {
let r = ExternRef::new(CountDrops(num_refs_dropped.clone()));
let args = [Val::ExternRef(Some(r))];
drop_ref.call(&mut store, &args)?;
drop_ref.call(&mut store, &args, &mut [])?;
}
assert!(num_refs_dropped.load(SeqCst) > 0);
@@ -163,7 +163,7 @@ fn many_live_refs() -> anyhow::Result<()> {
let instance = Instance::new(&mut store, &module, &[make_ref.into(), observe_ref.into()])?;
let many_live_refs = instance.get_func(&mut store, "many_live_refs").unwrap();
many_live_refs.call(&mut store, &[])?;
many_live_refs.call(&mut store, &[], &mut [])?;
store.gc();
assert_eq!(live_refs.load(SeqCst), 0);
@@ -214,7 +214,7 @@ fn drop_externref_via_table_set() -> anyhow::Result<()> {
{
let args = vec![Val::ExternRef(Some(foo))];
table_set.call(&mut store, &args)?;
table_set.call(&mut store, &args, &mut [])?;
}
store.gc();
assert!(!foo_is_dropped.load(SeqCst));
@@ -222,13 +222,13 @@ fn drop_externref_via_table_set() -> anyhow::Result<()> {
{
let args = vec![Val::ExternRef(Some(bar))];
table_set.call(&mut store, &args)?;
table_set.call(&mut store, &args, &mut [])?;
}
store.gc();
assert!(foo_is_dropped.load(SeqCst));
assert!(!bar_is_dropped.load(SeqCst));
table_set.call(&mut store, &[Val::ExternRef(None)])?;
table_set.call(&mut store, &[Val::ExternRef(None)], &mut [])?;
assert!(foo_is_dropped.load(SeqCst));
assert!(bar_is_dropped.load(SeqCst));

View File

@@ -279,10 +279,12 @@ fn import_works() -> Result<()> {
f.as_ref().unwrap().data().downcast_ref::<String>().unwrap(),
"hello"
);
assert_eq!(
g.as_ref().unwrap().call(&mut caller, &[]).unwrap()[0].unwrap_i32(),
42
);
let mut results = [Val::I32(0)];
g.as_ref()
.unwrap()
.call(&mut caller, &[], &mut results)
.unwrap();
assert_eq!(results[0].unwrap_i32(), 42);
assert_eq!(HITS.fetch_add(1, SeqCst), 3);
},
)?;
@@ -299,6 +301,7 @@ fn import_works() -> Result<()> {
Val::ExternRef(Some(ExternRef::new("hello".to_string()))),
funcref,
],
&mut [],
)?;
assert_eq!(HITS.load(SeqCst), 4);
@@ -360,7 +363,7 @@ fn call_import_many_args() -> Result<()> {
let mut store = Store::new(&engine, ());
let instance = linker.instantiate(&mut store, &module)?;
let run = instance.get_func(&mut store, "run").unwrap();
run.call(&mut store, &[])?;
run.call(&mut store, &[], &mut [])?;
Ok(())
}
@@ -422,6 +425,7 @@ fn call_wasm_many_args() -> Result<()> {
9.into(),
10.into(),
],
&mut [],
)?;
let typed_run = instance
@@ -431,7 +435,7 @@ fn call_wasm_many_args() -> Result<()> {
typed_run.call(&mut store, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))?;
let test = instance.get_func(&mut store, "test").unwrap();
test.call(&mut store, &[])?;
test.call(&mut store, &[], &mut [])?;
Ok(())
}
@@ -450,7 +454,10 @@ fn trap_smoke() -> Result<()> {
.into_func()
.unwrap();
let err = f.call(&mut store, &[]).unwrap_err().downcast::<Trap>()?;
let err = f
.call(&mut store, &[], &mut [])
.unwrap_err()
.downcast::<Trap>()?;
assert!(err.to_string().contains("test"));
assert!(err.i32_exit_status().is_none());
@@ -524,6 +531,7 @@ fn new_from_signature() -> Result<()> {
fn call_wrapped_func() -> Result<()> {
let engine = Engine::default();
let mut linker = Linker::new(&engine);
let mut results = [Val::I32(0)];
linker.func_wrap("", "f1", |a: i32, b: i64, c: f32, d: f64| {
assert_eq!(a, 1);
@@ -550,6 +558,7 @@ fn call_wrapped_func() -> Result<()> {
f.call(
&mut store,
&[Val::I32(1), Val::I64(2), 3.0f32.into(), 4.0f64.into()],
&mut [],
)?;
f.typed::<(i32, i64, f32, f64), (), _>(&store)?
.call(&mut store, (1, 2, 3.0, 4.0))?;
@@ -559,8 +568,7 @@ fn call_wrapped_func() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_i32(), 1);
assert_eq!(f.typed::<(), i32, _>(&store)?.call(&mut store, ())?, 1);
@@ -569,8 +577,7 @@ fn call_wrapped_func() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_i64(), 2);
assert_eq!(f.typed::<(), i64, _>(&store)?.call(&mut store, ())?, 2);
@@ -579,8 +586,7 @@ fn call_wrapped_func() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_f32(), 3.0);
assert_eq!(f.typed::<(), f32, _>(&store)?.call(&mut store, ())?, 3.0);
@@ -589,8 +595,7 @@ fn call_wrapped_func() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
let results = f.call(&mut store, &[])?;
assert_eq!(results.len(), 1);
f.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].unwrap_f64(), 4.0);
assert_eq!(f.typed::<(), f64, _>(&store)?.call(&mut store, ())?, 4.0);
@@ -610,7 +615,10 @@ fn func_return_nothing() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
let err = f.call(&mut store, &[]).unwrap_err().downcast::<Trap>()?;
let err = f
.call(&mut store, &[], &mut [Val::I32(0)])
.unwrap_err()
.downcast::<Trap>()?;
assert!(err
.to_string()
.contains("function attempted to return an incompatible value"));
@@ -658,18 +666,17 @@ fn call_via_funcref() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
let results = instance
let mut results = [Val::I32(0), Val::I32(0)];
instance
.get_func(&mut store, "call")
.unwrap()
.call(&mut store, &[f.into()])?;
assert_eq!(results.len(), 2);
.call(&mut store, &[f.into()], &mut results)?;
assert_eq!(results[0].unwrap_i32(), 7);
{
let f = results[1].unwrap_funcref().unwrap();
let results = f.call(&mut store, &[1.into(), 2.into()])?;
assert_eq!(results.len(), 1);
let mut results = [Val::I32(0)];
f.call(&mut store, &[1.into(), 2.into()], &mut results)?;
assert_eq!(results[0].unwrap_i32(), 3);
}
@@ -706,7 +713,7 @@ fn store_with_context() -> Result<()> {
.unwrap()
.into_func()
.unwrap();
f.call(&mut store, &[])?;
f.call(&mut store, &[], &mut [])?;
assert!(store.data().called);

View File

@@ -24,7 +24,7 @@ fn test_import_calling_export() {
caller
.data()
.unwrap()
.call(&mut caller, &[])
.call(&mut caller, &[], &mut [])
.expect("expected function not to trap");
Ok(())
},
@@ -44,7 +44,7 @@ fn test_import_calling_export() {
*store.data_mut() = Some(other_func);
run_func
.call(&mut store, &[])
.call(&mut store, &[], &mut [])
.expect("expected function not to trap");
}
@@ -79,8 +79,9 @@ fn test_returns_incorrect_type() -> Result<()> {
.get_func(&mut store, "run")
.expect("expected a run func in the module");
let mut result = [Val::I32(0)];
let trap = run_func
.call(&mut store, &[])
.call(&mut store, &[], &mut result)
.expect_err("the execution should fail")
.downcast::<Trap>()?;
assert!(trap

View File

@@ -26,7 +26,8 @@ fn test_invoke_func_via_table() -> Result<()> {
.unwrap()
.unwrap()
.clone();
let result = f.call(&mut store, &[]).unwrap();
assert_eq!(result[0].unwrap_i64(), 42);
let mut results = [Val::I32(0)];
f.call(&mut store, &[], &mut results).unwrap();
assert_eq!(results[0].unwrap_i64(), 42);
Ok(())
}

View File

@@ -285,7 +285,7 @@ fn funcs_live_on_to_fight_another_day() -> Result<()> {
assert_eq!(flag.load(SeqCst), 0);
let mut store = Store::new(&engine, ());
let func = linker.get(&mut store, "", Some("")).unwrap();
func.into_func().unwrap().call(&mut store, &[])?;
func.into_func().unwrap().call(&mut store, &[], &mut [])?;
assert_eq!(flag.load(SeqCst), 0);
Ok(())
};

View File

@@ -329,17 +329,17 @@ fn mismatched_arguments() -> Result<()> {
let instance = Instance::new(&mut store, &module, &[])?;
let func = instance.get_func(&mut store, "foo").unwrap();
assert_eq!(
func.call(&mut store, &[]).unwrap_err().to_string(),
func.call(&mut store, &[], &mut []).unwrap_err().to_string(),
"expected 1 arguments, got 0"
);
assert_eq!(
func.call(&mut store, &[Val::F32(0)])
func.call(&mut store, &[Val::F32(0)], &mut [])
.unwrap_err()
.to_string(),
"argument type mismatch: found f32 but expected i32",
);
assert_eq!(
func.call(&mut store, &[Val::I32(0), Val::I32(1)])
func.call(&mut store, &[Val::I32(0), Val::I32(1)], &mut [])
.unwrap_err()
.to_string(),
"expected 1 arguments, got 2"
@@ -519,7 +519,10 @@ fn parse_dwarf_info() -> Result<()> {
);
linker.module(&mut store, "", &module)?;
let run = linker.get_default(&mut store, "")?;
let trap = run.call(&mut store, &[]).unwrap_err().downcast::<Trap>()?;
let trap = run
.call(&mut store, &[], &mut [])
.unwrap_err()
.downcast::<Trap>()?;
let mut found = false;
for frame in trap.trace() {

View File

@@ -139,7 +139,7 @@ fn main() {
overrun_the_stack();
})
});
run_future(f.call_async(&mut store, &[])).unwrap();
run_future(f.call_async(&mut store, &[], &mut [])).unwrap();
unreachable!();
},
true,
@@ -157,7 +157,7 @@ fn main() {
overrun_the_stack();
})
});
run_future(f.call_async(&mut store, &[])).unwrap();
run_future(f.call_async(&mut store, &[], &mut [])).unwrap();
unreachable!();
},
true,