diff --git a/CHANGELOG.md b/CHANGELOG.md index b05cfe471c3..e2421d0d731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C ## **Unreleased** ### Changed +- [#3047](https://github.com/wasmerio/wasmer/pull/3047) `Store::new` now takes an `impl Into`. - [#3017](https://github.com/wasmerio/wasmer/pull/3017) Fix typo in README.md - [#3008](https://github.com/wasmerio/wasmer/pull/3008) Add a new CI check that uses cargo public-api to track changes in the API between master and the last deployed version on crates.io - [#3003](https://github.com/wasmerio/wasmer/pull/3003) Remove RuntimeError::raise from public API diff --git a/Cargo.lock b/Cargo.lock index 0e8b79d030d..eb9f28714e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2902,7 +2902,6 @@ dependencies = [ "tempfile", "thiserror", "wasmer", - "wasmer-compiler", "wasmer-compiler-singlepass", ] diff --git a/benches/static_and_dynamic_functions.rs b/benches/static_and_dynamic_functions.rs index 1eb80a13894..61945b5357e 100644 --- a/benches/static_and_dynamic_functions.rs +++ b/benches/static_and_dynamic_functions.rs @@ -149,35 +149,19 @@ pub fn run_basic_dynamic_function(store: &Store, compiler_name: &str, c: &mut Cr fn run_static_benchmarks(_c: &mut Criterion) { #[cfg(feature = "llvm")] { - let mut store = Store::new_with_engine( - &EngineBuilder::new(Some(wasmer_compiler_llvm::LLVM::new()), None, None).engine(), - ); + let mut store = Store::new(wasmer_compiler_llvm::LLVM::new()); run_basic_static_function(&store, "llvm", c); } #[cfg(feature = "cranelift")] { - let mut store = Store::new_with_engine( - &EngineBuilder::new( - Some(wasmer_compiler_cranelift::Cranelift::new()), - None, - None, - ) - .engine(), - ); + let mut store = Store::new(wasmer_compiler_cranelift::Cranelift::new()); run_basic_static_function(&store, "cranelift", c); } #[cfg(feature = "singlepass")] { - let mut store = Store::new_with_engine( - &EngineBuilder::new( - Some(wasmer_compiler_singlepass::Singlepass::new()), - None, - None, - ) - .engine(), - ); + let mut store = Store::new(wasmer_compiler_singlepass::Singlepass::new()); run_basic_static_function(&store, "singlepass", c); } } @@ -185,35 +169,19 @@ fn run_static_benchmarks(_c: &mut Criterion) { fn run_dynamic_benchmarks(_c: &mut Criterion) { #[cfg(feature = "llvm")] { - let mut store = Store::new_with_engine( - &EngineBuilder::new(Some(wasmer_compiler_llvm::LLVM::new()), None, None).engine(), - ); + let mut store = Store::new(wasmer_compiler_llvm::LLVM::new()); run_basic_dynamic_function(&store, "llvm", c); } #[cfg(feature = "cranelift")] { - let mut store = Store::new_with_engine( - &EngineBuilder::new( - Some(wasmer_compiler_cranelift::Cranelift::new()), - None, - None, - ) - .engine(), - ); + let mut store = Store::new(wasmer_compiler_cranelift::Cranelift::new()); run_basic_dynamic_function(&store, "cranelift", c); } #[cfg(feature = "singlepass")] { - let mut store = Store::new_with_engine( - &EngineBuilder::new( - Some(wasmer_compiler_singlepass::Singlepass::new()), - None, - None, - ) - .engine(), - ); + let mut store = Store::new(wasmer_compiler_singlepass::Singlepass::new()); run_basic_dynamic_function(&store, "singlepass", c); } } diff --git a/docs/migration_to_3.0.0.md b/docs/migration_to_3.0.0.md index 72523d5bc9c..ce939e93334 100644 --- a/docs/migration_to_3.0.0.md +++ b/docs/migration_to_3.0.0.md @@ -145,12 +145,27 @@ let wasm_bytes = wat2wasm( "..".as_bytes(), )?; -let compiler_config = Cranelift::default(); -let mut store = Store::new(&compiler_config); +let compiler = Cranelift::default(); +let mut store = Store::new(compiler); let module = Module::new(&store, wasm_bytes)?; let instance = Instance::new(&mut store, &module, &imports! {})?; ``` +#### Advanced configuration + +The previous ability to define target and features remains in a new `EngineBuilder` interface: + +```rust +let compiler = Cranelift::default(); + +let mut features = Features::new(); +// Enable the multi-value feature. +features.multi_value(true); + +let engine = EngineBuilder::new(compiler).set_features(Some(features)); +let store = Store::new(engine); +``` + [examples]: https://docs.wasmer.io/integrations/examples [wasmer]: https://crates.io/crates/wasmer [wasmer-wasi]: https://crates.io/crates/wasmer-wasi diff --git a/examples/compiler_cranelift.rs b/examples/compiler_cranelift.rs index 394c9adf920..cb8b8f91926 100644 --- a/examples/compiler_cranelift.rs +++ b/examples/compiler_cranelift.rs @@ -11,7 +11,6 @@ //! Ready? use wasmer::{imports, wat2wasm, Instance, Module, Store, Value}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -33,7 +32,7 @@ fn main() -> Result<(), Box> { let compiler = Cranelift::default(); // Create the store - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/compiler_llvm.rs b/examples/compiler_llvm.rs index 0fc43967f49..26222272596 100644 --- a/examples/compiler_llvm.rs +++ b/examples/compiler_llvm.rs @@ -11,7 +11,6 @@ //! Ready? use wasmer::{imports, wat2wasm, Instance, Module, Store, Value}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_llvm::LLVM; fn main() -> Result<(), Box> { @@ -33,7 +32,7 @@ fn main() -> Result<(), Box> { let compiler = LLVM::default(); // Create the store - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/compiler_singlepass.rs b/examples/compiler_singlepass.rs index 79cda03d15a..01fb181b128 100644 --- a/examples/compiler_singlepass.rs +++ b/examples/compiler_singlepass.rs @@ -11,7 +11,6 @@ //! Ready? use wasmer::{imports, wat2wasm, Instance, Module, Store, Value}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_singlepass::Singlepass; fn main() -> Result<(), Box> { @@ -33,7 +32,7 @@ fn main() -> Result<(), Box> { let compiler = Singlepass::default(); // Create the store - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/early_exit.rs b/examples/early_exit.rs index fda18128eb0..f582447896f 100644 --- a/examples/early_exit.rs +++ b/examples/early_exit.rs @@ -20,7 +20,6 @@ use wasmer::{ imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, TypedFunction, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; // First we need to create an error type that we'll use to signal the end of execution. @@ -58,8 +57,7 @@ fn main() -> anyhow::Result<()> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/engine.rs b/examples/engine.rs index 52fb6a9b1f9..1bb2001610f 100644 --- a/examples/engine.rs +++ b/examples/engine.rs @@ -18,8 +18,7 @@ //! //! Ready? -use wasmer::{imports, wat2wasm, Instance, Module, Store, Value}; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, wat2wasm, EngineBuilder, Instance, Module, Store, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -49,10 +48,10 @@ fn main() -> Result<(), Box> { // // In this case, the engine is `wasmer_compiler` which roughly // means that the executable code will live in memory. - let engine = EngineBuilder::new(compiler_config, None, None).engine(); + let engine = EngineBuilder::new(compiler_config); // Create a store, that holds the engine. - let mut store = Store::new_with_engine(&engine); + let mut store = Store::new(engine); println!("Compiling module..."); // Here we go. diff --git a/examples/engine_cross_compilation.rs b/examples/engine_cross_compilation.rs index 4170b4b4245..de432d5cdb8 100644 --- a/examples/engine_cross_compilation.rs +++ b/examples/engine_cross_compilation.rs @@ -19,8 +19,7 @@ //! Ready? use std::str::FromStr; -use wasmer::{wat2wasm, Module, RuntimeError, Store}; -use wasmer_compiler::EngineBuilder; +use wasmer::{wat2wasm, EngineBuilder, Module, RuntimeError, Store}; use wasmer_compiler_cranelift::Cranelift; use wasmer_types::{CpuFeature, Target, Triple}; @@ -70,10 +69,10 @@ fn main() -> Result<(), Box> { // That's where we specify the target for the compiler. // // Use the Universal engine. - let mut engine = EngineBuilder::new(compiler_config, Some(target), None).engine(); + let mut engine = EngineBuilder::new(compiler_config).set_target(Some(target)); // Create a store, that holds the engine. - let mut store = Store::new_with_engine(&engine); + let mut store = Store::new(engine); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/engine_dylib.rs b/examples/engine_dylib.rs index 2b9c4d7a773..07fa3cd1201 100644 --- a/examples/engine_dylib.rs +++ b/examples/engine_dylib.rs @@ -52,10 +52,10 @@ fn main() -> Result<(), Box> { // // In this case, the engine is `wasmer_engine_dylib` which means // that a shared object is going to be generated. - let engine = Dylib::new(compiler_config).engine(); + let engine = Dylib::new(compiler_config); // Create a store, that holds the engine. - let mut store = Store::new_with_engine(&engine); + let mut store = Store::new(engine); println!("Compiling module..."); // Here we go. diff --git a/examples/engine_headless.rs b/examples/engine_headless.rs index 943865cefcf..374ccb5600a 100644 --- a/examples/engine_headless.rs +++ b/examples/engine_headless.rs @@ -45,14 +45,7 @@ //! Ready? use tempfile::NamedTempFile; -use wasmer::imports; -use wasmer::wat2wasm; -use wasmer::FunctionEnv; -use wasmer::Instance; -use wasmer::Module; -use wasmer::Store; -use wasmer::Value; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, wat2wasm, EngineBuilder, FunctionEnv, Instance, Module, Store, Value}; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -78,14 +71,10 @@ fn main() -> Result<(), Box> { // In this situation, the compiler is // `wasmer_compiler_cranelift`. The compiler is responsible to // compile the Wasm module into executable code. - let compiler_config = Cranelift::default(); - - println!("Creating univesral engine..."); - // Define the engine that will drive everything. - let engine = EngineBuilder::new(compiler_config, None, None).engine(); + let compiler = Cranelift::default(); // Create a store, that holds the engine. - let mut store = Store::new_with_engine(&engine); + let mut store = Store::new(compiler); println!("Compiling module..."); // Let's compile the Wasm module. @@ -105,8 +94,8 @@ fn main() -> Result<(), Box> { { println!("Creating headless Universal engine..."); // We create a headless Universal engine. - let engine = EngineBuilder::headless().engine(); - let mut store = Store::new_with_engine(&engine); + let engine = EngineBuilder::headless(); + let mut store = Store::new(engine); let mut env = FunctionEnv::new(&mut store, ()); println!("Deserializing module..."); diff --git a/examples/errors.rs b/examples/errors.rs index 2de13765e50..ecbb0c02ac8 100644 --- a/examples/errors.rs +++ b/examples/errors.rs @@ -14,7 +14,6 @@ //! Ready? use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -39,8 +38,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/exports_function.rs b/examples/exports_function.rs index e8d236345a3..12eaf36b1b1 100644 --- a/examples/exports_function.rs +++ b/examples/exports_function.rs @@ -18,7 +18,6 @@ //! Ready? use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction, Value}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -40,8 +39,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/exports_global.rs b/examples/exports_global.rs index bec126847eb..3a71368d871 100644 --- a/examples/exports_global.rs +++ b/examples/exports_global.rs @@ -18,7 +18,6 @@ use wasmer::{ imports, wat2wasm, FunctionEnv, Instance, Module, Mutability, Store, Type, TypedFunction, Value, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -40,8 +39,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/exports_memory.rs b/examples/exports_memory.rs index 255e8916b85..6bf9f7cae52 100644 --- a/examples/exports_memory.rs +++ b/examples/exports_memory.rs @@ -12,7 +12,6 @@ //! Ready? use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction, WasmPtr}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -37,8 +36,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/features.rs b/examples/features.rs index 3438059aa34..aa3b9090118 100644 --- a/examples/features.rs +++ b/examples/features.rs @@ -10,8 +10,9 @@ //! //! Ready? -use wasmer::{imports, wat2wasm, Features, FunctionEnv, Instance, Module, Store, Value}; -use wasmer_compiler::EngineBuilder; +use wasmer::{ + imports, wat2wasm, EngineBuilder, Features, FunctionEnv, Instance, Module, Store, Value, +}; use wasmer_compiler_cranelift::Cranelift; fn main() -> anyhow::Result<()> { @@ -36,10 +37,10 @@ fn main() -> anyhow::Result<()> { features.multi_value(true); // Set up the engine. That's where we define the features! - let engine = EngineBuilder::new(compiler, None, Some(features)); + let engine = EngineBuilder::new(compiler).set_features(Some(features)); // Now, let's define the store, and compile the module. - let mut store = Store::new_with_engine(&engine.engine()); + let mut store = Store::new(engine); let mut env = FunctionEnv::new(&mut store, ()); let module = Module::new(&store, wasm_bytes)?; diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 6edaa391e45..eaec3d75cfd 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -10,7 +10,6 @@ use wasmer::{ imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, TypedFunction, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> anyhow::Result<()> { @@ -47,8 +46,7 @@ fn main() -> anyhow::Result<()> { // However for the purposes of showing what's happening, we create a compiler // (`Cranelift`) and pass it to an engine (`Universal`). We then pass the engine to // the store and are now ready to compile and run WebAssembly! - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); // We then use our store and Wasm bytes to compile a `Module`. // A `Module` is a compiled WebAssembly module that isn't ready to execute yet. diff --git a/examples/imports_exports.rs b/examples/imports_exports.rs index ea0ada3d8d4..01b4c7ef89b 100644 --- a/examples/imports_exports.rs +++ b/examples/imports_exports.rs @@ -19,7 +19,6 @@ use wasmer::{ imports, wat2wasm, Function, FunctionEnv, FunctionType, Global, Instance, Memory, Module, Store, Table, Type, Value, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -44,8 +43,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/imports_function.rs b/examples/imports_function.rs index 2a15b18abad..c111cfeb8c5 100644 --- a/examples/imports_function.rs +++ b/examples/imports_function.rs @@ -21,7 +21,6 @@ use wasmer::{ imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, FunctionType, Instance, Module, Store, Type, TypedFunction, Value, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -45,8 +44,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env1 = FunctionEnv::new(&mut store, ()); struct MyEnv; let mut env2 = FunctionEnv::new(&mut store, MyEnv {}); diff --git a/examples/imports_function_env.rs b/examples/imports_function_env.rs index 2496b69dd23..df7ee8b26a0 100644 --- a/examples/imports_function_env.rs +++ b/examples/imports_function_env.rs @@ -24,7 +24,6 @@ use wasmer::{ imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, TypedFunction, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -52,8 +51,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/imports_global.rs b/examples/imports_global.rs index 829cd183668..9ae571bf482 100644 --- a/examples/imports_global.rs +++ b/examples/imports_global.rs @@ -18,7 +18,6 @@ use wasmer::{ imports, wat2wasm, FunctionEnv, Global, Instance, Module, Store, TypedFunction, Value, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -40,8 +39,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/instance.rs b/examples/instance.rs index 16b13fee2ae..cff03d54875 100644 --- a/examples/instance.rs +++ b/examples/instance.rs @@ -15,7 +15,6 @@ //! Ready? use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; fn main() -> Result<(), Box> { @@ -39,8 +38,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/memory.rs b/examples/memory.rs index 3aee97ad637..3204e5a2229 100644 --- a/examples/memory.rs +++ b/examples/memory.rs @@ -18,7 +18,6 @@ use std::mem; use wasmer::{ imports, wat2wasm, Bytes, FunctionEnv, Instance, Module, Pages, Store, TypedFunction, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; // this example is a work in progress: @@ -59,8 +58,7 @@ fn main() -> anyhow::Result<()> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/metering.rs b/examples/metering.rs index 00e04e112c0..d9eb765f086 100644 --- a/examples/metering.rs +++ b/examples/metering.rs @@ -18,8 +18,9 @@ use anyhow::bail; use std::sync::Arc; use wasmer::wasmparser::Operator; use wasmer::CompilerConfig; -use wasmer::{imports, wat2wasm, FunctionEnv, Instance, Module, Store, TypedFunction}; -use wasmer_compiler::EngineBuilder; +use wasmer::{ + imports, wat2wasm, EngineBuilder, FunctionEnv, Instance, Module, Store, TypedFunction, +}; use wasmer_compiler_cranelift::Cranelift; use wasmer_middlewares::{ metering::{get_remaining_points, set_remaining_points, MeteringPoints}, @@ -70,8 +71,7 @@ fn main() -> anyhow::Result<()> { // // We use our previously create compiler configuration // with the Universal engine. - let mut store = - Store::new_with_engine(&EngineBuilder::new(compiler_config, None, None).engine()); + let mut store = Store::new(EngineBuilder::new(compiler_config)); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/platform_ios_headless.rs b/examples/platform_ios_headless.rs index de15e9adf2f..94639ae46e9 100644 --- a/examples/platform_ios_headless.rs +++ b/examples/platform_ios_headless.rs @@ -50,10 +50,10 @@ fn main() -> Result<(), Box> { println!("Chosen target: {:?}", target); println!("Creating Dylib engine..."); - let engine = Dylib::new(compiler_config).target(target).engine(); + let engine = Dylib::new(compiler_config).target(target); // Create a store, that holds the engine. - let mut store = Store::new_with_engine(&engine); + let mut store = Store::new(engine); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/table.rs b/examples/table.rs index 28a4d508da8..922b8f30032 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -2,7 +2,6 @@ use wasmer::{ imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store, TableType, Type, TypedFunction, Value, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; /// A function we'll call through a table. @@ -52,8 +51,7 @@ fn main() -> anyhow::Result<()> { )?; // We set up our store with an engine and a compiler. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); let mut env = FunctionEnv::new(&mut store, ()); // Then compile our Wasm. let module = Module::new(&store, wasm_bytes)?; diff --git a/examples/tunables_limit_memory.rs b/examples/tunables_limit_memory.rs index 90ed9028140..b6267431beb 100644 --- a/examples/tunables_limit_memory.rs +++ b/examples/tunables_limit_memory.rs @@ -6,7 +6,6 @@ use wasmer::{ wat2wasm, BaseTunables, FunctionEnv, Instance, Memory, MemoryType, Module, Pages, Store, TableType, Target, Tunables, }; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; /// A custom tunables that allows you to set a memory limit. @@ -135,17 +134,15 @@ fn main() -> Result<(), Box> { let wasm_bytes = wat2wasm(wat)?; - // Any compiler and any engine do the job here + // Any compiler do the job here let compiler = Cranelift::default(); - let engine = EngineBuilder::new(compiler, None, None).engine(); // Here is where the fun begins - let base = BaseTunables::for_target(&Target::default()); let tunables = LimitingTunables::new(base, Pages(24)); // Create a store, that holds the engine and our custom tunables - let mut store = Store::new_with_tunables(&engine, tunables); + let mut store = Store::new_with_tunables(compiler, tunables); let mut env = FunctionEnv::new(&mut store, ()); println!("Compiling module..."); diff --git a/examples/wasi.rs b/examples/wasi.rs index 011346db3b9..2a8a047c447 100644 --- a/examples/wasi.rs +++ b/examples/wasi.rs @@ -16,7 +16,6 @@ //! Ready? use wasmer::{FunctionEnv, Instance, Module, Store}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; use wasmer_wasi::WasiState; @@ -32,8 +31,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/examples/wasi_pipes.rs b/examples/wasi_pipes.rs index 8ce9897cfa7..84f1a9b6229 100644 --- a/examples/wasi_pipes.rs +++ b/examples/wasi_pipes.rs @@ -13,7 +13,6 @@ use std::io::{Read, Write}; use wasmer::{FunctionEnv, Instance, Module, Store}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_cranelift::Cranelift; use wasmer_wasi::{Pipe, WasiState}; @@ -29,8 +28,7 @@ fn main() -> Result<(), Box> { // Note that we don't need to specify the engine/compiler if we want to use // the default provided by Wasmer. // You can use `Store::default()` for that. - let mut store = - Store::new_with_engine(&EngineBuilder::new(Cranelift::default(), None, None).engine()); + let mut store = Store::new(Cranelift::default()); println!("Compiling module..."); // Let's compile the Wasm module. diff --git a/fuzz/fuzz_targets/deterministic.rs b/fuzz/fuzz_targets/deterministic.rs index 6ac7d38caa4..70892a3a333 100644 --- a/fuzz/fuzz_targets/deterministic.rs +++ b/fuzz/fuzz_targets/deterministic.rs @@ -2,8 +2,7 @@ use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; use wasm_smith::{Config, ConfiguredModule}; -use wasmer::{CompilerConfig, Engine, Module, Store}; -use wasmer_compiler::EngineBuilder; +use wasmer::{CompilerConfig, Engine, EngineBuilder, Module, Store}; use wasmer_compiler_cranelift::Cranelift; use wasmer_compiler_llvm::LLVM; use wasmer_compiler_singlepass::Singlepass; @@ -24,7 +23,7 @@ impl Config for NoImportsConfig { } fn compile_and_compare(name: &str, engine: Engine, wasm: &[u8]) { - let store = Store::new_with_engine(&engine); + let store = Store::new(engine); // compile for first time let module = Module::new(&store, wasm).unwrap(); @@ -47,7 +46,7 @@ fuzz_target!(|module: ConfiguredModule| { compiler.enable_verifier(); compile_and_compare( "universal-cranelift", - EngineBuilder::new(compiler.clone(), None, None).engine(), + EngineBuilder::new(compiler.clone()), &wasm_bytes, ); @@ -56,14 +55,14 @@ fuzz_target!(|module: ConfiguredModule| { compiler.enable_verifier(); compile_and_compare( "universal-llvm", - EngineBuilder::new(compiler.clone(), None, None).engine(), + EngineBuilder::new(compiler.clone()), &wasm_bytes, ); let compiler = Singlepass::default(); compile_and_compare( "universal-singlepass", - EngineBuilder::new(compiler.clone(), None, None).engine(), + EngineBuilder::new(compiler.clone()), &wasm_bytes, ); }); diff --git a/fuzz/fuzz_targets/equivalence_universal.rs b/fuzz/fuzz_targets/equivalence_universal.rs index 3d105462b5b..4db992f3140 100644 --- a/fuzz/fuzz_targets/equivalence_universal.rs +++ b/fuzz/fuzz_targets/equivalence_universal.rs @@ -4,8 +4,7 @@ use anyhow::Result; use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; use wasm_smith::{Config, ConfiguredModule}; -use wasmer::{imports, CompilerConfig, Instance, Module, Store, Val}; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, CompilerConfig, EngineBuilder, Instance, Module, Store, Val}; #[cfg(feature = "cranelift")] use wasmer_compiler_cranelift::Cranelift; #[cfg(feature = "llvm")] @@ -48,7 +47,7 @@ impl std::fmt::Debug for WasmSmithModule { #[cfg(feature = "singlepass")] fn maybe_instantiate_singlepass(wasm_bytes: &[u8]) -> Result> { let compiler = Singlepass::default(); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes); let module = match module { Ok(m) => m, @@ -69,7 +68,7 @@ fn maybe_instantiate_cranelift(wasm_bytes: &[u8]) -> Result> { let mut compiler = Cranelift::default(); compiler.canonicalize_nans(true); compiler.enable_verifier(); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes)?; let instance = Instance::new(&module, &imports! {})?; Ok(Some(instance)) @@ -80,7 +79,7 @@ fn maybe_instantiate_llvm(wasm_bytes: &[u8]) -> Result> { let mut compiler = LLVM::default(); compiler.canonicalize_nans(true); compiler.enable_verifier(); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes)?; let instance = Instance::new(&module, &imports! {})?; Ok(Some(instance)) diff --git a/fuzz/fuzz_targets/metering.rs b/fuzz/fuzz_targets/metering.rs index 3d68f0c1f4e..8c1053c4aad 100644 --- a/fuzz/fuzz_targets/metering.rs +++ b/fuzz/fuzz_targets/metering.rs @@ -4,8 +4,7 @@ use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; use std::sync::Arc; use wasm_smith::{Config, ConfiguredModule}; use wasmer::wasmparser::Operator; -use wasmer::{imports, CompilerConfig, Instance, Module, Store}; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, CompilerConfig, EngineBuilder, Instance, Module, Store}; use wasmer_compiler_cranelift::Cranelift; use wasmer_middlewares::Metering; @@ -56,7 +55,7 @@ fuzz_target!(|module: WasmSmithModule| { compiler.enable_verifier(); let metering = Arc::new(Metering::new(10, cost)); compiler.push_middleware(metering); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes).unwrap(); match Instance::new(&module, &imports! {}) { Ok(_) => {} diff --git a/fuzz/fuzz_targets/universal_cranelift.rs b/fuzz/fuzz_targets/universal_cranelift.rs index 1fef9ea2015..c1303cf8778 100644 --- a/fuzz/fuzz_targets/universal_cranelift.rs +++ b/fuzz/fuzz_targets/universal_cranelift.rs @@ -2,8 +2,7 @@ use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; use wasm_smith::{Config, ConfiguredModule}; -use wasmer::{imports, CompilerConfig, Instance, Module, Store}; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, CompilerConfig, EngineBuilder, Instance, Module, Store}; use wasmer_compiler_cranelift::Cranelift; #[derive(Arbitrary, Debug, Default, Copy, Clone)] @@ -42,7 +41,7 @@ fuzz_target!(|module: WasmSmithModule| { let mut compiler = Cranelift::default(); compiler.canonicalize_nans(true); compiler.enable_verifier(); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes).unwrap(); match Instance::new(&module, &imports! {}) { Ok(_) => {} diff --git a/fuzz/fuzz_targets/universal_llvm.rs b/fuzz/fuzz_targets/universal_llvm.rs index 4b9ac9692da..42331a599cb 100644 --- a/fuzz/fuzz_targets/universal_llvm.rs +++ b/fuzz/fuzz_targets/universal_llvm.rs @@ -2,8 +2,7 @@ use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; use wasm_smith::{Config, ConfiguredModule}; -use wasmer::{imports, CompilerConfig, Instance, Module, Store}; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, CompilerConfig, EngineBuilder, Instance, Module, Store}; use wasmer_compiler_llvm::LLVM; #[derive(Arbitrary, Debug, Default, Copy, Clone)] @@ -42,7 +41,7 @@ fuzz_target!(|module: WasmSmithModule| { let mut compiler = LLVM::default(); compiler.canonicalize_nans(true); compiler.enable_verifier(); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes).unwrap(); match Instance::new(&module, &imports! {}) { Ok(_) => {} diff --git a/fuzz/fuzz_targets/universal_singlepass.rs b/fuzz/fuzz_targets/universal_singlepass.rs index d6a059eb5c2..b0082aeb6a8 100644 --- a/fuzz/fuzz_targets/universal_singlepass.rs +++ b/fuzz/fuzz_targets/universal_singlepass.rs @@ -2,8 +2,7 @@ use libfuzzer_sys::{arbitrary, arbitrary::Arbitrary, fuzz_target}; use wasm_smith::{Config, ConfiguredModule}; -use wasmer::{imports, Instance, Module, Store}; -use wasmer_compiler::EngineBuilder; +use wasmer::{imports, EngineBuilder, Instance, Module, Store}; use wasmer_compiler_singlepass::Singlepass; #[derive(Arbitrary, Debug, Default, Copy, Clone)] @@ -40,7 +39,7 @@ fuzz_target!(|module: WasmSmithModule| { } let compiler = Singlepass::default(); - let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let mut store = Store::new(compiler); let module = Module::new(&store, &wasm_bytes); let module = match module { Ok(m) => m, diff --git a/lib/api/src/sys/store.rs b/lib/api/src/sys/store.rs index 21350cd3906..c75f1f018b8 100644 --- a/lib/api/src/sys/store.rs +++ b/lib/api/src/sys/store.rs @@ -2,8 +2,6 @@ use crate::sys::tunables::BaseTunables; use std::fmt; use std::sync::{Arc, RwLock}; #[cfg(feature = "compiler")] -use wasmer_compiler::CompilerConfig; -#[cfg(feature = "compiler")] use wasmer_compiler::{Engine, EngineBuilder, Tunables}; use wasmer_vm::{init_traps, TrapHandler, TrapHandlerFn}; @@ -37,15 +35,20 @@ pub struct Store { impl Store { #[cfg(feature = "compiler")] - /// Creates a new `Store` with a specific [`CompilerConfig`]. - pub fn new(compiler_config: Box) -> Self { - let engine = EngineBuilder::new(compiler_config, None, None).engine(); - Self::new_with_tunables(&engine, BaseTunables::for_target(engine.target())) + /// Creates a new `Store` with a specific [`Engine`]. + pub fn new(engine: impl Into) -> Self { + let engine = engine.into(); + let target = engine.target().clone(); + Self::new_with_tunables(engine, BaseTunables::for_target(&target)) } + #[deprecated( + since = "3.0.0", + note = "Store::new_with_engine has been deprecated in favor of Store::new" + )] /// Creates a new `Store` with a specific [`Engine`]. - pub fn new_with_engine(engine: &Engine) -> Self { - Self::new_with_tunables(engine, BaseTunables::for_target(engine.target())) + pub fn new_with_engine(engine: impl Into) -> Self { + Self::new(engine) } /// Set the trap handler in this store. @@ -55,9 +58,11 @@ impl Store { /// Creates a new `Store` with a specific [`Engine`] and [`Tunables`]. pub fn new_with_tunables( - engine: &Engine, + engine: impl Into, tunables: impl Tunables + Send + Sync + 'static, ) -> Self { + let engine = engine.into(); + // Make sure the signal handlers are installed. // This is required for handling traps. init_traps(); @@ -128,7 +133,7 @@ impl Default for Store { // more than one compiler is enabled. #[allow(unreachable_code)] #[cfg(any(feature = "cranelift", feature = "llvm", feature = "singlepass"))] - fn get_config() -> impl CompilerConfig + 'static { + fn get_config() -> impl wasmer_compiler::CompilerConfig + 'static { cfg_if::cfg_if! { if #[cfg(feature = "cranelift")] { wasmer_compiler_cranelift::Cranelift::default() @@ -151,10 +156,10 @@ impl Default for Store { if #[cfg(any(feature = "cranelift", feature = "llvm", feature = "singlepass"))] { let config = get_config(); - wasmer_compiler::EngineBuilder::new(Box::new(config) as Box, None, None) + EngineBuilder::new(Box::new(config) as Box) .engine() } else { - wasmer_compiler::EngineBuilder::headless() + EngineBuilder::headless() .engine() } } diff --git a/lib/c-api/src/wasm_c_api/engine.rs b/lib/c-api/src/wasm_c_api/engine.rs index e16da83c59f..505109f9bbc 100644 --- a/lib/c-api/src/wasm_c_api/engine.rs +++ b/lib/c-api/src/wasm_c_api/engine.rs @@ -279,7 +279,7 @@ cfg_if! { #[no_mangle] pub extern "C" fn wasm_engine_new() -> Box { let compiler_config: Box = get_default_compiler_config(); - let engine: Engine = EngineBuilder::new(compiler_config, None, None).engine(); + let engine: Engine = EngineBuilder::new(compiler_config).engine(); Box::new(wasm_engine_t { inner: engine }) } } else if #[cfg(feature = "compiler-headless")] { @@ -405,14 +405,14 @@ pub extern "C" fn wasm_engine_new_with_config( let inner: Engine = { - let mut builder = EngineBuilder::new(compiler_config, None, None); + let mut builder = EngineBuilder::new(compiler_config); if let Some(target) = config.target { - builder.set_target(Some(target.inner)); + builder = builder.set_target(Some(target.inner)); } if let Some(features) = config.features { - builder.set_features(Some(features.inner)); + builder = builder.set_features(Some(features.inner)); } builder.engine() @@ -424,11 +424,11 @@ pub extern "C" fn wasm_engine_new_with_config( let mut builder = EngineBuilder::headless(); if let Some(target) = config.target { - builder.set_target(Some(target.inner)); + builder = builder.set_target(Some(target.inner)); } if let Some(features) = config.features { - builder.set_features(Some(features.inner)); + builder = builder.set_features(Some(features.inner)); } builder.engine() diff --git a/lib/c-api/src/wasm_c_api/store.rs b/lib/c-api/src/wasm_c_api/store.rs index 7a3b9b67c09..b1ac2c85254 100644 --- a/lib/c-api/src/wasm_c_api/store.rs +++ b/lib/c-api/src/wasm_c_api/store.rs @@ -34,7 +34,7 @@ pub unsafe extern "C" fn wasm_store_new( engine: Option<&wasm_engine_t>, ) -> Option> { let engine = engine?; - let store = Store::new_with_engine(&engine.inner); + let store = Store::new(&engine.inner); Some(Box::new(wasm_store_t { inner: StoreRef { diff --git a/lib/cache/Cargo.toml b/lib/cache/Cargo.toml index 319afb3042d..21fc1798077 100644 --- a/lib/cache/Cargo.toml +++ b/lib/cache/Cargo.toml @@ -21,7 +21,6 @@ criterion = "0.3" tempfile = "3" rand = "0.8.3" wasmer-compiler-singlepass = { path = "../compiler-singlepass", version = "=2.3.0" } -wasmer-compiler = { path = "../compiler", version = "=2.3.0" } [features] default = ["wasmer/js-serializable-module", "filesystem"] diff --git a/lib/cache/benches/bench_filesystem_cache.rs b/lib/cache/benches/bench_filesystem_cache.rs index 4d18ab4145c..8153e7c7b36 100644 --- a/lib/cache/benches/bench_filesystem_cache.rs +++ b/lib/cache/benches/bench_filesystem_cache.rs @@ -6,7 +6,6 @@ use tempfile::TempDir; use wasmer::{Module, Store}; use wasmer_cache::Cache; use wasmer_cache::{FileSystemCache, Hash}; -use wasmer_compiler::EngineBuilder; use wasmer_compiler_singlepass::Singlepass; fn random_key() -> Hash { @@ -17,7 +16,7 @@ pub fn store_cache_universal(c: &mut Criterion) { let tmp_dir = TempDir::new().unwrap(); let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap(); let compiler = Singlepass::default(); - let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let store = Store::new(compiler); let module = Module::new( &store, std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(), @@ -36,7 +35,7 @@ pub fn load_cache_universal(c: &mut Criterion) { let tmp_dir = TempDir::new().unwrap(); let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap(); let compiler = Singlepass::default(); - let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let store = Store::new(compiler); let module = Module::new( &store, std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(), @@ -54,7 +53,7 @@ pub fn store_cache_native(c: &mut Criterion) { let tmp_dir = TempDir::new().unwrap(); let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap(); let compiler = Singlepass::default(); - let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let store = Store::new(compiler); let module = Module::new( &store, std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(), @@ -73,7 +72,7 @@ pub fn load_cache_native(c: &mut Criterion) { let tmp_dir = TempDir::new().unwrap(); let mut fs_cache = FileSystemCache::new(tmp_dir.path()).unwrap(); let compiler = Singlepass::default(); - let store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); + let store = Store::new(compiler); let module = Module::new( &store, std::fs::read("../../lib/c-api/examples/assets/qjs.wasm").unwrap(), diff --git a/lib/cli-compiler/src/store.rs b/lib/cli-compiler/src/store.rs index 2f4ae826fda..ef5f102d04b 100644 --- a/lib/cli-compiler/src/store.rs +++ b/lib/cli-compiler/src/store.rs @@ -174,8 +174,9 @@ impl CompilerOptions { compiler_config: Box, ) -> Result { let features = self.get_features(compiler_config.default_features_for_target(&target))?; - let engine: EngineBuilder = - EngineBuilder::new(compiler_config, Some(target), Some(features)); + let engine: EngineBuilder = EngineBuilder::new(compiler_config) + .set_target(Some(target)) + .set_features(Some(features)); Ok(engine) } diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 75655330325..a41eda33ff4 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -262,8 +262,8 @@ impl Run { fn get_store_module(&self) -> Result<(Store, Module)> { let contents = std::fs::read(self.path.clone())?; if wasmer_compiler::Artifact::is_deserializable(&contents) { - let engine = wasmer_compiler::EngineBuilder::headless().engine(); - let store = Store::new_with_engine(&engine); + let engine = wasmer_compiler::EngineBuilder::headless(); + let store = Store::new(engine); let module = unsafe { Module::deserialize_from_file(&store, &self.path)? }; return Ok((store, module)); } diff --git a/lib/cli/src/store.rs b/lib/cli/src/store.rs index 990c56d2343..523e3584883 100644 --- a/lib/cli/src/store.rs +++ b/lib/cli/src/store.rs @@ -105,7 +105,7 @@ impl CompilerOptions { pub fn get_store_for_target(&self, target: Target) -> Result<(Store, CompilerType)> { let (compiler_config, compiler_type) = self.get_compiler_config()?; let engine = self.get_engine(target, compiler_config)?; - let store = Store::new_with_engine(&engine); + let store = Store::new(engine); Ok((store, compiler_type)) } @@ -114,12 +114,12 @@ impl CompilerOptions { &self, target: Target, compiler_config: Box, - ) -> Result> { + ) -> Result { let features = self.get_features(compiler_config.default_features_for_target(&target))?; - let engine: Box = Box::new( - wasmer_compiler::EngineBuilder::new(compiler_config, Some(target), Some(features)) - .engine(), - ); + let engine: Engine = wasmer_compiler::EngineBuilder::new(compiler_config) + .set_features(Some(features)) + .set_target(Some(target)) + .engine(); Ok(engine) } @@ -313,7 +313,7 @@ impl StoreOptions { pub fn get_store_for_target(&self, target: Target) -> Result<(Store, CompilerType)> { let (compiler_config, compiler_type) = self.compiler.get_compiler_config()?; let engine = self.get_engine_with_compiler(target, compiler_config)?; - let store = Store::new_with_engine(&engine); + let store = Store::new(engine); Ok((store, compiler_type)) } @@ -322,9 +322,8 @@ impl StoreOptions { &self, target: Target, compiler_config: Box, - ) -> Result> { + ) -> Result { let engine = self.compiler.get_engine(target, compiler_config)?; - Ok(engine) } } @@ -340,7 +339,7 @@ impl StoreOptions { /// Get the store (headless engine) pub fn get_store(&self) -> Result<(Store, CompilerType)> { let engine = self.get_engine_headless()?; - let store = Store::new_with_engine(&engine); + let store = Store::new(engine); Ok((store, CompilerType::Headless)) } } diff --git a/lib/compiler-cranelift/README.md b/lib/compiler-cranelift/README.md index e20c2de104f..51e8b92b272 100644 --- a/lib/compiler-cranelift/README.md +++ b/lib/compiler-cranelift/README.md @@ -9,8 +9,7 @@ use wasmer::{Store, EngineBuilder}; use wasmer_compiler_cranelift::Cranelift; let compiler = Cranelift::new(); -// Put it into an engine and add it to the store -let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); +let mut store = Store::new(compiler); ``` *Note: you can find a [full working example using Cranelift compiler diff --git a/lib/compiler-cranelift/src/config.rs b/lib/compiler-cranelift/src/config.rs index 115fe30d752..5c757c9ef76 100644 --- a/lib/compiler-cranelift/src/config.rs +++ b/lib/compiler-cranelift/src/config.rs @@ -3,7 +3,7 @@ use cranelift_codegen::isa::{lookup, TargetIsa}; use cranelift_codegen::settings::{self, Configurable}; use cranelift_codegen::CodegenResult; use std::sync::Arc; -use wasmer_compiler::{Compiler, CompilerConfig, ModuleMiddleware}; +use wasmer_compiler::{Compiler, CompilerConfig, Engine, EngineBuilder, ModuleMiddleware}; use wasmer_types::{Architecture, CpuFeature, Target}; // Runtime Environment @@ -214,3 +214,9 @@ impl Default for Cranelift { Self::new() } } + +impl From for Engine { + fn from(config: Cranelift) -> Self { + EngineBuilder::new(config).engine() + } +} diff --git a/lib/compiler-llvm/README.md b/lib/compiler-llvm/README.md index 885ce905ff2..9dc0f90b1dc 100644 --- a/lib/compiler-llvm/README.md +++ b/lib/compiler-llvm/README.md @@ -9,8 +9,7 @@ use wasmer::{Store, EngineBuilder}; use wasmer_compiler_llvm::LLVM; let compiler = LLVM::new(); -// Put it into an engine and add it to the store -let mut store = Store::new_with_engine(&EngineBuilder::new(Some(Box::new(compiler)), None, None).engine()); +let mut store = Store::new(compiler); ``` *Note: you can find a [full working example using LLVM compiler here][example].* diff --git a/lib/compiler-llvm/src/config.rs b/lib/compiler-llvm/src/config.rs index 343ede359f0..bb81a85938a 100644 --- a/lib/compiler-llvm/src/config.rs +++ b/lib/compiler-llvm/src/config.rs @@ -8,7 +8,7 @@ use itertools::Itertools; use std::fmt::Debug; use std::sync::Arc; use target_lexicon::Architecture; -use wasmer_compiler::{Compiler, CompilerConfig, ModuleMiddleware}; +use wasmer_compiler::{Compiler, CompilerConfig, Engine, EngineBuilder, ModuleMiddleware}; use wasmer_types::{FunctionType, LocalFunctionIndex, Target, Triple}; /// The InkWell ModuleInfo type @@ -227,3 +227,9 @@ impl Default for LLVM { Self::new() } } + +impl From for Engine { + fn from(config: LLVM) -> Self { + EngineBuilder::new(config).engine() + } +} diff --git a/lib/compiler-singlepass/README.md b/lib/compiler-singlepass/README.md index dcd99e7bf67..6b788772a43 100644 --- a/lib/compiler-singlepass/README.md +++ b/lib/compiler-singlepass/README.md @@ -9,8 +9,7 @@ use wasmer::{Store, EngineBuilder}; use wasmer_compiler_singlepass::Singlepass; let compiler = Singlepass::new(); -// Put it into an engine and add it to the store -let mut store = Store::new_with_engine(&EngineBuilder::new(compiler, None, None).engine()); +let mut store = Store::new(compiler); ``` *Note: you can find a [full working example using Singlepass compiler diff --git a/lib/compiler-singlepass/src/config.rs b/lib/compiler-singlepass/src/config.rs index 95b8699d1f1..b5e1e0f29c2 100644 --- a/lib/compiler-singlepass/src/config.rs +++ b/lib/compiler-singlepass/src/config.rs @@ -3,7 +3,7 @@ use crate::compiler::SinglepassCompiler; use std::sync::Arc; -use wasmer_compiler::{Compiler, CompilerConfig, ModuleMiddleware}; +use wasmer_compiler::{Compiler, CompilerConfig, Engine, EngineBuilder, ModuleMiddleware}; use wasmer_types::{CpuFeature, Features, Target}; #[derive(Debug, Clone)] @@ -62,3 +62,9 @@ impl Default for Singlepass { Self::new() } } + +impl From for Engine { + fn from(config: Singlepass) -> Self { + EngineBuilder::new(config).engine() + } +} diff --git a/lib/compiler/src/artifact_builders/engine_builder.rs b/lib/compiler/src/artifact_builders/engine_builder.rs deleted file mode 100644 index cba5285ac8f..00000000000 --- a/lib/compiler/src/artifact_builders/engine_builder.rs +++ /dev/null @@ -1,39 +0,0 @@ -//! Universal compilation. - -use crate::Compiler; -use wasmer_types::{CompileError, Features}; - -/// The Builder contents of `Engine` -pub struct EngineBuilder { - /// The compiler - compiler: Option>, - /// The features to compile the Wasm module with - features: Features, -} - -impl EngineBuilder { - /// Create a new builder with pre-made components - pub fn new(compiler: Option>, features: Features) -> Self { - Self { compiler, features } - } - - /// Gets the compiler associated to this engine. - pub fn compiler(&self) -> Result<&dyn Compiler, CompileError> { - if self.compiler.is_none() { - return Err(CompileError::Codegen( - "The Engine is not compiled in.".to_string(), - )); - } - Ok(&**self.compiler.as_ref().unwrap()) - } - - /// Validate the module - pub fn validate(&self, data: &[u8]) -> Result<(), CompileError> { - self.compiler()?.validate_module(self.features(), data) - } - - /// The Wasm features - pub fn features(&self) -> &Features { - &self.features - } -} diff --git a/lib/compiler/src/engine/builder.rs b/lib/compiler/src/engine/builder.rs index bf97f86811d..0f07396578d 100644 --- a/lib/compiler/src/engine/builder.rs +++ b/lib/compiler/src/engine/builder.rs @@ -14,14 +14,14 @@ pub struct EngineBuilder { impl EngineBuilder { /// Create a new builder with pre-made components - pub fn new(compiler_config: T, target: Option, features: Option) -> Self + pub fn new(compiler_config: T) -> Self where T: Into>, { Self { compiler_config: Some(compiler_config.into()), - target, - features, + target: None, + features: None, } } @@ -35,13 +35,13 @@ impl EngineBuilder { } /// Set the target - pub fn set_target(&mut self, target: Option) -> &mut Self { + pub fn set_target(mut self, target: Option) -> Self { self.target = target; self } /// Set the features - pub fn set_features(&mut self, features: Option) -> &mut Self { + pub fn set_features(mut self, features: Option) -> Self { self.features = features; self } @@ -70,4 +70,9 @@ impl EngineBuilder { pub fn features(&self) -> Option<&Features> { self.features.as_ref() } + + /// The target + pub fn target(&self) -> Option<&Target> { + self.target.as_ref() + } } diff --git a/lib/compiler/src/engine/inner.rs b/lib/compiler/src/engine/inner.rs index 6b902bbf0e7..5ff4dccf7b0 100644 --- a/lib/compiler/src/engine/inner.rs +++ b/lib/compiler/src/engine/inner.rs @@ -1,5 +1,6 @@ //! Universal compilation. +use crate::engine::builder::EngineBuilder; #[cfg(not(target_arch = "wasm32"))] use crate::Artifact; #[cfg(not(target_arch = "wasm32"))] @@ -347,6 +348,24 @@ impl EngineInner { } } +impl From> for Engine { + fn from(config: Box) -> Self { + EngineBuilder::new(config).engine() + } +} + +impl From for Engine { + fn from(engine_builder: EngineBuilder) -> Self { + engine_builder.engine() + } +} + +impl From<&Self> for Engine { + fn from(engine_ref: &Self) -> Self { + engine_ref.cloned() + } +} + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] #[repr(transparent)] /// A unique identifier for an Engine. diff --git a/lib/middlewares/src/metering.rs b/lib/middlewares/src/metering.rs index e16ef16aad2..3e786777f87 100644 --- a/lib/middlewares/src/metering.rs +++ b/lib/middlewares/src/metering.rs @@ -386,8 +386,7 @@ mod tests { let metering = Arc::new(Metering::new(10, cost_function)); let mut compiler_config = Cranelift::default(); compiler_config.push_middleware(metering); - let mut store = - Store::new_with_engine(&EngineBuilder::new(compiler_config, None, None).engine()); + let mut store = Store::new(EngineBuilder::new(compiler_config)); let module = Module::new(&store, bytecode()).unwrap(); // Instantiate @@ -435,8 +434,7 @@ mod tests { let metering = Arc::new(Metering::new(10, cost_function)); let mut compiler_config = Cranelift::default(); compiler_config.push_middleware(metering); - let mut store = - Store::new_with_engine(&EngineBuilder::new(compiler_config, None, None).engine()); + let mut store = Store::new(EngineBuilder::new(compiler_config)); let module = Module::new(&store, bytecode()).unwrap(); // Instantiate diff --git a/tests/compilers/config.rs b/tests/compilers/config.rs index d150a73bf93..4a3e30d4095 100644 --- a/tests/compilers/config.rs +++ b/tests/compilers/config.rs @@ -42,24 +42,24 @@ impl Config { pub fn store(&self) -> Store { let compiler_config = self.compiler_config(self.canonicalize_nans); let engine = self.engine(compiler_config); - Store::new_with_engine(&*engine) + Store::new(engine) } pub fn headless_store(&self) -> Store { let engine = self.engine_headless(); - Store::new_with_engine(&*engine) + Store::new(engine) } - pub fn engine(&self, compiler_config: Box) -> Box { - let mut engine = wasmer_compiler::EngineBuilder::new(compiler_config, None, None); + pub fn engine(&self, compiler_config: Box) -> Engine { + let mut engine = wasmer_compiler::EngineBuilder::new(compiler_config); if let Some(ref features) = self.features { - engine.set_features(Some(features.clone())); + engine = engine.set_features(Some(features.clone())); } - Box::new(engine.engine()) + engine.engine() } - pub fn engine_headless(&self) -> Box { - Box::new(wasmer_compiler::EngineBuilder::headless().engine()) + pub fn engine_headless(&self) -> Engine { + wasmer_compiler::EngineBuilder::headless().engine() } pub fn compiler_config( diff --git a/tests/compilers/metering.rs b/tests/compilers/metering.rs index 4df524aad4b..5888a1f6b43 100644 --- a/tests/compilers/metering.rs +++ b/tests/compilers/metering.rs @@ -20,7 +20,6 @@ fn run_add_with_limit(mut config: crate::Config, limit: u64) -> Result<()> { (i32.add (local.get 0) (local.get 1))) )"#; - let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; @@ -54,7 +53,6 @@ fn run_loop(mut config: crate::Config, limit: u64, iter_count: i32) -> Result<() ) )"#; let module = Module::new(&store, wat).unwrap(); - let mut env = FunctionEnv::new(&mut store, ()); let import_object = imports! {}; diff --git a/tests/compilers/wast.rs b/tests/compilers/wast.rs index 0f6e3f36cb9..576e62e19fa 100644 --- a/tests/compilers/wast.rs +++ b/tests/compilers/wast.rs @@ -34,7 +34,7 @@ pub fn run_wast(mut config: crate::Config, wast_path: &str) -> anyhow::Result<() config.set_features(features); config.set_nan_canonicalization(try_nan_canonicalization); - let mut store = config.store(); + let store = config.store(); let mut wast = Wast::new_with_spectest(store); // `bulk-memory-operations/bulk.wast` checks for a message that // specifies which element is uninitialized, but our traps don't