Files
parity-wasm/src/interpreter/program.rs

133 lines
3.5 KiB
Rust
Raw Normal View History

2017-12-13 13:25:24 +01:00
use std::rc::Rc;
2017-04-21 14:35:12 +03:00
use std::collections::HashMap;
use elements::Module;
2017-11-25 22:55:45 +03:00
use interpreter::Error;
2017-12-13 14:36:06 +01:00
use interpreter::store::{Store, ExternVal, FuncInstance, ModuleInstance};
2017-12-11 19:22:45 +01:00
use interpreter::host::HostModule;
2017-12-11 19:37:08 +01:00
use interpreter::value::RuntimeValue;
2017-04-21 14:35:12 +03:00
/// Program instance. Program is a set of instantiated modules.
2017-11-25 22:55:45 +03:00
pub struct ProgramInstance {
2017-12-11 14:58:02 +01:00
store: Store,
2017-12-13 14:36:06 +01:00
modules: HashMap<String, Rc<ModuleInstance>>,
2017-04-21 14:35:12 +03:00
}
2017-11-25 22:55:45 +03:00
impl ProgramInstance {
2017-04-21 14:35:12 +03:00
/// Create new program instance.
2017-11-27 16:11:12 +03:00
pub fn new() -> Self {
ProgramInstance {
2017-12-11 14:58:02 +01:00
store: Store::new(),
modules: HashMap::new(),
2017-11-27 16:11:12 +03:00
}
2017-05-30 17:15:36 +03:00
}
2017-06-07 14:48:02 +03:00
/// Instantiate module with validation.
2017-12-11 16:38:52 +01:00
pub fn add_module<'a, St: 'static>(
2017-12-11 14:58:02 +01:00
&mut self,
name: &str,
module: Module,
2017-12-11 18:38:09 +01:00
state: &mut St,
2017-12-13 14:36:06 +01:00
) -> Result<Rc<ModuleInstance>, Error> {
2017-12-11 15:12:46 +01:00
let mut extern_vals = Vec::new();
2017-12-11 14:58:02 +01:00
for import_entry in module.import_section().map(|s| s.entries()).unwrap_or(&[]) {
2017-12-13 14:36:06 +01:00
let module = self.modules.get(import_entry.module()).ok_or_else(|| Error::Program(format!("Module {} not found", import_entry.module())))?;
2017-12-11 14:58:02 +01:00
let extern_val = module
2017-12-13 14:36:06 +01:00
.export_by_name(import_entry.field())
2017-12-11 19:37:08 +01:00
.ok_or_else(|| {
Error::Program(format!(
"Module {} doesn't have export {}",
import_entry.module(),
import_entry.field()
))
})?;
2017-12-11 14:58:02 +01:00
extern_vals.push(extern_val);
}
2017-12-13 14:36:06 +01:00
let module_instance = self.store.instantiate_module(&module, &extern_vals, state)?;
self.modules.insert(name.to_owned(), Rc::clone(&module_instance));
2017-06-13 12:01:59 +03:00
2017-12-13 14:36:06 +01:00
Ok(module_instance)
2017-06-07 14:48:02 +03:00
}
2017-12-11 19:37:08 +01:00
pub fn add_host_module(
&mut self,
name: &str,
host_module: HostModule,
2017-12-13 14:36:06 +01:00
) -> Result<Rc<ModuleInstance>, Error> {
let module_instance = host_module.allocate()?;
self.modules.insert(name.to_owned(), Rc::clone(&module_instance));
Ok(module_instance)
2017-12-11 16:28:05 +01:00
}
2017-12-13 14:36:06 +01:00
pub fn insert_loaded_module(&mut self, name: &str, module: Rc<ModuleInstance>) {
2017-12-12 17:01:02 +01:00
self.modules.insert(name.to_owned(), module);
}
2017-12-11 19:37:08 +01:00
pub fn invoke_export<St: 'static>(
&mut self,
module_name: &str,
func_name: &str,
args: Vec<RuntimeValue>,
state: &mut St,
) -> Result<Option<RuntimeValue>, Error> {
2017-12-13 14:38:08 +01:00
let module_instance = self.modules.get(module_name).ok_or_else(|| {
2017-12-11 19:37:08 +01:00
Error::Program(format!("Module {} not found", module_name))
})?;
2017-12-13 14:38:08 +01:00
let extern_val = module_instance
2017-12-13 14:36:06 +01:00
.export_by_name(func_name)
2017-12-11 19:37:08 +01:00
.ok_or_else(|| {
Error::Program(format!(
"Module {} doesn't have export {}",
module_name,
func_name
))
})?;
2017-12-13 14:38:08 +01:00
let func_instance = match extern_val {
ExternVal::Func(func_instance) => func_instance,
2017-12-11 19:37:08 +01:00
unexpected => {
return Err(Error::Program(format!(
"Export {} is not a function, but {:?}",
func_name,
unexpected
)))
}
};
2017-12-13 14:38:08 +01:00
self.store.invoke(func_instance, args, state)
2017-12-11 19:37:08 +01:00
}
pub fn invoke_index<St: 'static>(
&mut self,
module_name: &str,
func_idx: u32,
args: Vec<RuntimeValue>,
state: &mut St,
) -> Result<Option<RuntimeValue>, Error> {
2017-12-13 14:38:08 +01:00
let module_instance = self.modules.get(module_name).cloned().ok_or_else(|| {
2017-12-11 19:37:08 +01:00
Error::Program(format!("Module {} not found", module_name))
})?;
2017-12-13 14:38:08 +01:00
let func_instance = module_instance.func_by_index(func_idx).ok_or_else(|| {
2017-12-11 19:39:38 +01:00
Error::Program(format!("Module doesn't contain function at index {}", func_idx))
})?;
2017-12-13 14:38:08 +01:00
self.invoke_func(func_instance, args, state)
2017-12-12 17:01:02 +01:00
}
2017-12-11 19:37:08 +01:00
2017-12-12 17:01:02 +01:00
pub fn invoke_func<St: 'static>(
&mut self,
2017-12-13 13:25:24 +01:00
func: Rc<FuncInstance>,
2017-12-12 17:01:02 +01:00
args: Vec<RuntimeValue>,
state: &mut St,
) -> Result<Option<RuntimeValue>, Error> {
self.store.invoke(func, args, state)
2017-04-21 14:35:12 +03:00
}
2017-12-12 15:18:35 +01:00
pub fn store(&self) -> &Store {
&self.store
}
2017-12-12 15:40:54 +01:00
2017-12-13 14:36:06 +01:00
pub fn module(&self, name: &str) -> Option<Rc<ModuleInstance>> {
2017-12-12 15:40:54 +01:00
self.modules.get(name).cloned()
}
2017-04-21 14:35:12 +03:00
}