Change order of Map type params and globalinit to Initializer

This commit is contained in:
Lachlan Sneff 2018-12-28 16:40:40 -05:00
parent 696bc9a0a9
commit a5b4cc21f6
4 changed files with 31 additions and 28 deletions

View File

@ -4,7 +4,7 @@ use crate::runtime::{
module::{ImportName, Module},
sig_registry::SigRegistry,
table::{TableBacking, TableElements},
types::{GlobalInit, MapIndex, Val},
types::{Initializer, MapIndex, Val},
vm,
};
@ -149,11 +149,11 @@ impl LocalBacking {
) -> Box<[vm::LocalGlobal]> {
for (to, (_, from)) in globals.iter_mut().zip(module.globals.into_iter()) {
to.data = match from.init {
GlobalInit::Val(Val::I32(x)) => x as u64,
GlobalInit::Val(Val::I64(x)) => x as u64,
GlobalInit::Val(Val::F32(x)) => x as u64,
GlobalInit::Val(Val::F64(x)) => x,
GlobalInit::GetGlobal(index) => (imports.globals[index.index()].global).data,
Initializer::Const(Val::I32(x)) => x as u64,
Initializer::Const(Val::I64(x)) => x as u64,
Initializer::Const(Val::F32(x)) => x as u64,
Initializer::Const(Val::F64(x)) => x,
Initializer::GetGlobal(index) => (imports.globals[index.index()].global).data,
};
}

View File

@ -11,14 +11,14 @@ use std::ptr::NonNull;
/// This is used to instantiate a new webassembly module.
pub struct Module {
pub function_resolver: Box<dyn Fn(&Module, FuncIndex) -> Option<NonNull<vm::Func>>>,
pub memories: Map<Memory, MemoryIndex>,
pub globals: Map<Global, GlobalIndex>,
pub tables: Map<Table, TableIndex>,
pub memories: Map<MemoryIndex, Memory>,
pub globals: Map<GlobalIndex, Global>,
pub tables: Map<TableIndex, Table>,
pub imported_functions: Map<ImportName, FuncIndex>,
pub imported_memories: Map<(ImportName, Memory), MemoryIndex>,
pub imported_tables: Map<(ImportName, Table), TableIndex>,
pub imported_globals: Map<(ImportName, GlobalDesc), GlobalIndex>,
pub imported_functions: Map<FuncIndex, ImportName>,
pub imported_memories: Map<MemoryIndex, (ImportName, Memory)>,
pub imported_tables: Map<TableIndex, (ImportName, Table)>,
pub imported_globals: Map<GlobalIndex, (ImportName, GlobalDesc)>,
pub exports: HashMap<String, Export>,
@ -26,8 +26,8 @@ pub struct Module {
pub table_initializers: Vec<TableInitializer>,
pub start_func: Option<FuncIndex>,
pub signature_assoc: Map<SigIndex, FuncIndex>,
pub signatures: Map<FuncSig, SigIndex>,
pub signature_assoc: Map<FuncIndex, SigIndex>,
pub signatures: Map<SigIndex, FuncSig>,
}
impl Module {

View File

@ -7,7 +7,7 @@ use hashbrown::HashMap;
pub struct SigRegistry {
sig_set: HashMap<FuncSig, vm::SigId>,
signatures: Map<vm::SigId, SigIndex>,
signatures: Map<SigIndex, vm::SigId>,
}
impl SigRegistry {
@ -19,7 +19,8 @@ impl SigRegistry {
for (_, &sig_index) in &module.signature_assoc {
let func_sig = module.signatures[sig_index].clone();
registry.register(func_sig);
let new_sig_index = registry.register(func_sig);
assert_eq!(sig_index, new_sig_index);
}
registry
@ -33,12 +34,12 @@ impl SigRegistry {
self.signatures[sig_index]
}
fn register(&mut self, signature: FuncSig) {
fn register(&mut self, signature: FuncSig) -> SigIndex {
let index = self.sig_set.len();
let vm_sig_id = *self
.sig_set
.entry(signature)
.or_insert_with(|| vm::SigId(index as u32));
self.signatures.push(vm_sig_id);
self.signatures.push(vm_sig_id)
}
}

View File

@ -84,8 +84,10 @@ pub struct Table {
/// Overtime, this will be able to represent more and more
/// complex expressions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlobalInit {
Val(Val),
pub enum Initializer {
/// Corresponds to a `const.*` instruction.
Const(Val),
/// Corresponds to a `get_global` instruction.
GetGlobal(GlobalIndex),
}
@ -99,7 +101,7 @@ pub struct GlobalDesc {
#[derive(Debug, Clone, Copy)]
pub struct Global {
pub desc: GlobalDesc,
pub init: GlobalInit,
pub init: Initializer,
}
/// A wasm memory.
@ -144,7 +146,7 @@ pub trait MapIndex {
/// Dense item map
#[derive(Debug, Clone)]
pub struct Map<T, I>
pub struct Map<I, T>
where
I: MapIndex,
{
@ -152,7 +154,7 @@ where
_marker: PhantomData<I>,
}
impl<T, I> Map<T, I>
impl<I, T> Map<I, T>
where
I: MapIndex,
{
@ -189,7 +191,7 @@ where
}
}
impl<T, I> Index<I> for Map<T, I>
impl<I, T> Index<I> for Map<I, T>
where
I: MapIndex,
{
@ -199,7 +201,7 @@ where
}
}
impl<T, I> IndexMut<I> for Map<T, I>
impl<I, T> IndexMut<I> for Map<I, T>
where
I: MapIndex,
{
@ -208,7 +210,7 @@ where
}
}
impl<'a, T, I> IntoIterator for &'a Map<T, I>
impl<'a, I, T> IntoIterator for &'a Map<I, T>
where
I: MapIndex,
{
@ -220,7 +222,7 @@ where
}
}
impl<'a, T, I> IntoIterator for &'a mut Map<T, I>
impl<'a, I, T> IntoIterator for &'a mut Map<I, T>
where
I: MapIndex,
{