diff --git a/src/interpreter/program.rs b/src/interpreter/program.rs index 4b68a76..fe69b21 100644 --- a/src/interpreter/program.rs +++ b/src/interpreter/program.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use std::collections::HashMap; use elements::Module; use interpreter::Error; -use interpreter::store::{ExternVal, FuncInstance, ModuleInstance}; +use interpreter::store::{FuncInstance, ModuleInstance}; use interpreter::host::HostModule; use interpreter::value::RuntimeValue; @@ -71,28 +71,7 @@ impl ProgramInstance { let module_instance = self.modules.get(module_name).ok_or_else(|| { Error::Program(format!("Module {} not found", module_name)) })?; - let extern_val = module_instance - .export_by_name(func_name) - .ok_or_else(|| { - Error::Program(format!( - "Module {} doesn't have export {}", - module_name, - func_name - )) - })?; - - let func_instance = match extern_val { - ExternVal::Func(func_instance) => func_instance, - unexpected => { - return Err(Error::Program(format!( - "Export {} is not a function, but {:?}", - func_name, - unexpected - ))); - } - }; - - FuncInstance::invoke(Rc::clone(&func_instance), args, state) + module_instance.invoke_export(func_name, args, state) } pub fn invoke_index( @@ -105,10 +84,7 @@ impl ProgramInstance { let module_instance = self.modules.get(module_name).cloned().ok_or_else(|| { Error::Program(format!("Module {} not found", module_name)) })?; - let func_instance = module_instance.func_by_index(func_idx).ok_or_else(|| { - Error::Program(format!("Module doesn't contain function at index {}", func_idx)) - })?; - self.invoke_func(func_instance, args, state) + module_instance.invoke_index(func_idx, args, state) } pub fn invoke_func( diff --git a/src/interpreter/store.rs b/src/interpreter/store.rs index f0a3301..3da9a42 100644 --- a/src/interpreter/store.rs +++ b/src/interpreter/store.rs @@ -190,6 +190,82 @@ impl ModuleInstance { } } + + pub fn memory_by_index(&self, idx: u32) -> Option> { + self + .memories + .borrow() + .get(idx as usize) + .cloned() + } + + pub fn table_by_index(&self, idx: u32) -> Option> { + self + .tables + .borrow() + .get(idx as usize) + .cloned() + } + + pub fn global_by_index(&self, idx: u32) -> Option> { + self + .globals + .borrow() + .get(idx as usize) + .cloned() + } + + pub fn func_by_index(&self, idx: u32) -> Option> { + self + .funcs + .borrow() + .get(idx as usize) + .cloned() + } + + pub fn type_by_index(&self, idx: u32) -> Option> { + self + .types + .borrow() + .get(idx as usize) + .cloned() + } + + pub fn export_by_name(&self, name: &str) -> Option { + self + .exports + .borrow() + .get(name) + .cloned() + } + + fn push_func(&self, func: Rc) { + self.funcs.borrow_mut().push(func); + } + + fn push_type(&self, func_type: Rc) { + self.types.borrow_mut().push(func_type) + } + + fn push_memory(&self, memory: Rc) { + self.memories.borrow_mut().push(memory) + } + + fn push_table(&self, table: Rc) { + self.tables.borrow_mut().push(table) + } + + fn push_global(&self, global: Rc) { + self.globals.borrow_mut().push(global) + } + + fn insert_export>(&self, name: N, extern_val: ExternVal) { + self + .exports + .borrow_mut() + .insert(name.into(), extern_val); + } + fn alloc_module_internal( module: &Module, extern_vals: &[ExternVal], @@ -395,83 +471,48 @@ impl ModuleInstance { Ok(instance) } - pub fn memory_by_index(&self, idx: u32) -> Option> { - self - .memories - .borrow() - .get(idx as usize) - .cloned() + pub fn invoke_index( + &self, + func_idx: u32, + args: Vec, + state: &mut St, + ) -> Result, Error> { + let func_instance = self.func_by_index(func_idx).ok_or_else(|| { + Error::Program(format!("Module doesn't contain function at index {}", func_idx)) + })?; + FuncInstance::invoke(func_instance, args, state) } - pub fn table_by_index(&self, idx: u32) -> Option> { - self - .tables - .borrow() - .get(idx as usize) - .cloned() - } + pub fn invoke_export( + &self, + func_name: &str, + args: Vec, + state: &mut St, + ) -> Result, Error> { + let extern_val = self + .export_by_name(func_name) + .ok_or_else(|| { + Error::Program(format!( + "Module doesn't have export {}", + func_name + )) + })?; - pub fn global_by_index(&self, idx: u32) -> Option> { - self - .globals - .borrow() - .get(idx as usize) - .cloned() - } + let func_instance = match extern_val { + ExternVal::Func(func_instance) => func_instance, + unexpected => { + return Err(Error::Program(format!( + "Export {} is not a function, but {:?}", + func_name, + unexpected + ))); + } + }; - pub fn func_by_index(&self, idx: u32) -> Option> { - self - .funcs - .borrow() - .get(idx as usize) - .cloned() - } - - pub fn type_by_index(&self, idx: u32) -> Option> { - self - .types - .borrow() - .get(idx as usize) - .cloned() - } - - pub fn export_by_name(&self, name: &str) -> Option { - self - .exports - .borrow() - .get(name) - .cloned() - } - - fn push_func(&self, func: Rc) { - self.funcs.borrow_mut().push(func); - } - - fn push_type(&self, func_type: Rc) { - self.types.borrow_mut().push(func_type) - } - - fn push_memory(&self, memory: Rc) { - self.memories.borrow_mut().push(memory) - } - - fn push_table(&self, table: Rc) { - self.tables.borrow_mut().push(table) - } - - fn push_global(&self, global: Rc) { - self.globals.borrow_mut().push(global) - } - - fn insert_export>(&self, name: N, extern_val: ExternVal) { - self - .exports - .borrow_mut() - .insert(name.into(), extern_val); + FuncInstance::invoke(Rc::clone(&func_instance), args, state) } } - fn alloc_func_type(func_type: FunctionType) -> Rc { Rc::new(func_type) }