Skip to content

Commit

Permalink
Improved EngineBuilder and Store API
Browse files Browse the repository at this point in the history
- Store::new() now takes an impl Into<Engine>.
- Added Into<Engine> impls in each of the compilers
- Updated docs/migration_to_3.0.0.md on API usage
  • Loading branch information
syrusakbary authored and epilys committed Jul 28, 2022
1 parent b2dc07e commit 4384ddf
Show file tree
Hide file tree
Showing 57 changed files with 191 additions and 263 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Engine>`.
- [#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
Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 6 additions & 38 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,71 +149,39 @@ 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);
}
}

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);
}
}
Expand Down
19 changes: 17 additions & 2 deletions docs/migration_to_3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions examples/compiler_cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -33,7 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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.
Expand Down
3 changes: 1 addition & 2 deletions examples/compiler_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -33,7 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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.
Expand Down
3 changes: 1 addition & 2 deletions examples/compiler_singlepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -33,7 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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.
Expand Down
4 changes: 1 addition & 3 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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...");
Expand Down
7 changes: 3 additions & 4 deletions examples/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand Down Expand Up @@ -49,10 +48,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//
// 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.
Expand Down
7 changes: 3 additions & 4 deletions examples/engine_cross_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -70,10 +69,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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.
Expand Down
4 changes: 2 additions & 2 deletions examples/engine_dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//
// 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.
Expand Down
21 changes: 5 additions & 16 deletions examples/engine_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -78,14 +71,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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.
Expand All @@ -105,8 +94,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{
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...");
Expand Down
4 changes: 1 addition & 3 deletions examples/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -39,8 +38,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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...");
Expand Down
4 changes: 1 addition & 3 deletions examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -40,8 +39,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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...");
Expand Down
4 changes: 1 addition & 3 deletions examples/exports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -40,8 +39,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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...");
Expand Down
4 changes: 1 addition & 3 deletions examples/exports_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -37,8 +36,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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...");
Expand Down
9 changes: 5 additions & 4 deletions examples/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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)?;

Expand Down
Loading

0 comments on commit 4384ddf

Please sign in to comment.