diff --git a/crates/api/src/context.rs b/crates/api/src/context.rs index a9db461de7..3c004db1ce 100644 --- a/crates/api/src/context.rs +++ b/crates/api/src/context.rs @@ -1,8 +1,8 @@ +use crate::Config; use alloc::rc::Rc; use core::cell::{RefCell, RefMut}; use core::hash::{Hash, Hasher}; -use cranelift_codegen::settings; -use wasmtime_jit::{CompilationStrategy, Compiler, Features}; +use wasmtime_jit::{Compiler, Features}; #[derive(Clone)] pub struct Context { @@ -12,21 +12,19 @@ pub struct Context { } impl Context { - pub fn new(compiler: Compiler, features: Features, debug_info: bool) -> Context { - Context { - compiler: Rc::new(RefCell::new(compiler)), - features, - debug_info, - } + pub fn new(config: &Config) -> Context { + let isa_builder = + cranelift_native::builder().expect("host machine is not a supported target"); + let isa = isa_builder.finish(config.flags.clone()); + Context::new_with_compiler(config, Compiler::new(isa, config.strategy)) } - pub fn create( - flags: settings::Flags, - features: Features, - debug_info: bool, - strategy: CompilationStrategy, - ) -> Context { - Context::new(create_compiler(flags, strategy), features, debug_info) + pub fn new_with_compiler(config: &Config, compiler: Compiler) -> Context { + Context { + compiler: Rc::new(RefCell::new(compiler)), + features: config.features.clone(), + debug_info: config.debug_info, + } } pub(crate) fn debug_info(&self) -> bool { @@ -54,13 +52,3 @@ impl PartialEq for Context { 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) -} diff --git a/crates/api/src/runtime.rs b/crates/api/src/runtime.rs index e79050516b..2b7e462ff2 100644 --- a/crates/api/src/runtime.rs +++ b/crates/api/src/runtime.rs @@ -15,15 +15,23 @@ fn default_flags() -> settings::Flags { 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 { - flags: settings::Flags, - features: Features, - debug_info: bool, - strategy: CompilationStrategy, + pub(crate) flags: settings::Flags, + pub(crate) features: Features, + pub(crate) debug_info: bool, + pub(crate) strategy: CompilationStrategy, } impl Config { - pub fn default() -> Config { + /// Creates a new configuration object with the default configuration + /// specified. + pub fn new() -> Config { Config { debug_info: false, features: Default::default(), @@ -32,54 +40,68 @@ impl Config { } } - pub fn new( - flags: settings::Flags, - features: Features, - debug_info: bool, - strategy: CompilationStrategy, - ) -> Config { - Config { - flags, - features, - debug_info, - strategy, - } + /// Configures whether DWARF debug information will be emitted during + /// compilation. + /// + /// By default this option is `false`. + pub fn debug_info(&mut self, enable: bool) -> &mut Self { + self.debug_info = enable; + self } - pub(crate) fn debug_info(&self) -> bool { - self.debug_info + /// Configures various flags for compilation such as optimization level and + /// 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 { - &self.flags + /// Indicates which WebAssembly features are enabled for this compilation + /// 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 { - &self.features + /// Configures the compilation `strategy` provided, indicating which + /// 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 { - self.strategy +impl Default for Config { + fn default() -> Config { + Config::new() } } // Engine +#[derive(Default)] pub struct Engine { config: Config, } impl Engine { - pub fn new(config: Config) -> Engine { - Engine { config } - } - - pub fn default() -> Engine { - Engine::new(Config::default()) - } - - pub(crate) fn config(&self) -> &Config { - &self.config + pub fn new(config: &Config) -> Engine { + Engine { + config: config.clone(), + } } } @@ -94,13 +116,9 @@ pub struct Store { impl Store { pub fn new(engine: &HostRef) -> 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 { 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())), signature_cache: HashMap::new(), } diff --git a/crates/api/tests/import_calling_export.rs b/crates/api/tests/import_calling_export.rs index 8e49387d85..55741eae59 100644 --- a/crates/api/tests/import_calling_export.rs +++ b/crates/api/tests/import_calling_export.rs @@ -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 module = HostRef::new( Module::new( diff --git a/crates/misc/rust/Cargo.toml b/crates/misc/rust/Cargo.toml index 7de0e1707e..9dde6ff4a7 100644 --- a/crates/misc/rust/Cargo.toml +++ b/crates/misc/rust/Cargo.toml @@ -15,8 +15,6 @@ test = false doctest = false [dependencies] -cranelift-codegen = "0.50.0" -cranelift-native = "0.50.0" wasmtime-interface-types = { path = "../../interface-types" } wasmtime-jit = { path = "../../jit" } wasmtime-rust-macro = { path = "./macro" } diff --git a/crates/misc/rust/macro/src/lib.rs b/crates/misc/rust/macro/src/lib.rs index ad1584ff35..2ff5071d5e 100644 --- a/crates/misc/rust/macro/src/lib.rs +++ b/crates/misc/rust/macro/src/lib.rs @@ -51,17 +51,12 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result { use #root::wasmtime_api::{HostRef, Config, Extern, Engine, Store, Instance, Module}; use #root::anyhow::{bail, format_err}; - let config = { - let flag_builder = #root::cranelift_codegen::settings::builder(); - let flags = #root::cranelift_codegen::settings::Flags::new(flag_builder); - let features = #root::wasmtime_jit::Features { - multi_value: true, - ..Default::default() - }; - let strategy = #root::wasmtime_jit::CompilationStrategy::Auto; - Config::new(flags, features, false, strategy) - }; - let engine = HostRef::new(Engine::new(config)); + let mut config = Config::new(); + config.features(#root::wasmtime_jit::Features { + multi_value: true, + ..Default::default() + }); + let engine = HostRef::new(Engine::new(&config)); let store = HostRef::new(Store::new(&engine)); let global_exports = store.borrow().global_exports().clone(); diff --git a/crates/misc/rust/src/lib.rs b/crates/misc/rust/src/lib.rs index 9e03dd8954..4e3ce43098 100644 --- a/crates/misc/rust/src/lib.rs +++ b/crates/misc/rust/src/lib.rs @@ -4,8 +4,6 @@ pub use wasmtime_rust_macro::wasmtime; #[doc(hidden)] pub mod __rt { pub use anyhow; - pub use cranelift_codegen; - pub use cranelift_native; pub use wasmtime_api; pub use wasmtime_interface_types; pub use wasmtime_jit; diff --git a/crates/wasi-common/tests/wasm_tests/runtime.rs b/crates/wasi-common/tests/wasm_tests/runtime.rs index 215fbb8a3e..3d06b6cfbb 100644 --- a/crates/wasi-common/tests/wasm_tests/runtime.rs +++ b/crates/wasi-common/tests/wasm_tests/runtime.rs @@ -3,7 +3,6 @@ use cranelift_codegen::settings::{self, Configurable}; use std::fs::File; use std::{collections::HashMap, path::Path}; 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<()> { // Prepare runtime @@ -14,13 +13,9 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any .enable("avoid_div_traps") .context("error while enabling proper division trap")?; - let config = Config::new( - settings::Flags::new(flag_builder), - Features::default(), - false, - CompilationStrategy::Auto, - ); - let engine = HostRef::new(Engine::new(config)); + let mut config = Config::new(); + config.flags(settings::Flags::new(flag_builder)); + let engine = HostRef::new(Engine::new(&config)); let store = HostRef::new(Store::new(&engine)); let mut module_registry = HashMap::new(); diff --git a/src/bin/wasmtime.rs b/src/bin/wasmtime.rs index 77b7eb204d..204bc05344 100644 --- a/src/bin/wasmtime.rs +++ b/src/bin/wasmtime.rs @@ -267,13 +267,13 @@ fn main() -> Result<()> { // Decide how to compile. let strategy = pick_compilation_strategy(args.flag_cranelift, args.flag_lightbeam); - let config = Config::new( - settings::Flags::new(flag_builder), - features, - debug_info, - strategy, - ); - let engine = HostRef::new(Engine::new(config)); + let mut config = Config::new(); + config + .features(features) + .flags(settings::Flags::new(flag_builder)) + .debug_info(debug_info) + .strategy(strategy); + let engine = HostRef::new(Engine::new(&config)); let store = HostRef::new(Store::new(&engine)); let mut module_registry = HashMap::new();