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

89 lines
2.9 KiB
Rust
Raw Normal View History

2017-04-21 14:35:12 +03:00
use std::sync::Arc;
use std::collections::HashMap;
use parking_lot::RwLock;
use elements::Module;
2017-11-25 22:55:45 +03:00
use interpreter::Error;
2017-11-27 15:37:39 +03:00
use interpreter::emscripten::{self, env_module};
2017-05-04 11:25:25 +03:00
use interpreter::module::{ModuleInstance, ModuleInstanceInterface};
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-04-21 14:35:12 +03:00
/// Shared data reference.
2017-11-25 22:55:45 +03:00
essence: Arc<ProgramInstanceEssence>,
2017-04-21 14:35:12 +03:00
}
/// Program instance essence.
2017-11-25 22:55:45 +03:00
pub struct ProgramInstanceEssence {
2017-04-21 14:35:12 +03:00
/// Loaded modules.
2017-11-25 22:55:45 +03:00
modules: RwLock<HashMap<String, Arc<ModuleInstanceInterface>>>,
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-25 22:55:45 +03:00
pub fn new() -> Result<Self, Error> {
2017-11-27 15:37:39 +03:00
ProgramInstance::with_env_params(emscripten::EnvParams::default())
2017-05-30 17:15:36 +03:00
}
/// Create new program instance with custom env module params (mostly memory)
2017-11-27 15:37:39 +03:00
pub fn with_env_params(params: emscripten::EnvParams) -> Result<Self, Error> {
2017-05-04 11:25:25 +03:00
Ok(ProgramInstance {
2017-05-30 17:15:36 +03:00
essence: Arc::new(ProgramInstanceEssence::with_env_params(params)?),
2017-05-04 11:25:25 +03:00
})
2017-04-21 14:35:12 +03:00
}
/// Create a new program instance with a custom env module
2017-11-25 22:55:45 +03:00
pub fn with_env_module(env_module: Arc<ModuleInstanceInterface>) -> Self {
ProgramInstance {
essence: Arc::new(ProgramInstanceEssence::with_env_module(env_module)),
}
}
2017-06-07 14:48:02 +03:00
/// Instantiate module with validation.
2017-11-25 22:55:45 +03:00
pub fn add_module<'a>(&self, name: &str, module: Module, externals: Option<&'a HashMap<String, Arc<ModuleInstanceInterface + 'a>>>) -> Result<Arc<ModuleInstance>, Error> {
2017-06-22 17:52:05 +03:00
let mut module_instance = ModuleInstance::new(Arc::downgrade(&self.essence), name.into(), module)?;
module_instance.instantiate(externals)?;
2017-06-22 17:52:05 +03:00
let module_instance = Arc::new(module_instance);
self.essence.modules.write().insert(name.into(), module_instance.clone());
module_instance.run_start_function()?;
Ok(module_instance)
2017-06-13 12:01:59 +03:00
}
/// Insert instantiated module.
2017-11-25 22:55:45 +03:00
pub fn insert_loaded_module(&self, name: &str, module_instance: Arc<ModuleInstance>) -> Result<Arc<ModuleInstance>, Error> {
2017-06-07 14:48:02 +03:00
// replace existing module with the same name with new one
self.essence.modules.write().insert(name.into(), module_instance.clone());
Ok(module_instance)
}
/// Get one of the modules by name
2017-11-25 22:55:45 +03:00
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface>> {
self.essence.module(name)
}
2017-04-21 14:35:12 +03:00
}
2017-11-25 22:55:45 +03:00
impl ProgramInstanceEssence {
2017-04-21 14:35:12 +03:00
/// Create new program essence.
2017-11-25 22:55:45 +03:00
pub fn new() -> Result<Self, Error> {
2017-11-27 15:37:39 +03:00
ProgramInstanceEssence::with_env_params(emscripten::EnvParams::default())
2017-05-30 17:15:36 +03:00
}
2017-11-27 15:37:39 +03:00
pub fn with_env_params(env_params: emscripten::EnvParams) -> Result<Self, Error> {
let env_mod = env_module(env_params)?;
Ok(ProgramInstanceEssence::with_env_module(Arc::new(env_mod)))
}
2017-11-25 22:55:45 +03:00
pub fn with_env_module(env_module: Arc<ModuleInstanceInterface>) -> Self {
2017-05-04 11:25:25 +03:00
let mut modules = HashMap::new();
modules.insert("env".into(), env_module);
ProgramInstanceEssence {
2017-05-04 11:25:25 +03:00
modules: RwLock::new(modules),
}
2017-04-21 14:35:12 +03:00
}
/// Get module reference.
2017-11-25 22:55:45 +03:00
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface>> {
2017-04-21 14:35:12 +03:00
self.modules.read().get(name).cloned()
}
}