wasmtime: Refactor how imports are resolved (#2102)
This commit removes all import resolution handling from the `wasmtime-jit` crate, instead moving the logic to the `wasmtime` crate. Previously `wasmtime-jit` had a generic `Resolver` trait and would do all the import type matching itself, but with the upcoming module-linking implementation this is going to get much trickier. The goal of this commit is to centralize all meaty "preparation" logic for instantiation into one location, probably the `wasmtime` crate itself. Instantiation will soon involve recursive instantiation and management of alias definitions as well. Having everything in one location, especially with access to `Store` so we can persist instances for safety, will be quite convenient. Additionally the `Resolver` trait isn't really necessary any more since imports are, at the lowest level, provided as a list rather than a map of some kind. More generic resolution functionality is provided via `Linker` or user layers on top of `Instance::new` itself. This makes matching up provided items to expected imports much easier as well. Overall this is largely just moving code around, but most of the code in the previous `resolve_imports` phase can be deleted since a lot of it is handled by surrounding pieces of `wasmtime` as well.
This commit is contained in:
@@ -1,46 +1,22 @@
|
||||
use crate::vmcontext::{VMFunctionImport, VMGlobalImport, VMMemoryImport, VMTableImport};
|
||||
use wasmtime_environ::entity::{BoxedSlice, PrimaryMap};
|
||||
use wasmtime_environ::wasm::{FuncIndex, GlobalIndex, MemoryIndex, TableIndex};
|
||||
|
||||
/// Resolved import pointers.
|
||||
#[derive(Clone)]
|
||||
pub struct Imports {
|
||||
///
|
||||
/// Note that each of these fields are slices, not `PrimaryMap`. They should be
|
||||
/// stored in index-order as with the module that we're providing the imports
|
||||
/// for, and indexing is all done the same way as the main module's index
|
||||
/// spaces.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Imports<'a> {
|
||||
/// Resolved addresses for imported functions.
|
||||
pub functions: BoxedSlice<FuncIndex, VMFunctionImport>,
|
||||
pub functions: &'a [VMFunctionImport],
|
||||
|
||||
/// Resolved addresses for imported tables.
|
||||
pub tables: BoxedSlice<TableIndex, VMTableImport>,
|
||||
pub tables: &'a [VMTableImport],
|
||||
|
||||
/// Resolved addresses for imported memories.
|
||||
pub memories: BoxedSlice<MemoryIndex, VMMemoryImport>,
|
||||
pub memories: &'a [VMMemoryImport],
|
||||
|
||||
/// Resolved addresses for imported globals.
|
||||
pub globals: BoxedSlice<GlobalIndex, VMGlobalImport>,
|
||||
}
|
||||
|
||||
impl Imports {
|
||||
/// Construct a new `Imports` instance.
|
||||
pub fn new(
|
||||
function_imports: PrimaryMap<FuncIndex, VMFunctionImport>,
|
||||
table_imports: PrimaryMap<TableIndex, VMTableImport>,
|
||||
memory_imports: PrimaryMap<MemoryIndex, VMMemoryImport>,
|
||||
global_imports: PrimaryMap<GlobalIndex, VMGlobalImport>,
|
||||
) -> Self {
|
||||
Self {
|
||||
functions: function_imports.into_boxed_slice(),
|
||||
tables: table_imports.into_boxed_slice(),
|
||||
memories: memory_imports.into_boxed_slice(),
|
||||
globals: global_imports.into_boxed_slice(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a new `Imports` instance with no imports.
|
||||
pub fn none() -> Self {
|
||||
Self {
|
||||
functions: PrimaryMap::new().into_boxed_slice(),
|
||||
tables: PrimaryMap::new().into_boxed_slice(),
|
||||
memories: PrimaryMap::new().into_boxed_slice(),
|
||||
globals: PrimaryMap::new().into_boxed_slice(),
|
||||
}
|
||||
}
|
||||
pub globals: &'a [VMGlobalImport],
|
||||
}
|
||||
|
||||
@@ -897,28 +897,36 @@ impl InstanceHandle {
|
||||
};
|
||||
let instance = handle.instance();
|
||||
|
||||
debug_assert_eq!(vmshared_signatures.len(), handle.module().signatures.len());
|
||||
ptr::copy(
|
||||
vmshared_signatures.values().as_slice().as_ptr(),
|
||||
instance.signature_ids_ptr() as *mut VMSharedSignatureIndex,
|
||||
vmshared_signatures.len(),
|
||||
);
|
||||
debug_assert_eq!(imports.functions.len(), handle.module().num_imported_funcs);
|
||||
ptr::copy(
|
||||
imports.functions.values().as_slice().as_ptr(),
|
||||
imports.functions.as_ptr(),
|
||||
instance.imported_functions_ptr() as *mut VMFunctionImport,
|
||||
imports.functions.len(),
|
||||
);
|
||||
debug_assert_eq!(imports.tables.len(), handle.module().num_imported_tables);
|
||||
ptr::copy(
|
||||
imports.tables.values().as_slice().as_ptr(),
|
||||
imports.tables.as_ptr(),
|
||||
instance.imported_tables_ptr() as *mut VMTableImport,
|
||||
imports.tables.len(),
|
||||
);
|
||||
debug_assert_eq!(
|
||||
imports.memories.len(),
|
||||
handle.module().num_imported_memories
|
||||
);
|
||||
ptr::copy(
|
||||
imports.memories.values().as_slice().as_ptr(),
|
||||
imports.memories.as_ptr(),
|
||||
instance.imported_memories_ptr() as *mut VMMemoryImport,
|
||||
imports.memories.len(),
|
||||
);
|
||||
debug_assert_eq!(imports.globals.len(), handle.module().num_imported_globals);
|
||||
ptr::copy(
|
||||
imports.globals.values().as_slice().as_ptr(),
|
||||
imports.globals.as_ptr(),
|
||||
instance.imported_globals_ptr() as *mut VMGlobalImport,
|
||||
imports.globals.len(),
|
||||
);
|
||||
|
||||
@@ -50,6 +50,11 @@ impl SignatureRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
/// Looks up a shared index from the wasm signature itself.
|
||||
pub fn lookup(&self, wasm: &WasmFuncType) -> Option<VMSharedSignatureIndex> {
|
||||
self.wasm2index.get(wasm).cloned()
|
||||
}
|
||||
|
||||
/// Looks up a shared native signature within this registry.
|
||||
///
|
||||
/// Note that for this operation to be semantically correct the `idx` must
|
||||
|
||||
Reference in New Issue
Block a user