Add dynamic sigindices to the vm context.

This commit is contained in:
Lachlan Sneff 2019-03-03 21:26:34 -08:00
parent 51cf9dde05
commit 801979c40d
2 changed files with 33 additions and 2 deletions

View File

@ -4,13 +4,14 @@ use crate::{
global::Global,
import::ImportObject,
memory::Memory,
module::{ImportName, ModuleInner},
module::{ImportName, ModuleInfo, ModuleInner},
sig_registry::SigRegistry,
structures::{BoxedMap, Map, SliceMap, TypedIndex},
table::Table,
types::{
ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex,
Initializer, LocalGlobalIndex, LocalMemoryIndex, LocalOrImport, LocalTableIndex, Value,
Initializer, LocalGlobalIndex, LocalMemoryIndex, LocalOrImport, LocalTableIndex, SigIndex,
Value,
},
vm,
};
@ -25,6 +26,8 @@ pub struct LocalBacking {
pub(crate) vm_memories: BoxedMap<LocalMemoryIndex, *mut vm::LocalMemory>,
pub(crate) vm_tables: BoxedMap<LocalTableIndex, *mut vm::LocalTable>,
pub(crate) vm_globals: BoxedMap<LocalGlobalIndex, *mut vm::LocalGlobal>,
pub(crate) dynamic_sigindices: BoxedMap<SigIndex, vm::SigId>,
}
// impl LocalBacking {
@ -47,6 +50,8 @@ impl LocalBacking {
let vm_tables = Self::finalize_tables(module, imports, &mut tables, vmctx);
let vm_globals = Self::finalize_globals(&mut globals);
let dynamic_sigindices = Self::generate_sigindices(&module.info);
Self {
memories,
tables,
@ -55,9 +60,23 @@ impl LocalBacking {
vm_memories,
vm_tables,
vm_globals,
dynamic_sigindices,
}
}
fn generate_sigindices(info: &ModuleInfo) -> BoxedMap<SigIndex, vm::SigId> {
info.signatures
.iter()
.map(|(_, signature)| {
let signature = SigRegistry.lookup_signature_ref(signature);
let sig_index = SigRegistry.lookup_sig_index(signature);
vm::SigId(sig_index.index() as u32)
})
.collect::<Map<_, _>>()
.into_boxed_map()
}
fn generate_memories(module: &ModuleInner) -> BoxedMap<LocalMemoryIndex, Memory> {
let mut memories = Map::with_capacity(module.info.memories.len());
for (_, &desc) in &module.info.memories {

View File

@ -34,6 +34,12 @@ pub struct Ctx {
/// A pointer to an array of imported functions, indexed by `FuncIndex`.
pub(crate) imported_funcs: *mut ImportedFunc,
/// A pointer to an array of signature ids. Conceptually, this maps
/// from a static, module-local signature id to a runtime-global
/// signature id. This is used to allow call-indirect to other
/// modules safely.
pub(crate) dynamic_sigindices: *const SigId,
local_backing: *mut LocalBacking,
import_backing: *mut ImportBacking,
module: *const ModuleInner,
@ -59,6 +65,8 @@ impl Ctx {
imported_globals: import_backing.vm_globals.as_mut_ptr(),
imported_funcs: import_backing.vm_functions.as_mut_ptr(),
dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(),
local_backing,
import_backing,
module,
@ -86,6 +94,8 @@ impl Ctx {
imported_globals: import_backing.vm_globals.as_mut_ptr(),
imported_funcs: import_backing.vm_functions.as_mut_ptr(),
dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(),
local_backing,
import_backing,
module,
@ -468,6 +478,8 @@ mod vm_ctx_tests {
vm_memories: Map::new().into_boxed_map(),
vm_tables: Map::new().into_boxed_map(),
vm_globals: Map::new().into_boxed_map(),
dynamic_sigindices: Map::new().into_boxed_map(),
};
let module = generate_module();
let data = &mut data as *mut _ as *mut c_void;