161 lines
4.1 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-12-18 16:06:56 +03:00
// TODO(pepyakin): Fix these asap
2017-12-18 13:04:05 +03:00
#![allow(deprecated)]
2017-12-18 16:06:56 +03:00
#![allow(missing_docs)]
2017-12-18 13:04:05 +03:00
2017-11-25 22:28:15 +03:00
use std::any::TypeId;
2017-12-01 15:35:01 +03:00
use validation;
2017-12-05 18:12:10 +01:00
use common;
2017-11-25 22:28:15 +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
}
}
}
/// 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),
/// Error while instantiating a module. Might occur when provided
/// with incorrect exports (i.e. linkage failure).
2017-12-18 20:31:47 +03:00
Instantiation(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-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-12-18 20:31:47 +03:00
Error::Instantiation(s) => s,
2017-04-21 14:35:12 +03:00
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-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),
2017-12-18 20:31:47 +03:00
Error::Instantiation(ref s) => write!(f, "Instantiation: {}", s),
2017-09-05 14:12:54 +02:00
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::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-12-01 15:35:01 +03:00
impl From<validation::Error> for Error {
fn from(e: validation::Error) -> Self {
Error::Validation(e.to_string())
}
}
2017-12-05 18:12:10 +01:00
impl From<common::stack::Error> for Error {
fn from(e: common::stack::Error) -> Self {
2017-12-01 15:35:01 +03:00
Error::Stack(e.to_string())
}
}
2017-04-21 14:35:12 +03:00
mod memory;
mod module;
mod program;
mod runner;
mod table;
mod value;
2017-12-11 16:28:05 +01:00
mod host;
2017-12-13 16:57:28 +01:00
mod imports;
2017-12-13 18:19:42 +01:00
mod global;
2017-12-13 18:28:34 +01:00
mod func;
2017-04-27 14:22:02 +03:00
#[cfg(test)]
mod tests;
2017-04-27 14:44:03 +03:00
2018-01-05 16:22:20 +03:00
pub use self::memory::{MemoryInstance, MemoryRef};
pub use self::table::{TableInstance, TableRef};
2017-08-01 14:44:33 +03:00
pub use self::program::ProgramInstance;
pub use self::value::{RuntimeValue, TryInto};
pub use self::host::{Externals, HostFuncIndex, EmptyExternals};
pub use self::imports::{ImportResolver, Imports};
pub use self::module::{ModuleInstance, ModuleRef};
2018-01-05 16:22:20 +03:00
pub use self::global::{GlobalInstance, GlobalRef};
pub use self::func::{FuncInstance, FuncRef};