Reduce number of crates needed for Config usage
This commit is an attempt to reduce the number of crates necessary to link to when using `wasmtime::Config` in "default mode" or with only one or two tweaks. The change moves to a builder-style pattern for `Config` to only require importing crates as necessary if you configure a particular setting. This then also propagates that change to `Context` as well by taking a `Config` instead of requiring that all arguments are passed alone.
This commit is contained in:
@@ -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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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,
|
/// Configures various flags for compilation such as optimization level and
|
||||||
strategy,
|
/// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 debug_info(&self) -> bool {
|
impl Default for Config {
|
||||||
self.debug_info
|
fn default() -> Config {
|
||||||
}
|
Config::new()
|
||||||
|
|
||||||
pub(crate) fn flags(&self) -> &settings::Flags {
|
|
||||||
&self.flags
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn features(&self) -> &Features {
|
|
||||||
&self.features
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn strategy(&self) -> CompilationStrategy {
|
|
||||||
self.strategy
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
@@ -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" }
|
||||||
|
|||||||
@@ -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);
|
|
||||||
let features = #root::wasmtime_jit::Features {
|
|
||||||
multi_value: true,
|
multi_value: true,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
});
|
||||||
let strategy = #root::wasmtime_jit::CompilationStrategy::Auto;
|
let engine = HostRef::new(Engine::new(&config));
|
||||||
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();
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user