func_by_index

This commit is contained in:
Sergey Pepyakin
2017-12-11 19:39:38 +01:00
parent c35d54bd9f
commit 79924b2ab6
3 changed files with 15 additions and 6 deletions

View File

@ -103,7 +103,9 @@ impl ProgramInstance {
let module_id = self.modules.get(module_name).ok_or_else(|| { let module_id = self.modules.get(module_name).ok_or_else(|| {
Error::Program(format!("Module {} not found", module_name)) Error::Program(format!("Module {} not found", module_name))
})?; })?;
let func_id = module_id.resolve_func(&self.store, func_idx); let func_id = module_id.func_by_index(&self.store, func_idx).ok_or_else(|| {
Error::Program(format!("Module doesn't contain function at index {}", func_idx))
})?;
self.store.invoke(func_id, args, state) self.store.invoke(func_id, args, state)
} }

View File

@ -413,8 +413,15 @@ impl<'store, St: 'static> Interpreter<'store, St> {
Ok(InstructionOutcome::Return) Ok(InstructionOutcome::Return)
} }
fn run_call<'a>(&mut self, context: &mut FunctionContext, func_idx: u32) -> Result<InstructionOutcome, Error> { fn run_call<'a>(
let func = context.module().resolve_func(self.store, func_idx); &mut self,
context: &mut FunctionContext,
func_idx: u32,
) -> Result<InstructionOutcome, Error> {
let func = context
.module()
.func_by_index(self.store, func_idx)
.expect("Due to validation func should exists");
Ok(InstructionOutcome::ExecuteCall(func)) Ok(InstructionOutcome::ExecuteCall(func))
} }

View File

@ -56,12 +56,12 @@ impl ModuleId {
.expect("Due to validation global should exists") .expect("Due to validation global should exists")
} }
pub fn resolve_func(&self, store: &Store, idx: u32) -> FuncId { pub fn func_by_index(&self, store: &Store, idx: u32) -> Option<FuncId> {
let instance = store.resolve_module(*self); let instance = store.resolve_module(*self);
*instance instance
.funcs .funcs
.get(idx as usize) .get(idx as usize)
.expect("Due to validation func should exists") .cloned()
} }
pub fn resolve_type(&self, store: &Store, idx: u32) -> TypeId { pub fn resolve_type(&self, store: &Store, idx: u32) -> TypeId {