mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-21 18:51:52 +00:00
return custom user errors from native functions
This commit is contained in:
@ -2,23 +2,23 @@ use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use parking_lot::RwLock;
|
||||
use elements::Module;
|
||||
use interpreter::Error;
|
||||
use interpreter::{Error, CustomUserError, InterpreterError};
|
||||
use interpreter::env::{self, env_module};
|
||||
use interpreter::module::{ModuleInstance, ModuleInstanceInterface};
|
||||
|
||||
/// Program instance. Program is a set of instantiated modules.
|
||||
pub struct ProgramInstance {
|
||||
pub struct ProgramInstance<E: CustomUserError> {
|
||||
/// Shared data reference.
|
||||
essence: Arc<ProgramInstanceEssence>,
|
||||
essence: Arc<ProgramInstanceEssence<E>>,
|
||||
}
|
||||
|
||||
/// Program instance essence.
|
||||
pub struct ProgramInstanceEssence {
|
||||
pub struct ProgramInstanceEssence<E: CustomUserError> {
|
||||
/// Loaded modules.
|
||||
modules: RwLock<HashMap<String, Arc<ModuleInstanceInterface>>>,
|
||||
modules: RwLock<HashMap<String, Arc<ModuleInstanceInterface<E>>>>,
|
||||
}
|
||||
|
||||
impl ProgramInstance {
|
||||
impl<E> ProgramInstance<E> where E: CustomUserError {
|
||||
/// Create new program instance.
|
||||
pub fn new() -> Result<Self, Error> {
|
||||
ProgramInstance::with_env_params(env::EnvParams::default())
|
||||
@ -32,7 +32,7 @@ impl ProgramInstance {
|
||||
}
|
||||
|
||||
/// Instantiate module with validation.
|
||||
pub fn add_module<'a>(&self, name: &str, module: Module, externals: Option<&'a HashMap<String, Arc<ModuleInstanceInterface + 'a>>>) -> Result<Arc<ModuleInstance>, Error> {
|
||||
pub fn add_module<'a>(&self, name: &str, module: Module, externals: Option<&'a HashMap<String, Arc<ModuleInstanceInterface<E> + 'a>>>) -> Result<Arc<ModuleInstance<E>>, InterpreterError<E>> {
|
||||
let mut module_instance = ModuleInstance::new(Arc::downgrade(&self.essence), name.into(), module)?;
|
||||
module_instance.instantiate(externals)?;
|
||||
|
||||
@ -43,19 +43,19 @@ impl ProgramInstance {
|
||||
}
|
||||
|
||||
/// Insert instantiated module.
|
||||
pub fn insert_loaded_module(&self, name: &str, module_instance: Arc<ModuleInstance>) -> Result<Arc<ModuleInstance>, Error> {
|
||||
pub fn insert_loaded_module(&self, name: &str, module_instance: Arc<ModuleInstance<E>>) -> Result<Arc<ModuleInstance<E>>, Error> {
|
||||
// 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
|
||||
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface>> {
|
||||
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface<E>>> {
|
||||
self.essence.module(name)
|
||||
}
|
||||
}
|
||||
|
||||
impl ProgramInstanceEssence {
|
||||
impl<E> ProgramInstanceEssence<E> where E: CustomUserError {
|
||||
/// Create new program essence.
|
||||
pub fn new() -> Result<Self, Error> {
|
||||
ProgramInstanceEssence::with_env_params(env::EnvParams::default())
|
||||
@ -63,7 +63,7 @@ impl ProgramInstanceEssence {
|
||||
|
||||
pub fn with_env_params(env_params: env::EnvParams) -> Result<Self, Error> {
|
||||
let mut modules = HashMap::new();
|
||||
let env_module: Arc<ModuleInstanceInterface> = Arc::new(env_module(env_params)?);
|
||||
let env_module: Arc<ModuleInstanceInterface<E>> = Arc::new(env_module(env_params)?);
|
||||
modules.insert("env".into(), env_module);
|
||||
Ok(ProgramInstanceEssence {
|
||||
modules: RwLock::new(modules),
|
||||
@ -71,7 +71,7 @@ impl ProgramInstanceEssence {
|
||||
}
|
||||
|
||||
/// Get module reference.
|
||||
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface>> {
|
||||
pub fn module(&self, name: &str) -> Option<Arc<ModuleInstanceInterface<E>>> {
|
||||
self.modules.read().get(name).cloned()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user