2017-04-27 15:49:14 +03:00
|
|
|
//! WebAssembly interpreter module.
|
2017-04-21 14:35:12 +03:00
|
|
|
|
2017-11-25 22:28:15 +03:00
|
|
|
use std::any::TypeId;
|
|
|
|
|
2017-08-01 13:16:25 +03:00
|
|
|
/// Custom user error.
|
2017-11-25 22:28:15 +03:00
|
|
|
pub trait UserError: 'static + ::std::fmt::Display + ::std::fmt::Debug {
|
|
|
|
#[doc(hidden)]
|
2017-11-26 00:29:33 +03:00
|
|
|
fn __private_get_type_id__(&self) -> TypeId {
|
|
|
|
TypeId::of::<Self>()
|
|
|
|
}
|
2017-11-25 22:28:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserError {
|
2017-11-26 00:29:33 +03:00
|
|
|
/// Attempt to downcast this `UserError` to a concrete type by reference.
|
2017-11-25 22:28:15 +03:00
|
|
|
pub fn downcast_ref<T: UserError>(&self) -> Option<&T> {
|
2017-11-26 00:29:33 +03:00
|
|
|
if self.__private_get_type_id__() == TypeId::of::<T>() {
|
|
|
|
unsafe { Some(&*(self as *const UserError as *const T)) }
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 22:28:15 +03:00
|
|
|
|
2017-11-26 00:29:33 +03:00
|
|
|
/// Attempt to downcast this `UserError` to a concrete type by mutable
|
|
|
|
/// reference.
|
2017-11-25 22:28:15 +03:00
|
|
|
pub fn downcast_mut<T: UserError>(&mut self) -> Option<&mut T> {
|
|
|
|
if self.__private_get_type_id__() == TypeId::of::<T>() {
|
2017-11-26 00:29:33 +03:00
|
|
|
unsafe { Some(&mut *(self as *mut UserError as *mut T)) }
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 13:16:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal interpreter error.
|
2017-11-25 22:28:15 +03:00
|
|
|
#[derive(Debug)]
|
2017-11-25 22:55:45 +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-08-01 14:44:33 +03:00
|
|
|
/// Custom user error.
|
2017-11-25 22:28:15 +03:00
|
|
|
User(Box<UserError>),
|
2017-04-21 14:35:12 +03:00
|
|
|
}
|
|
|
|
|
2017-11-25 22:55:45 +03:00
|
|
|
impl Into<String> for Error {
|
2017-04-21 14:35:12 +03:00
|
|
|
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-08-01 14:44:33 +03:00
|
|
|
Error::User(e) => format!("user: {}", e),
|
2017-04-21 14:35:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:55:45 +03:00
|
|
|
impl ::std::fmt::Display for Error {
|
2017-09-05 14:12:54 +02:00
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
|
|
|
|
match *self {
|
|
|
|
Error::Program(ref s) => write!(f, "Program: {}", s),
|
|
|
|
Error::Validation(ref s) => write!(f, "Validation: {}", s),
|
|
|
|
Error::Initialization(ref s) => write!(f, "Initialization: {}", s),
|
|
|
|
Error::Function(ref s) => write!(f, "Function: {}", s),
|
|
|
|
Error::Table(ref s) => write!(f, "Table: {}", s),
|
|
|
|
Error::Memory(ref s) => write!(f, "Memory: {}", s),
|
|
|
|
Error::Variable(ref s) => write!(f, "Variable: {}", s),
|
|
|
|
Error::Global(ref s) => write!(f, "Global: {}", s),
|
|
|
|
Error::Local(ref s) => write!(f, "Local: {}", s),
|
|
|
|
Error::Stack(ref s) => write!(f, "Stack: {}", s),
|
|
|
|
Error::Interpreter(ref s) => write!(f, "Interpreter: {}", s),
|
|
|
|
Error::Value(ref s) => write!(f, "Value: {}", s),
|
|
|
|
Error::Env(ref s) => write!(f, "Env: {}", s),
|
|
|
|
Error::Native(ref s) => write!(f, "Native: {}", s),
|
|
|
|
Error::Trap(ref s) => write!(f, "Trap: {}", s),
|
|
|
|
Error::User(ref e) => write!(f, "User: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:55:45 +03:00
|
|
|
impl<U> From<U> for Error where U: UserError + Sized {
|
2017-09-05 12:09:12 +02:00
|
|
|
fn from(e: U) -> Self {
|
2017-11-25 22:28:15 +03:00
|
|
|
Error::User(Box::new(e))
|
2017-09-05 12:09:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:37:39 +03:00
|
|
|
mod emscripten;
|
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;
|
2017-08-01 14:44:33 +03:00
|
|
|
pub use self::module::{ModuleInstance, ModuleInstanceInterface,
|
2017-08-01 13:16:25 +03:00
|
|
|
ItemIndex, ExportEntryType, CallerContext, ExecutionParams, FunctionSignature};
|
2017-05-19 09:36:50 +03:00
|
|
|
pub use self::table::TableInstance;
|
2017-08-01 14:44:33 +03:00
|
|
|
pub use self::program::ProgramInstance;
|
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};
|
2017-11-27 15:37:39 +03:00
|
|
|
pub use self::emscripten::EnvParams;
|