wasmi/src/lib.rs

177 lines
4.3 KiB
Rust
Raw Normal View History

2018-01-17 15:32:33 +00:00
//! WebAssembly interpreter module.
2018-01-22 17:22:22 +00:00
// TODO(pepyakin): Fix this asap https://github.com/pepyakin/wasmi/issues/3
2018-01-17 15:32:33 +00:00
#![allow(missing_docs)]
#[cfg(test)]
extern crate wabt;
extern crate parity_wasm;
extern crate byteorder;
use std::fmt;
use std::error;
2018-01-22 13:11:20 +00:00
use std::collections::HashMap;
use parity_wasm::elements::Module;
2018-01-17 15:32:33 +00:00
/// Internal interpreter error.
#[derive(Debug)]
pub enum Error {
2018-01-22 13:11:20 +00:00
/// Module validation error. Might occur only at load time.
Validation(String),
2018-01-17 15:32:33 +00:00
/// Error while instantiating a module. Might occur when provided
/// with incorrect exports (i.e. linkage failure).
Instantiation(String),
/// Function-level error.
Function(String),
/// Table-level error.
Table(String),
/// Memory-level error.
Memory(String),
/// Global-level error.
Global(String),
/// Stack-level error.
Stack(String),
/// Value-level error.
Value(String),
/// Trap.
Trap(String),
/// Custom embedder error.
Host(Box<host::HostError>),
}
impl Into<String> for Error {
fn into(self) -> String {
match self {
2018-01-22 13:11:20 +00:00
Error::Validation(s) => s,
2018-01-17 15:32:33 +00:00
Error::Instantiation(s) => s,
Error::Function(s) => s,
Error::Table(s) => s,
Error::Memory(s) => s,
Error::Global(s) => s,
Error::Stack(s) => s,
Error::Value(s) => s,
Error::Trap(s) => format!("trap: {}", s),
Error::Host(e) => format!("user: {}", e),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
2018-01-22 13:11:20 +00:00
Error::Validation(ref s) => write!(f, "Validation: {}", s),
2018-01-17 15:32:33 +00:00
Error::Instantiation(ref s) => write!(f, "Instantiation: {}", 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::Global(ref s) => write!(f, "Global: {}", s),
Error::Stack(ref s) => write!(f, "Stack: {}", s),
Error::Value(ref s) => write!(f, "Value: {}", s),
Error::Trap(ref s) => write!(f, "Trap: {}", s),
Error::Host(ref e) => write!(f, "User: {}", e),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
2018-01-22 13:11:20 +00:00
Error::Validation(ref s) => s,
2018-01-17 15:32:33 +00:00
Error::Instantiation(ref s) => s,
Error::Function(ref s) => s,
Error::Table(ref s) => s,
Error::Memory(ref s) => s,
Error::Global(ref s) => s,
Error::Stack(ref s) => s,
Error::Value(ref s) => s,
Error::Trap(ref s) => s,
Error::Host(_) => "Host error",
}
}
}
2018-01-22 13:11:20 +00:00
2018-01-17 15:32:33 +00:00
impl<U> From<U> for Error where U: host::HostError + Sized {
fn from(e: U) -> Self {
Error::Host(Box::new(e))
}
}
2018-01-22 13:11:20 +00:00
impl From<validation::Error> for Error {
fn from(e: validation::Error) -> Error {
Error::Validation(e.to_string())
}
}
2018-01-17 15:32:33 +00:00
impl From<::common::stack::Error> for Error {
fn from(e: ::common::stack::Error) -> Self {
Error::Stack(e.to_string())
}
}
mod validation;
mod common;
mod memory;
mod module;
mod runner;
mod table;
mod value;
mod host;
mod imports;
mod global;
mod func;
2018-01-18 12:54:31 +00:00
mod types;
2018-01-17 15:32:33 +00:00
#[cfg(test)]
mod tests;
pub use self::memory::{MemoryInstance, MemoryRef};
pub use self::table::{TableInstance, TableRef};
pub use self::value::{RuntimeValue, TryInto};
2018-01-23 11:26:45 +00:00
pub use self::host::{Externals, NopExternals, HostError, RuntimeArgs};
2018-01-17 15:32:33 +00:00
pub use self::imports::{ModuleImportResolver, ImportResolver, ImportsBuilder};
pub use self::module::{ModuleInstance, ModuleRef, ExternVal, NotStartedModuleRef};
pub use self::global::{GlobalInstance, GlobalRef};
pub use self::func::{FuncInstance, FuncRef};
2018-01-22 13:34:32 +00:00
pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor, MemoryDescriptor};
2018-01-22 13:11:20 +00:00
pub struct LoadedModule {
labels: HashMap<usize, HashMap<usize, usize>>,
module: Module,
}
impl LoadedModule {
pub(crate) fn module(&self) -> &Module {
&self.module
}
pub(crate) fn labels(&self) -> &HashMap<usize, HashMap<usize, usize>> {
&self.labels
}
pub fn into_module(self) -> Module {
self.module
}
}
pub fn load_from_module(module: Module) -> Result<LoadedModule, Error> {
use validation::{validate_module, ValidatedModule};
let ValidatedModule {
labels,
module,
} = validate_module(module)?;
Ok(LoadedModule {
labels,
module,
})
}
pub fn load_from_buffer<B: AsRef<[u8]>>(buffer: B) -> Result<LoadedModule, Error> {
let module = parity_wasm::elements::deserialize_buffer(buffer.as_ref())
.map_err(|e: parity_wasm::elements::Error| Error::Validation(e.to_string()))?;
load_from_module(module)
}