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

95 lines
2.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;
use interpreter::store::{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-12-13 17:31:30 +01:00
use interpreter::imports::Imports;
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-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
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-13 17:31:30 +01:00
let module_instance = {
let mut imports = Imports::new();
for (module_name, module_instance) in self.modules.iter() {
imports.push_resolver(&**module_name, &**module_instance);
}
ModuleInstance::instantiate(&module, &imports, state)?
};
2017-12-13 14:36:06 +01:00
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))
})?;
module_instance.invoke_export(func_name, 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))
})?;
module_instance.invoke_index(func_idx, 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 15:00:54 +01:00
func_instance: Rc<FuncInstance>,
2017-12-12 17:01:02 +01:00
args: Vec<RuntimeValue>,
state: &mut St,
) -> Result<Option<RuntimeValue>, Error> {
2017-12-13 15:00:54 +01:00
FuncInstance::invoke(Rc::clone(&func_instance), args, state)
2017-12-12 15:18:35 +01:00
}
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
}