Add the ability to cache typechecking an instance (#2962)

* Add the ability to cache typechecking an instance

This commit adds the abilty to cache the type-checked imports of an
instance if an instance is going to be instantiated multiple times. This
can also be useful to do a "dry run" of instantiation where no wasm code
is run but it's double-checked that a `Linker` possesses everything
necessary to instantiate the provided module.

This should ideally help cut down repeated instantiation costs slightly
by avoiding type-checking and allocation a `Vec<Extern>` on each
instantiation. It's expected though that the impact on instantiation
time is quite small and likely not super significant. The functionality,
though, of pre-checking can be useful for some embeddings.

* Fix build with async
This commit is contained in:
Alex Crichton
2021-06-03 17:04:07 -05:00
committed by GitHub
parent e25bf362ab
commit 05baddfb2b
8 changed files with 459 additions and 175 deletions

View File

@@ -308,3 +308,35 @@ fn alias_one() -> Result<()> {
assert!(linker.get(&mut store, "c", Some("d")).is_some());
Ok(())
}
#[test]
fn instance_pre() -> Result<()> {
let engine = Engine::default();
let mut linker = Linker::new(&engine);
linker.func_wrap("", "", || {})?;
let module = Module::new(&engine, r#"(module (import "" "" (func)))"#)?;
let instance_pre = linker.instantiate_pre(&mut Store::new(&engine, ()), &module)?;
instance_pre.instantiate(&mut Store::new(&engine, ()))?;
instance_pre.instantiate(&mut Store::new(&engine, ()))?;
let mut store = Store::new(&engine, ());
let global = Global::new(
&mut store,
GlobalType::new(ValType::I32, Mutability::Const),
1.into(),
)?;
linker.define("", "g", global)?;
let module = Module::new(
&engine,
r#"(module
(import "" "" (func))
(import "" "g" (global i32))
)"#,
)?;
let instance_pre = linker.instantiate_pre(&mut store, &module)?;
instance_pre.instantiate(&mut store)?;
instance_pre.instantiate(&mut store)?;
Ok(())
}