Merge pull request #562 from alexcrichton/less-public-api-dependencies

Reduce number of crates needed for `Config` usage
This commit is contained in:
Nick Fitzgerald
2019-11-13 10:02:51 -08:00
committed by GitHub
8 changed files with 88 additions and 96 deletions

View File

@@ -1,8 +1,8 @@
use crate::Config;
use alloc::rc::Rc; use alloc::rc::Rc;
use core::cell::{RefCell, RefMut}; use core::cell::{RefCell, RefMut};
use core::hash::{Hash, Hasher}; use core::hash::{Hash, Hasher};
use cranelift_codegen::settings; use wasmtime_jit::{Compiler, Features};
use wasmtime_jit::{CompilationStrategy, Compiler, Features};
#[derive(Clone)] #[derive(Clone)]
pub struct Context { pub struct Context {
@@ -12,21 +12,19 @@ pub struct Context {
} }
impl Context { impl Context {
pub fn new(compiler: Compiler, features: Features, debug_info: bool) -> Context { pub fn new(config: &Config) -> Context {
Context { let isa_builder =
compiler: Rc::new(RefCell::new(compiler)), cranelift_native::builder().expect("host machine is not a supported target");
features, let isa = isa_builder.finish(config.flags.clone());
debug_info, Context::new_with_compiler(config, Compiler::new(isa, config.strategy))
}
} }
pub fn create( pub fn new_with_compiler(config: &Config, compiler: Compiler) -> Context {
flags: settings::Flags, Context {
features: Features, compiler: Rc::new(RefCell::new(compiler)),
debug_info: bool, features: config.features.clone(),
strategy: CompilationStrategy, debug_info: config.debug_info,
) -> Context { }
Context::new(create_compiler(flags, strategy), features, debug_info)
} }
pub(crate) fn debug_info(&self) -> bool { pub(crate) fn debug_info(&self) -> bool {
@@ -54,13 +52,3 @@ impl PartialEq for Context {
Rc::ptr_eq(&self.compiler, &other.compiler) Rc::ptr_eq(&self.compiler, &other.compiler)
} }
} }
pub(crate) fn create_compiler(flags: settings::Flags, strategy: CompilationStrategy) -> Compiler {
let isa = {
let isa_builder =
cranelift_native::builder().expect("host machine is not a supported target");
isa_builder.finish(flags)
};
Compiler::new(isa, strategy)
}

View File

@@ -15,15 +15,23 @@ fn default_flags() -> settings::Flags {
settings::Flags::new(flag_builder) settings::Flags::new(flag_builder)
} }
/// Global configuration options used to create an [`Engine`] and customize its
/// behavior.
///
/// This structure exposed a builder-like interface and is primarily consumed by
/// [`Engine::new()`]
#[derive(Clone)]
pub struct Config { pub struct Config {
flags: settings::Flags, pub(crate) flags: settings::Flags,
features: Features, pub(crate) features: Features,
debug_info: bool, pub(crate) debug_info: bool,
strategy: CompilationStrategy, pub(crate) strategy: CompilationStrategy,
} }
impl Config { impl Config {
pub fn default() -> Config { /// Creates a new configuration object with the default configuration
/// specified.
pub fn new() -> Config {
Config { Config {
debug_info: false, debug_info: false,
features: Default::default(), features: Default::default(),
@@ -32,54 +40,68 @@ impl Config {
} }
} }
pub fn new( /// Configures whether DWARF debug information will be emitted during
flags: settings::Flags, /// compilation.
features: Features, ///
debug_info: bool, /// By default this option is `false`.
strategy: CompilationStrategy, pub fn debug_info(&mut self, enable: bool) -> &mut Self {
) -> Config { self.debug_info = enable;
Config { self
flags,
features,
debug_info,
strategy,
}
} }
pub(crate) fn debug_info(&self) -> bool { /// Configures various flags for compilation such as optimization level and
self.debug_info /// such.
///
/// For more information on defaults and configuration options, see the
/// documentation for [`Flags`](settings::Flags)
pub fn flags(&mut self, flags: settings::Flags) -> &mut Self {
self.flags = flags;
self
} }
pub(crate) fn flags(&self) -> &settings::Flags { /// Indicates which WebAssembly features are enabled for this compilation
&self.flags /// session.
///
/// By default only stable features are enabled by default (and none are
/// fully stabilized yet at this time). If you're loading wasm modules
/// which may use non-MVP features you'll want to be sure to call this
/// method and enable the appropriate feature in the [`Features`]
/// structure.
pub fn features(&mut self, features: Features) -> &mut Self {
self.features = features;
self
} }
pub(crate) fn features(&self) -> &Features { /// Configures the compilation `strategy` provided, indicating which
&self.features /// backend will be used for compiling WebAssembly to native code.
///
/// Currently the primary strategies are with cranelift (an optimizing
/// compiler) or lightbeam (a fast single-pass JIT which produces code
/// quickly).
pub fn strategy(&mut self, strategy: CompilationStrategy) -> &mut Self {
self.strategy = strategy;
self
} }
}
pub(crate) fn strategy(&self) -> CompilationStrategy { impl Default for Config {
self.strategy fn default() -> Config {
Config::new()
} }
} }
// Engine // Engine
#[derive(Default)]
pub struct Engine { pub struct Engine {
config: Config, config: Config,
} }
impl Engine { impl Engine {
pub fn new(config: Config) -> Engine { pub fn new(config: &Config) -> Engine {
Engine { config } Engine {
} config: config.clone(),
}
pub fn default() -> Engine {
Engine::new(Config::default())
}
pub(crate) fn config(&self) -> &Config {
&self.config
} }
} }
@@ -94,13 +116,9 @@ pub struct Store {
impl Store { impl Store {
pub fn new(engine: &HostRef<Engine>) -> Store { pub fn new(engine: &HostRef<Engine>) -> Store {
let flags = engine.borrow().config().flags().clone();
let features = engine.borrow().config().features().clone();
let debug_info = engine.borrow().config().debug_info();
let strategy = engine.borrow().config().strategy();
Store { Store {
engine: engine.clone(), engine: engine.clone(),
context: Context::create(flags, features, debug_info, strategy), context: Context::new(&engine.borrow().config),
global_exports: Rc::new(RefCell::new(HashMap::new())), global_exports: Rc::new(RefCell::new(HashMap::new())),
signature_cache: HashMap::new(), signature_cache: HashMap::new(),
} }

View File

@@ -24,7 +24,7 @@ fn test_import_calling_export() {
} }
} }
let engine = HostRef::new(Engine::new(Config::default())); let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine)); let store = HostRef::new(Store::new(&engine));
let module = HostRef::new( let module = HostRef::new(
Module::new( Module::new(

View File

@@ -15,8 +15,6 @@ test = false
doctest = false doctest = false
[dependencies] [dependencies]
cranelift-codegen = "0.50.0"
cranelift-native = "0.50.0"
wasmtime-interface-types = { path = "../../interface-types" } wasmtime-interface-types = { path = "../../interface-types" }
wasmtime-jit = { path = "../../jit" } wasmtime-jit = { path = "../../jit" }
wasmtime-rust-macro = { path = "./macro" } wasmtime-rust-macro = { path = "./macro" }

View File

@@ -51,17 +51,12 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
use #root::wasmtime_api::{HostRef, Config, Extern, Engine, Store, Instance, Module}; use #root::wasmtime_api::{HostRef, Config, Extern, Engine, Store, Instance, Module};
use #root::anyhow::{bail, format_err}; use #root::anyhow::{bail, format_err};
let config = { let mut config = Config::new();
let flag_builder = #root::cranelift_codegen::settings::builder(); config.features(#root::wasmtime_jit::Features {
let flags = #root::cranelift_codegen::settings::Flags::new(flag_builder); multi_value: true,
let features = #root::wasmtime_jit::Features { ..Default::default()
multi_value: true, });
..Default::default() let engine = HostRef::new(Engine::new(&config));
};
let strategy = #root::wasmtime_jit::CompilationStrategy::Auto;
Config::new(flags, features, false, strategy)
};
let engine = HostRef::new(Engine::new(config));
let store = HostRef::new(Store::new(&engine)); let store = HostRef::new(Store::new(&engine));
let global_exports = store.borrow().global_exports().clone(); let global_exports = store.borrow().global_exports().clone();

View File

@@ -4,8 +4,6 @@ pub use wasmtime_rust_macro::wasmtime;
#[doc(hidden)] #[doc(hidden)]
pub mod __rt { pub mod __rt {
pub use anyhow; pub use anyhow;
pub use cranelift_codegen;
pub use cranelift_native;
pub use wasmtime_api; pub use wasmtime_api;
pub use wasmtime_interface_types; pub use wasmtime_interface_types;
pub use wasmtime_jit; pub use wasmtime_jit;

View File

@@ -3,7 +3,6 @@ use cranelift_codegen::settings::{self, Configurable};
use std::fs::File; use std::fs::File;
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
use wasmtime_api::{Config, Engine, HostRef, Instance, Module, Store}; use wasmtime_api::{Config, Engine, HostRef, Instance, Module, Store};
use wasmtime_jit::{CompilationStrategy, Features};
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> { pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
// Prepare runtime // Prepare runtime
@@ -14,13 +13,9 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
.enable("avoid_div_traps") .enable("avoid_div_traps")
.context("error while enabling proper division trap")?; .context("error while enabling proper division trap")?;
let config = Config::new( let mut config = Config::new();
settings::Flags::new(flag_builder), config.flags(settings::Flags::new(flag_builder));
Features::default(), let engine = HostRef::new(Engine::new(&config));
false,
CompilationStrategy::Auto,
);
let engine = HostRef::new(Engine::new(config));
let store = HostRef::new(Store::new(&engine)); let store = HostRef::new(Store::new(&engine));
let mut module_registry = HashMap::new(); let mut module_registry = HashMap::new();

View File

@@ -267,13 +267,13 @@ fn main() -> Result<()> {
// Decide how to compile. // Decide how to compile.
let strategy = pick_compilation_strategy(args.flag_cranelift, args.flag_lightbeam); let strategy = pick_compilation_strategy(args.flag_cranelift, args.flag_lightbeam);
let config = Config::new( let mut config = Config::new();
settings::Flags::new(flag_builder), config
features, .features(features)
debug_info, .flags(settings::Flags::new(flag_builder))
strategy, .debug_info(debug_info)
); .strategy(strategy);
let engine = HostRef::new(Engine::new(config)); let engine = HostRef::new(Engine::new(&config));
let store = HostRef::new(Store::new(&engine)); let store = HostRef::new(Store::new(&engine));
let mut module_registry = HashMap::new(); let mut module_registry = HashMap::new();