77 lines
2.3 KiB
Rust
Raw Normal View History

2017-04-21 14:35:12 +03:00
use std::sync::Arc;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use parking_lot::RwLock;
use elements::Module;
use interpreter::Error;
2017-05-15 13:58:55 +03:00
use interpreter::env::{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.
pub struct ProgramInstance {
/// Shared data reference.
essence: Arc<ProgramInstanceEssence>,
}
/// Program instance essence.
pub struct ProgramInstanceEssence {
/// Loaded modules.
2017-05-04 11:25:25 +03:00
modules: RwLock<HashMap<String, Arc<ModuleInstanceInterface>>>,
2017-04-21 14:35:12 +03:00
}
impl ProgramInstance {
/// Create new program instance.
2017-05-04 11:25:25 +03:00
pub fn new() -> Result<Self, Error> {
Ok(ProgramInstance {
essence: Arc::new(ProgramInstanceEssence::new()?),
})
2017-04-21 14:35:12 +03:00
}
2017-05-15 15:40:08 +03:00
/// Create new program instance with predefined user-defined functions
2017-05-15 13:58:55 +03:00
pub fn with_functions(funcs: env::UserFunctions) -> Result<Self, Error> {
Ok(ProgramInstance {
essence: Arc::new(ProgramInstanceEssence::with_functions(funcs)?),
})
}
2017-04-21 14:35:12 +03:00
/// Instantiate module.
2017-04-27 14:44:03 +03:00
pub fn add_module(&self, name: &str, module: Module) -> Result<Arc<ModuleInstance>, Error> {
2017-05-04 10:37:33 +03:00
let module_instance = Arc::new(ModuleInstance::new(Arc::downgrade(&self.essence), module)?);
2017-04-21 14:35:12 +03:00
let mut modules = self.essence.modules.write();
match modules.entry(name.into()) {
Entry::Occupied(_) => Err(Error::Program(format!("module {} already instantiated", name))),
Entry::Vacant(entry) => {
2017-04-27 14:44:03 +03:00
entry.insert(module_instance.clone());
Ok(module_instance)
2017-04-21 14:35:12 +03:00
},
}
}
/// Get one of the modules by name
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface>> {
self.essence.module(name)
}
2017-04-21 14:35:12 +03:00
}
impl ProgramInstanceEssence {
/// Create new program essence.
2017-05-04 11:25:25 +03:00
pub fn new() -> Result<Self, Error> {
2017-05-15 13:58:55 +03:00
ProgramInstanceEssence::with_functions(HashMap::with_capacity(0))
}
2017-05-15 15:40:08 +03:00
/// Create new program essence with provided user-defined functions
2017-05-15 13:58:55 +03:00
pub fn with_functions(funcs: env::UserFunctions) -> Result<Self, Error> {
2017-05-04 11:25:25 +03:00
let mut modules = HashMap::new();
2017-05-15 13:58:55 +03:00
let env_module: Arc<ModuleInstanceInterface> = Arc::new(env_module(funcs)?);
2017-05-04 11:25:25 +03:00
modules.insert("env".into(), env_module);
Ok(ProgramInstanceEssence {
modules: RwLock::new(modules),
})
2017-04-21 14:35:12 +03:00
}
/// Get module reference.
2017-05-04 11:25:25 +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()
}
}