Remove Backend dependency

This commit is contained in:
Syrus
2019-12-20 20:11:56 -08:00
parent d4e964519d
commit d7154fe791
15 changed files with 127 additions and 64 deletions

View File

@ -5,6 +5,7 @@
use crate::Module;
use memmap::Mmap;
use std::{
fmt,
fs::{create_dir_all, File},
io::{self, Write},
path::PathBuf,
@ -12,9 +13,27 @@ use std::{
use wasmer_runtime_core::cache::Error as CacheError;
pub use wasmer_runtime_core::{
backend::Backend,
cache::{Artifact, Cache, WasmHash},
cache::{Artifact, WasmHash},
};
pub use super::Backend;
/// A generic cache for storing and loading compiled wasm modules.
///
/// The `wasmer-runtime` supplies a naive `FileSystemCache` api.
pub trait Cache {
/// Error type to return when load error occurs
type LoadError: fmt::Debug;
/// Error type to return when store error occurs
type StoreError: fmt::Debug;
/// loads a module using the default `Backend`
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
/// loads a cached module using a specific `Backend`
fn load_with_backend(&self, key: WasmHash, backend: Backend)
-> Result<Module, Self::LoadError>;
/// Store a module into the cache with the given key
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
}
/// Representation of a directory that contains compiled wasm artifacts.
///
@ -105,7 +124,7 @@ impl Cache for FileSystemCache {
wasmer_runtime_core::load_cache_with(
serialized_cache,
crate::compiler_for_backend(backend)
.ok_or_else(|| CacheError::UnsupportedBackend(backend))?
.ok_or_else(|| CacheError::UnsupportedBackend(backend.to_string().to_owned()))?
.as_ref(),
)
}

View File

@ -91,7 +91,10 @@
//! [`wasmer-singlepass-backend`]: https://crates.io/crates/wasmer-singlepass-backend
//! [`wasmer-clif-backend`]: https://crates.io/crates/wasmer-clif-backend
pub use wasmer_runtime_core::backend::{Backend, Features};
#[macro_use]
extern crate serde_derive;
pub use wasmer_runtime_core::backend::Features;
pub use wasmer_runtime_core::codegen::{MiddlewareChain, StreamingCompiler};
pub use wasmer_runtime_core::export::Export;
pub use wasmer_runtime_core::global::Global;
@ -144,6 +147,52 @@ pub mod cache;
pub use wasmer_runtime_core::backend::{Compiler, CompilerConfig};
/// Enum used to select which compiler should be used to generate code.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub enum Backend {
#[cfg(feature = "singlepass")]
/// Singlepass backend
Singlepass,
#[cfg(feature = "cranelift")]
/// Cranelift backend
Cranelift,
#[cfg(feature = "llvm")]
/// LLVM backend
LLVM,
/// Auto backend
Auto,
}
impl Backend {
/// Stable string representation of the backend.
/// It can be used as part of a cache key, for example.
pub fn to_string(&self) -> &'static str {
match self {
#[cfg(feature = "cranelift")]
Backend::Cranelift => "cranelift",
#[cfg(feature = "singlepass")]
Backend::Singlepass => "singlepass",
#[cfg(feature = "llvm")]
Backend::LLVM => "llvm",
Backend::Auto => "auto",
}
}
}
impl Default for Backend {
fn default() -> Self {
#[cfg(all(feature = "default-backend-singlepass", not(feature = "docs")))]
return Backend::Singlepass;
#[cfg(any(feature = "default-backend-cranelift", feature = "docs"))]
return Backend::Cranelift;
#[cfg(all(feature = "default-backend-llvm", not(feature = "docs")))]
return Backend::LLVM;
}
}
/// Compile WebAssembly binary code into a [`Module`].
/// This function is useful if it is necessary to
/// compile a module before it can be instantiated
@ -268,9 +317,6 @@ pub fn compiler_for_backend(backend: Backend) -> Option<Box<dyn Compiler>> {
#[cfg(feature = "default-backend-llvm")]
return Some(Box::new(wasmer_llvm_backend::LLVMCompiler::new()));
}
#[cfg(not(all(feature = "llvm", feature = "singlepass", feature = "cranelift")))]
_ => None,
}
}