Refine new api

This commit is contained in:
Lachlan Sneff 2019-04-19 10:58:51 -07:00
parent 9ae789ff4f
commit 73cb47df1d
12 changed files with 156 additions and 105 deletions

View File

@ -16,7 +16,7 @@ use target_lexicon::Triple;
use wasmer_runtime_core::cache::{Artifact, Error as CacheError};
use wasmer_runtime_core::{
backend::{Compiler},
backend::Compiler,
config::CompileConfig,
error::{CompileError, CompileResult},
module::ModuleInner,
@ -40,11 +40,7 @@ impl CraneliftCompiler {
impl Compiler for CraneliftCompiler {
/// Compiles wasm binary to a wasmer module.
fn compile(
&self,
wasm: &[u8],
compiler_config: CompileConfig,
) -> CompileResult<ModuleInner> {
fn compile(&self, wasm: &[u8], compiler_config: CompileConfig) -> CompileResult<ModuleInner> {
validate(wasm)?;
let isa = get_isa();

View File

@ -1,7 +1,7 @@
#![cfg_attr(nightly, feature(unwind_attributes))]
use wasmer_runtime_core::{
backend::{Compiler},
backend::Compiler,
cache::{Artifact, Error as CacheError},
config::CompileConfig,
error::CompileError,

View File

@ -3,27 +3,34 @@ use crate::module::ResourceIndex;
pub struct Allowed {
pub float_ops: bool,
pub indirect_calls: bool,
_non_exhaustive: (),
#[doc(hidden)]
#[deprecated(note = "This field exists to make this struct non-exhaustive.")]
pub __non_exhaustive: (),
}
impl Default for Allowed {
fn default() -> Self {
#[allow(deprecated)]
Self {
float_ops: true,
indirect_calls: true,
_non_exhaustive: (),
__non_exhaustive: (),
}
}
}
pub struct Metering {
_non_exhaustive: (),
#[doc(hidden)]
#[deprecated(note = "This field exists to make this struct non-exhaustive.")]
pub __non_exhaustive: (),
}
impl Default for Metering {
fn default() -> Self {
#[allow(deprecated)]
Self {
_non_exhaustive: (),
__non_exhaustive: (),
}
}
}

View File

@ -8,9 +8,9 @@ use std::collections::VecDeque;
use std::{
cell::{Ref, RefCell},
ffi::c_void,
iter,
marker::PhantomData,
rc::Rc,
iter,
};
pub enum NamespaceItem<'a, Data = ()> {
@ -31,17 +31,15 @@ impl<'a, Data> NamespaceItem<'a, Data> {
Inst(ExportIter<'a, Data>),
Ns(HashMapIter<'a, String, Export<'a>>),
}
let mut export_iter = match self {
NamespaceItem::Instance(inst) => Iter::Inst(inst.exports()),
NamespaceItem::Namespace(ns) => Iter::Ns(ns.map.iter()),
};
iter::from_fn(move || {
match &mut export_iter {
Iter::Inst(iter) => iter.next(),
Iter::Ns(ns) => ns.next().map(|(name, export)| (&**name, export.clone())),
}
iter::from_fn(move || match &mut export_iter {
Iter::Inst(iter) => iter.next(),
Iter::Ns(ns) => ns.next().map(|(name, export)| (&**name, export.clone())),
})
}

View File

@ -69,7 +69,6 @@ pub fn compile_with(
wasm: &[u8],
compiler: &dyn backend::Compiler,
) -> CompileResult<module::Module> {
let metering = config::Metering::default();
let allowed = config::Allowed::default();
let config = config::CompileConfig {

View File

@ -87,7 +87,8 @@ pub struct Module {
}
impl Module {
pub(crate) fn new(inner: Arc<ModuleInner>) -> Self {
#[doc(hidden)]
pub fn new(inner: Arc<ModuleInner>) -> Self {
Module { inner }
}

View File

@ -1,4 +1,7 @@
use wasmer_runtime::{Compiler, error, imports, Ctx, Func, Value, backends::Cranelift};
use wasmer_runtime::{
config::{Backend, Metering},
error, imports, Compiler, Ctx, Func, Value,
};
use wabt::wat2wasm;
@ -36,7 +39,7 @@ fn foobar(_ctx: &mut Ctx) -> i32 {
42
}
fn do_panic(ctx: &mut Ctx<usize>) -> Result<i32, String> {
fn do_panic(ctx: &mut Ctx<i32>) -> Result<i32, String> {
println!("the number was {}", ctx.data);
Err("test error".to_string())
}
@ -59,13 +62,19 @@ fn main() -> Result<(), error::Error> {
let wasm = get_wasm();
let compiler: Compiler<Cranelift> = Compiler::new().build();
let compiler = Compiler::new()
.backend(Backend::LLVM)
.metering(Metering {
/* ... */
..Metering::default()
})
.build();
let module = compiler.module(&wasm).compile()?;
let instance = module.instantiate(&imports)?;
let foo: Func<(), i32> = instance.func("dbz")?;
let foo: Func<(), i32, _> = instance.func("dbz")?;
let result = foo.call();

View File

@ -1,44 +0,0 @@
use wasmer_runtime_core::{config::CompileConfig, error::CompileResult, module::Module, backend::Compiler};
pub trait Backend {
fn compile(wasm: &[u8], config: CompileConfig) -> CompileResult<Module>;
}
#[cfg(feature = "backend:singlepass")]
pub struct SinglePass {
_private: (),
}
#[cfg(feature = "backend:singlepass")]
impl Backend for SinglePass {
fn compile(wasm: &[u8], config: CompileConfig) -> CompileResult<Module> {
use wasmer_singlepass_backend::SinglePassCompiler;
SinglePassCompiler::new().compile(wasm, config)
}
}
#[cfg(feature = "backend:cranelift")]
pub struct Cranelift {
_private: (),
}
#[cfg(feature = "backend:cranelift")]
impl Backend for Cranelift {
fn compile(wasm: &[u8], config: CompileConfig) -> CompileResult<Module> {
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new().compile(wasm, config)
}
}
#[cfg(feature = "backend:llvm")]
pub struct Llvm {
_private: (),
}
#[cfg(feature = "backend:llvm")]
impl Backend for Llvm {
fn compile(wasm: &[u8], config: CompileConfig) -> CompileResult<Module> {
use wasmer_llvm_backend::LLVMCompiler;
LLVMCompiler::new().compile(wasm, config)
}
}

View File

@ -1,65 +1,119 @@
pub use wasmer_runtime_core::config::{Allowed, Metering};
use wasmer_runtime_core::{
backend::{Backend, Compiler as _},
config::CompileConfig,
error::CompileResult,
module::{Module, ResourceIndex},
};
use crate::backends::Backend;
// use crate::backends::Backend;
use std::marker::PhantomData;
pub struct CompilerBuilder<B: Backend> {
pub struct CompilerBuilder {
metering: Metering,
allowed: Allowed,
_phantom: PhantomData<B>,
backend: Backend,
}
impl<B: Backend> CompilerBuilder<B> {
pub fn metering(&mut self, metering: Metering) -> &mut Self {
impl CompilerBuilder {
pub fn metering(mut self, metering: Metering) -> Self {
self.metering = metering;
self
}
pub fn allowed(&mut self, allowed: Allowed) -> &mut Self {
pub fn allowed(mut self, allowed: Allowed) -> Self {
self.allowed = allowed;
self
}
pub fn build(self) -> Compiler<B> {
pub fn backend(mut self, backend: Backend) -> Self {
self.backend = backend;
self
}
pub fn build(self) -> Compiler {
Compiler {
metering: self.metering,
allowed: self.allowed,
_phantom: PhantomData,
backend: self.backend,
}
}
}
pub struct ModuleBuilder<'a, B: Backend> {
pub struct ModuleBuilder<'a> {
wasm: &'a [u8],
metering: &'a Metering,
allowed: &'a Allowed,
backend: Backend,
symbol_mapper: Option<Box<dyn Fn(ResourceIndex) -> Option<String>>>,
_phantom: PhantomData<B>,
}
impl<'a, B: Backend> ModuleBuilder<'a, B> {
impl<'a> ModuleBuilder<'a> {
pub fn map_symbols(
&mut self,
mut self,
mapper: impl Fn(ResourceIndex) -> Option<String> + 'static,
) -> &mut Self {
) -> Self {
self.symbol_mapper = Some(Box::new(mapper));
self
}
pub fn compile(self) -> CompileResult<Module> {
B::compile(
self.wasm,
CompileConfig {
metering: self.metering,
allowed: self.allowed,
symbol_map: self.symbol_mapper,
},
)
match self.backend {
Backend::Singlepass => {
#[cfg(feature = "backend:singlepass")]
{
use wasmer_singlepass_backend::SinglePassCompiler;
SinglePassCompiler::new()
.compile(
self.wasm,
CompileConfig {
metering: self.metering,
allowed: self.allowed,
symbol_map: self.symbol_mapper,
},
)
.map(|module| Module::new(module.into()))
}
#[cfg(not(feature = "backend:singlepass"))]
unimplemented!("the singlepass backend is not enabled, try rebuilding with the \"backend:singlepass\" feature enabled")
}
Backend::Cranelift => {
#[cfg(feature = "backend:cranelift")]
{
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
.compile(
self.wasm,
CompileConfig {
metering: self.metering,
allowed: self.allowed,
symbol_map: self.symbol_mapper,
},
)
.map(|module| Module::new(module.into()))
}
#[cfg(not(feature = "backend:cranelift"))]
unimplemented!("the cranelift backend is not enabled, try rebuilding with the \"backend:cranelift\" feature enabled")
}
Backend::LLVM => {
#[cfg(feature = "backend:llvm")]
{
use wasmer_llvm_backend::LLVMCompiler;
LLVMCompiler::new()
.compile(
self.wasm,
CompileConfig {
metering: self.metering,
allowed: self.allowed,
symbol_map: self.symbol_mapper,
},
)
.map(|module| Module::new(module.into()))
}
#[cfg(not(feature = "backend:llvm"))]
unimplemented!("the llvm backend is not enabled, try rebuilding with the \"backend:llvm\" feature enabled")
}
}
}
}
@ -80,28 +134,28 @@ impl<'a, B: Backend> ModuleBuilder<'a, B> {
///
///
/// ```
pub struct Compiler<B: Backend> {
pub struct Compiler {
metering: Metering,
allowed: Allowed,
_phantom: PhantomData<B>,
backend: Backend,
}
impl<B: Backend> Compiler<B> {
pub fn new() -> CompilerBuilder<B> {
impl Compiler {
pub fn new() -> CompilerBuilder {
CompilerBuilder {
metering: Default::default(),
allowed: Default::default(),
_phantom: PhantomData,
backend: Backend::Cranelift,
}
}
pub fn module<'a>(&'a self, wasm: &'a [u8]) -> ModuleBuilder<'a, B> {
pub fn module<'a>(&'a self, wasm: &'a [u8]) -> ModuleBuilder<'a> {
ModuleBuilder {
wasm,
metering: &self.metering,
allowed: &self.allowed,
backend: self.backend,
symbol_mapper: None,
_phantom: PhantomData,
}
}
}

View File

@ -6,6 +6,7 @@ use std::{
path::PathBuf,
};
use wasmer_runtime_core::backend::Backend;
use wasmer_runtime_core::cache::Error as CacheError;
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash, WASMER_VERSION_HASH};
@ -94,7 +95,41 @@ impl Cache for FileSystemCache {
let mmap = unsafe { Mmap::map(&file)? };
let serialized_cache = Artifact::deserialize(&mmap[..])?;
unsafe { wasmer_runtime_core::load_cache_with(serialized_cache, super::default_compiler()) }
let backend = serialized_cache.info().backend;
let load_cache =
|backend| unsafe { wasmer_runtime_core::load_cache_with(serialized_cache, backend) };
match backend {
Backend::Singlepass => {
#[cfg(feature = "backend:singlepass")]
{
use wasmer_singlepass_backend::SinglePassCompiler;
load_cache(&SinglePassCompiler::new())
}
#[cfg(not(feature = "backend:singlepass"))]
unimplemented!("the singlepass backend is not enabled, try rebuilding with the \"backend:singlepass\" feature enabled")
}
Backend::Cranelift => {
#[cfg(feature = "backend:cranelift")]
{
use wasmer_clif_backend::CraneliftCompiler;
load_cache(&CraneliftCompiler::new())
}
#[cfg(not(feature = "backend:cranelift"))]
unimplemented!("the cranelift backend is not enabled, try rebuilding with the \"backend:cranelift\" feature enabled")
}
Backend::LLVM => {
#[cfg(feature = "backend:llvm")]
{
use wasmer_llvm_backend::LLVMCompiler;
load_cache(&LLVMCompiler::new())
}
#[cfg(not(feature = "backend:llvm"))]
unimplemented!("the llvm backend is not enabled, try rebuilding with the \"backend:llvm\" feature enabled")
}
}
}
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> {

View File

@ -110,16 +110,16 @@ pub mod units {
pub use wasmer_runtime_core::units::{Bytes, Pages};
}
pub mod backends;
pub mod builder;
pub mod cache;
use wasmer_runtime_core::config::CompileConfig;
#[doc(inline)]
pub use self::builder::Compiler;
use wasmer_runtime_core::config::CompileConfig;
pub mod config {
pub use super::builder::{Allowed, Metering};
pub use wasmer_runtime_core::backend::Backend;
}
// /// Compile WebAssembly binary code into a [`Module`].

View File

@ -29,8 +29,8 @@ use crate::codegen::{CodegenError, ModuleCodeGenerator};
use crate::parse::LoadError;
use wasmer_runtime_core::{
backend::{sys::Memory, Backend, CacheGen, Compiler},
config::CompileConfig,
cache::{Artifact, Error as CacheError},
config::CompileConfig,
error::{CompileError, CompileResult},
module::{ModuleInfo, ModuleInner},
};
@ -55,11 +55,7 @@ impl SinglePassCompiler {
}
impl Compiler for SinglePassCompiler {
fn compile(
&self,
wasm: &[u8],
compile_config: CompileConfig,
) -> CompileResult<ModuleInner> {
fn compile(&self, wasm: &[u8], compile_config: CompileConfig) -> CompileResult<ModuleInner> {
let mut mcg = codegen_x64::X64ModuleCodeGenerator::new();
let info = parse::read_module(wasm, Backend::Singlepass, &mut mcg, compile_config)?;
let exec_context = mcg.finalize(&info)?;