Replace all table ids with Rcs

This commit is contained in:
Sergey Pepyakin 2017-12-13 13:34:15 +01:00
parent 471ec25732
commit 85bac6dba3
2 changed files with 14 additions and 34 deletions

View File

@ -447,8 +447,7 @@ impl<'a, St: 'static> Interpreter<'a, St> {
let table = context let table = context
.module() .module()
.table_by_index(self.store, DEFAULT_TABLE_INDEX) .table_by_index(self.store, DEFAULT_TABLE_INDEX)
.expect("Due to validation table should exists") .expect("Due to validation table should exists");
.resolve(self.store);
let func_ref = table.get(table_func_idx)?; let func_ref = table.get(table_func_idx)?;
let actual_function_type = func_ref.func_type().resolve(self.store); let actual_function_type = func_ref.func_type().resolve(self.store);

View File

@ -38,7 +38,7 @@ impl ModuleId {
.cloned() .cloned()
} }
pub fn table_by_index(&self, store: &Store, idx: u32) -> Option<TableId> { pub fn table_by_index(&self, store: &Store, idx: u32) -> Option<Rc<TableInstance>> {
store.resolve_module(*self) store.resolve_module(*self)
.tables .tables
.get(idx as usize) .get(idx as usize)
@ -74,19 +74,6 @@ impl ModuleId {
} }
} }
#[derive(Copy, Clone, Debug)]
pub struct TableId(u32);
impl TableId {
pub fn resolve<'s>(&self, store: &'s Store) -> &'s TableInstance {
store
.tables
.get(self.0 as usize)
.expect("ID should be always valid")
}
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct MemoryId(u32); pub struct MemoryId(u32);
@ -105,7 +92,7 @@ pub struct GlobalId(u32);
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum ExternVal { pub enum ExternVal {
Func(Rc<FuncInstance>), Func(Rc<FuncInstance>),
Table(TableId), Table(Rc<TableInstance>),
Memory(MemoryId), Memory(MemoryId),
Global(GlobalId), Global(GlobalId),
} }
@ -118,9 +105,9 @@ impl ExternVal {
} }
} }
pub fn as_table(&self) -> Option<TableId> { pub fn as_table(&self) -> Option<Rc<TableInstance>> {
match *self { match *self {
ExternVal::Table(table) => Some(table), ExternVal::Table(ref table) => Some(Rc::clone(table)),
_ => None, _ => None,
} }
} }
@ -210,7 +197,7 @@ pub struct ExportInstance {
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct ModuleInstance { pub struct ModuleInstance {
types: Vec<TypeId>, types: Vec<TypeId>,
tables: Vec<TableId>, tables: Vec<Rc<TableInstance>>,
funcs: Vec<Rc<FuncInstance>>, funcs: Vec<Rc<FuncInstance>>,
memories: Vec<MemoryId>, memories: Vec<MemoryId>,
globals: Vec<GlobalId>, globals: Vec<GlobalId>,
@ -277,11 +264,9 @@ impl Store {
Rc::new(func) Rc::new(func)
} }
pub fn alloc_table(&mut self, table_type: &TableType) -> Result<TableId, Error> { pub fn alloc_table(&mut self, table_type: &TableType) -> Result<Rc<TableInstance>, Error> {
let table = TableInstance::new(table_type)?; let table = TableInstance::new(table_type)?;
self.tables.push(table); Ok(Rc::new(table))
let table_id = self.tables.len() - 1;
Ok(TableId(table_id as u32))
} }
pub fn alloc_memory(&mut self, mem_type: &MemoryType) -> Result<MemoryId, Error> { pub fn alloc_memory(&mut self, mem_type: &MemoryType) -> Result<MemoryId, Error> {
@ -338,9 +323,9 @@ impl Store {
} }
instance.funcs.push(Rc::clone(func)) instance.funcs.push(Rc::clone(func))
} }
(&External::Table(ref tt), &ExternVal::Table(table)) => { (&External::Table(ref tt), &ExternVal::Table(ref table)) => {
match_limits(table.resolve(self).limits(), tt.limits())?; match_limits(table.limits(), tt.limits())?;
instance.tables.push(table); instance.tables.push(Rc::clone(table));
} }
(&External::Memory(ref mt), &ExternVal::Memory(memory)) => { (&External::Memory(ref mt), &ExternVal::Memory(memory)) => {
match_limits(memory.resolve(self).limits(), mt.limits())?; match_limits(memory.resolve(self).limits(), mt.limits())?;
@ -442,11 +427,11 @@ impl Store {
ExternVal::Memory(*memory_id) ExternVal::Memory(*memory_id)
} }
Internal::Table(idx) => { Internal::Table(idx) => {
let table_id = instance let table = instance
.tables .tables
.get(idx as usize) .get(idx as usize)
.expect("Due to validation table should exists"); .expect("Due to validation table should exists");
ExternVal::Table(*table_id) ExternVal::Table(Rc::clone(table))
} }
}; };
instance.exports.insert(field.into(), extern_val); instance.exports.insert(field.into(), extern_val);
@ -477,14 +462,10 @@ impl Store {
_ => panic!("Due to validation elem segment offset should evaluate to i32"), _ => panic!("Due to validation elem segment offset should evaluate to i32"),
}; };
let table_id = instance let table_inst = instance
.tables .tables
.get(DEFAULT_TABLE_INDEX as usize) .get(DEFAULT_TABLE_INDEX as usize)
.expect("Due to validation default table should exists"); .expect("Due to validation default table should exists");
let table_inst = self.tables
.get_mut(table_id.0 as usize)
.expect("ID should be always valid");
for (j, func_idx) in element_segment.members().into_iter().enumerate() { for (j, func_idx) in element_segment.members().into_iter().enumerate() {
let func = instance let func = instance
.funcs .funcs