invoke_index & invoke_export moved to module

This commit is contained in:
Sergey Pepyakin
2017-12-13 16:27:37 +01:00
parent 871b36187d
commit 1bbfd4219f
2 changed files with 114 additions and 97 deletions

View File

@ -2,7 +2,7 @@ use std::rc::Rc;
use std::collections::HashMap; use std::collections::HashMap;
use elements::Module; use elements::Module;
use interpreter::Error; use interpreter::Error;
use interpreter::store::{ExternVal, FuncInstance, ModuleInstance}; use interpreter::store::{FuncInstance, ModuleInstance};
use interpreter::host::HostModule; use interpreter::host::HostModule;
use interpreter::value::RuntimeValue; use interpreter::value::RuntimeValue;
@ -71,28 +71,7 @@ impl ProgramInstance {
let module_instance = self.modules.get(module_name).ok_or_else(|| { let module_instance = 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 extern_val = module_instance module_instance.invoke_export(func_name, args, state)
.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)
} }
pub fn invoke_index<St: 'static>( pub fn invoke_index<St: 'static>(
@ -105,10 +84,7 @@ impl ProgramInstance {
let module_instance = self.modules.get(module_name).cloned().ok_or_else(|| { let module_instance = self.modules.get(module_name).cloned().ok_or_else(|| {
Error::Program(format!("Module {} not found", module_name)) Error::Program(format!("Module {} not found", module_name))
})?; })?;
let func_instance = module_instance.func_by_index(func_idx).ok_or_else(|| { module_instance.invoke_index(func_idx, args, state)
Error::Program(format!("Module doesn't contain function at index {}", func_idx))
})?;
self.invoke_func(func_instance, args, state)
} }
pub fn invoke_func<St: 'static>( pub fn invoke_func<St: 'static>(

View File

@ -190,6 +190,82 @@ impl ModuleInstance {
} }
} }
pub fn memory_by_index(&self, idx: u32) -> Option<Rc<MemoryInstance>> {
self
.memories
.borrow()
.get(idx as usize)
.cloned()
}
pub fn table_by_index(&self, idx: u32) -> Option<Rc<TableInstance>> {
self
.tables
.borrow()
.get(idx as usize)
.cloned()
}
pub fn global_by_index(&self, idx: u32) -> Option<Rc<GlobalInstance>> {
self
.globals
.borrow()
.get(idx as usize)
.cloned()
}
pub fn func_by_index(&self, idx: u32) -> Option<Rc<FuncInstance>> {
self
.funcs
.borrow()
.get(idx as usize)
.cloned()
}
pub fn type_by_index(&self, idx: u32) -> Option<Rc<FunctionType>> {
self
.types
.borrow()
.get(idx as usize)
.cloned()
}
pub fn export_by_name(&self, name: &str) -> Option<ExternVal> {
self
.exports
.borrow()
.get(name)
.cloned()
}
fn push_func(&self, func: Rc<FuncInstance>) {
self.funcs.borrow_mut().push(func);
}
fn push_type(&self, func_type: Rc<FunctionType>) {
self.types.borrow_mut().push(func_type)
}
fn push_memory(&self, memory: Rc<MemoryInstance>) {
self.memories.borrow_mut().push(memory)
}
fn push_table(&self, table: Rc<TableInstance>) {
self.tables.borrow_mut().push(table)
}
fn push_global(&self, global: Rc<GlobalInstance>) {
self.globals.borrow_mut().push(global)
}
fn insert_export<N: Into<String>>(&self, name: N, extern_val: ExternVal) {
self
.exports
.borrow_mut()
.insert(name.into(), extern_val);
}
fn alloc_module_internal( fn alloc_module_internal(
module: &Module, module: &Module,
extern_vals: &[ExternVal], extern_vals: &[ExternVal],
@ -395,83 +471,48 @@ impl ModuleInstance {
Ok(instance) Ok(instance)
} }
pub fn memory_by_index(&self, idx: u32) -> Option<Rc<MemoryInstance>> { pub fn invoke_index<St: 'static>(
self &self,
.memories func_idx: u32,
.borrow() args: Vec<RuntimeValue>,
.get(idx as usize) state: &mut St,
.cloned() ) -> Result<Option<RuntimeValue>, 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<Rc<TableInstance>> { pub fn invoke_export<St: 'static>(
self &self,
.tables func_name: &str,
.borrow() args: Vec<RuntimeValue>,
.get(idx as usize) state: &mut St,
.cloned() ) -> Result<Option<RuntimeValue>, 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<Rc<GlobalInstance>> { let func_instance = match extern_val {
self ExternVal::Func(func_instance) => func_instance,
.globals unexpected => {
.borrow() return Err(Error::Program(format!(
.get(idx as usize) "Export {} is not a function, but {:?}",
.cloned() func_name,
} unexpected
)));
}
};
pub fn func_by_index(&self, idx: u32) -> Option<Rc<FuncInstance>> { FuncInstance::invoke(Rc::clone(&func_instance), args, state)
self
.funcs
.borrow()
.get(idx as usize)
.cloned()
}
pub fn type_by_index(&self, idx: u32) -> Option<Rc<FunctionType>> {
self
.types
.borrow()
.get(idx as usize)
.cloned()
}
pub fn export_by_name(&self, name: &str) -> Option<ExternVal> {
self
.exports
.borrow()
.get(name)
.cloned()
}
fn push_func(&self, func: Rc<FuncInstance>) {
self.funcs.borrow_mut().push(func);
}
fn push_type(&self, func_type: Rc<FunctionType>) {
self.types.borrow_mut().push(func_type)
}
fn push_memory(&self, memory: Rc<MemoryInstance>) {
self.memories.borrow_mut().push(memory)
}
fn push_table(&self, table: Rc<TableInstance>) {
self.tables.borrow_mut().push(table)
}
fn push_global(&self, global: Rc<GlobalInstance>) {
self.globals.borrow_mut().push(global)
}
fn insert_export<N: Into<String>>(&self, name: N, extern_val: ExternVal) {
self
.exports
.borrow_mut()
.insert(name.into(), extern_val);
} }
} }
fn alloc_func_type(func_type: FunctionType) -> Rc<FunctionType> { fn alloc_func_type(func_type: FunctionType) -> Rc<FunctionType> {
Rc::new(func_type) Rc::new(func_type)
} }