Helper function shared between `InstancePre::{instantiate,instantiate_async}` This is an out-of-line function to avoid the generic on `InstancePre` and get this compiled into the `wasmtime` crate to avoid having it monomorphized elsewhere.
(
store: &mut StoreOpaque,
module: &Module,
items: &Arc<TryVec<Definition>>,
host_funcs: usize,
func_refs: &Arc<TryVec<VMFuncRef>>,
asyncness: Asyncness,
)
| 960 | /// get this compiled into the `wasmtime` crate to avoid having it monomorphized |
| 961 | /// elsewhere. |
| 962 | fn pre_instantiate_raw( |
| 963 | store: &mut StoreOpaque, |
| 964 | module: &Module, |
| 965 | items: &Arc<TryVec<Definition>>, |
| 966 | host_funcs: usize, |
| 967 | func_refs: &Arc<TryVec<VMFuncRef>>, |
| 968 | asyncness: Asyncness, |
| 969 | ) -> Result<OwnedImports> { |
| 970 | // Register this module and use it to fill out any funcref wasm_call holes |
| 971 | // we can. For more comments on this see `typecheck_externs`. |
| 972 | let (modules, engine, breakpoints) = store.modules_and_engine_and_breakpoints_mut(); |
| 973 | modules.register_module(module, engine, breakpoints)?; |
| 974 | let (funcrefs, modules) = store.func_refs_and_modules(); |
| 975 | funcrefs.fill(modules); |
| 976 | |
| 977 | if host_funcs > 0 { |
| 978 | // Any linker-defined function of the `Definition::HostFunc` variant |
| 979 | // will insert a function into the store automatically as part of |
| 980 | // instantiation, so reserve space here to make insertion more efficient |
| 981 | // as it won't have to realloc during the instantiation. |
| 982 | funcrefs.reserve_storage(host_funcs)?; |
| 983 | |
| 984 | // The usage of `to_extern_store_rooted` requires that the items are |
| 985 | // rooted via another means, which happens here by cloning the list of |
| 986 | // items into the store once. This avoids cloning each individual item |
| 987 | // below. |
| 988 | funcrefs.push_instance_pre_definitions(items.clone())?; |
| 989 | funcrefs.push_instance_pre_func_refs(func_refs.clone())?; |
| 990 | } |
| 991 | |
| 992 | store.set_async_required(asyncness); |
| 993 | |
| 994 | let mut func_refs = func_refs.iter().map(|f| NonNull::from(f)); |
| 995 | let mut imports = OwnedImports::new(module)?; |
| 996 | for import in items.iter() { |
| 997 | if !import.comes_from_same_store(store) { |
| 998 | bail!("cross-`Store` instantiation is not currently supported"); |
| 999 | } |
| 1000 | // This unsafety should be encapsulated in the constructor of |
| 1001 | // `InstancePre` where the `T` of the original item should match the |
| 1002 | // `T` of the store. Additionally the rooting necessary has happened |
| 1003 | // above. |
| 1004 | let item = match import { |
| 1005 | Definition::Extern(e, _) => e.clone(), |
| 1006 | Definition::HostFunc(func) => unsafe { |
| 1007 | func.to_func_store_rooted( |
| 1008 | store, |
| 1009 | if func.func_ref().wasm_call.is_none() { |
| 1010 | Some(func_refs.next().unwrap()) |
| 1011 | } else { |
| 1012 | None |
| 1013 | }, |
| 1014 | ) |
| 1015 | .into() |
| 1016 | }, |
| 1017 | }; |
| 1018 | imports.push(&item, store)?; |
| 1019 | } |
no test coverage detected