127 lines
3.5 KiB
Rust
Raw Normal View History

2017-04-27 15:49:14 +03:00
//! WebAssembly interpreter module.
2017-04-21 14:35:12 +03:00
2017-04-27 15:49:14 +03:00
/// Interpreter error.
2017-04-21 14:35:12 +03:00
#[derive(Debug, Clone, PartialEq)]
pub enum InterpreterError<E: CustomUserError> {
/// Internal error.
Internal(Error),
/// Custom user error.
User(E),
}
/// Custom user error.
pub trait CustomUserError: 'static + ::std::fmt::Display + ::std::fmt::Debug + Clone + PartialEq {
}
/// Internal interpreter error.
#[derive(Debug, Clone, PartialEq)]
2017-04-21 14:35:12 +03:00
pub enum Error {
2017-04-27 15:49:14 +03:00
/// Program-level error.
2017-04-21 14:35:12 +03:00
Program(String),
2017-06-07 14:48:02 +03:00
/// Validation error.
Validation(String),
2017-04-27 15:49:14 +03:00
/// Initialization error.
2017-04-21 14:35:12 +03:00
Initialization(String),
2017-04-27 15:49:14 +03:00
/// Function-level error.
2017-04-21 14:35:12 +03:00
Function(String),
2017-04-27 15:49:14 +03:00
/// Table-level error.
2017-04-21 14:35:12 +03:00
Table(String),
2017-04-27 15:49:14 +03:00
/// Memory-level error.
2017-04-21 14:35:12 +03:00
Memory(String),
2017-04-27 15:49:14 +03:00
/// Variable-level error.
2017-04-21 14:35:12 +03:00
Variable(String),
2017-04-27 15:49:14 +03:00
/// Global-level error.
2017-04-21 14:35:12 +03:00
Global(String),
2017-04-27 15:49:14 +03:00
/// Local-level error.
2017-04-21 14:35:12 +03:00
Local(String),
2017-04-27 15:49:14 +03:00
/// Stack-level error.
2017-04-26 12:37:27 +03:00
Stack(String),
2017-04-27 15:49:14 +03:00
/// Value-level error.
2017-04-21 14:35:12 +03:00
Value(String),
2017-04-27 15:49:14 +03:00
/// Interpreter (code) error.
2017-04-21 14:35:12 +03:00
Interpreter(String),
2017-05-04 12:01:21 +03:00
/// Env module error.
Env(String),
2017-05-18 15:08:55 +03:00
/// Native module error.
Native(String),
2017-04-27 15:49:14 +03:00
/// Trap.
2017-04-26 15:41:22 +03:00
Trap(String),
2017-04-21 14:35:12 +03:00
}
impl<E> From<Error> for InterpreterError<E> where E: CustomUserError {
fn from(other: Error) -> Self {
InterpreterError::Internal(other)
}
}
2017-04-21 14:35:12 +03:00
impl Into<String> for Error {
fn into(self) -> String {
match self {
Error::Program(s) => s,
2017-06-07 14:48:02 +03:00
Error::Validation(s) => s,
2017-04-21 14:35:12 +03:00
Error::Initialization(s) => s,
Error::Function(s) => s,
Error::Table(s) => s,
Error::Memory(s) => s,
Error::Variable(s) => s,
Error::Global(s) => s,
Error::Local(s) => s,
2017-04-26 12:37:27 +03:00
Error::Stack(s) => s,
2017-04-21 14:35:12 +03:00
Error::Interpreter(s) => s,
Error::Value(s) => s,
2017-05-04 12:01:21 +03:00
Error::Env(s) => s,
2017-05-18 15:08:55 +03:00
Error::Native(s) => s,
2017-04-26 15:41:22 +03:00
Error::Trap(s) => format!("trap: {}", s),
2017-04-21 14:35:12 +03:00
}
}
}
/// Dummy user error.
#[derive(Debug, Clone, PartialEq)]
pub struct DummyCustomUserError;
impl CustomUserError for DummyCustomUserError {}
impl ::std::fmt::Display for DummyCustomUserError {
fn fmt(&self, _f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { Ok(()) }
}
2017-05-04 11:25:25 +03:00
mod env;
2017-05-18 15:08:55 +03:00
mod env_native;
2017-04-21 14:35:12 +03:00
mod imports;
mod memory;
mod module;
mod program;
mod runner;
2017-04-26 12:37:27 +03:00
mod stack;
2017-04-21 14:35:12 +03:00
mod table;
2017-06-07 14:48:02 +03:00
mod validator;
2017-04-21 14:35:12 +03:00
mod value;
mod variable;
2017-04-27 14:22:02 +03:00
#[cfg(test)]
mod tests;
2017-04-27 14:44:03 +03:00
2017-05-19 09:36:50 +03:00
pub use self::memory::MemoryInstance;
pub use self::module::{ModuleInstance as CustomModuleInstance,
ModuleInstanceInterface as CustomModuleInstanceInterface,
ItemIndex, ExportEntryType, CallerContext, ExecutionParams, FunctionSignature};
2017-05-19 09:36:50 +03:00
pub use self::table::TableInstance;
pub use self::program::ProgramInstance as CustomProgramInstance;
2017-05-15 15:40:08 +03:00
pub use self::value::RuntimeValue;
2017-07-31 11:58:24 +03:00
pub use self::variable::{VariableInstance, VariableType, ExternalVariableValue};
pub use self::env_native::{env_native_module, UserDefinedElements, UserFunctionExecutor, UserFunctionDescriptor};
pub use self::env::EnvParams;
/// Default type of ProgramInstance if you do not need any custom user errors.
/// To work with custom user errors or interpreter internals, use CustomProgramInstance.
pub type ProgramInstance = self::program::ProgramInstance<DummyCustomUserError>;
/// Default type of ModuleInstance if you do not need any custom user errors.
/// To work with custom user errors or interpreter internals, use CustomModuleInstance.
pub type ModuleInstance = self::module::ModuleInstance<DummyCustomUserError>;
/// Default type of ModuleInstanceInterface if you do not need any custom user errors.
/// To work with custom user errors or interpreter internals, use CustomModuleInstanceInterface.
pub type ModuleInstanceInterface = self::module::ModuleInstanceInterface<DummyCustomUserError>;