Remove usage of CompilationStrategy from Config (#764)

* Remove usage of `CompilationStrategy` from `Config`

This commit removes the public API usage of the internal
`CompilationStrategy` enumeration from the `Config` type in the
`wasmtime` crate. To do this the `enum` was copied locally into the
crate and renamed `Strategy`. The high-level description of this change
is:

* The `Config::strategy` method now takes a locally-defined `Strategy`
  enumeration instead of an internal type.

* The contents of `Strategy` are always the same, not relying on Cargo
  features to indicate which variants are present. This avoids
  unnecessary downstream `#[cfg]`.

* A `lightbeam` feature was added to the `wasmtime` crate itself to
  lightbeam compilation support.

* The `Config::strategy` method is now fallible. It returns a runtime
  error if support for the selected strategy wasn't compiled in.

* The `Strategy` enum is listed as `#[non_exhaustive]` so we can safely
  add variants over time to it.

This reduces the public crate dependencies of the `wasmtime` crate
itself, removing the need to reach into internal crates even more!

cc #708

* Fix fuzz targets

* Update nightly used to build releases

* Run rustfmt
This commit is contained in:
Alex Crichton
2020-01-06 18:08:13 -06:00
committed by GitHub
parent 787f50e107
commit 7474633cca
24 changed files with 173 additions and 153 deletions

View File

@@ -1,4 +1,5 @@
use crate::context::Context;
use anyhow::Result;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
@@ -158,15 +159,29 @@ impl Config {
self
}
/// Configures the compilation `strategy` provided, indicating which
/// backend will be used for compiling WebAssembly to native code.
/// Configures which compilation strategy will be used for wasm modules.
///
/// 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
/// This method can be used to configure which compiler is used for wasm
/// modules, and for more documentation consult the [`Strategy`] enumeration
/// and its documentation.
///
/// # Errors
///
/// Some compilation strategies require compile-time options of `wasmtime`
/// itself to be set, but if they're not set and the strategy is specified
/// here then an error will be returned.
pub fn strategy(&mut self, strategy: Strategy) -> Result<&mut Self> {
self.strategy = match strategy {
Strategy::Auto => CompilationStrategy::Auto,
Strategy::Cranelift => CompilationStrategy::Cranelift,
#[cfg(feature = "lightbeam")]
Strategy::Lightbeam => CompilationStrategy::Lightbeam,
#[cfg(not(feature = "lightbeam"))]
Strategy::Lightbeam => {
anyhow::bail!("lightbeam compilation strategy wasn't enabled at compile time");
}
};
Ok(self)
}
}
@@ -176,6 +191,30 @@ impl Default for Config {
}
}
/// Possible Compilation strategies for a wasm module.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum Strategy {
/// An indicator that the compilation strategy should be automatically
/// selected.
///
/// This is generally what you want for most projects and indicates that the
/// `wasmtime` crate itself should make the decision about what the best
/// code generator for a wasm module is.
///
/// Currently this always defaults to Cranelift, but the default value will
/// change over time.
Auto,
/// Currently the default backend, Cranelift aims to be a reasonably fast
/// code generator which generates high quality machine code.
Cranelift,
/// A single-pass code generator that is faster than Cranelift but doesn't
/// produce as high-quality code.
Lightbeam,
}
// Engine
/// An `Engine` which is a global context for compilation and management of wasm