Skip to content

Commit

Permalink
Improved Store API
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jul 28, 2022
1 parent 23f020a commit 2f43321
Show file tree
Hide file tree
Showing 44 changed files with 70 additions and 65 deletions.
14 changes: 6 additions & 8 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,21 @@ 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(wasmer_compiler_llvm::LLVM::new()));
let mut store = Store::new(EngineBuilder::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(
let mut store = Store::new(EngineBuilder::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(
let mut store = Store::new(EngineBuilder::new(
wasmer_compiler_singlepass::Singlepass::new(),
));
run_basic_static_function(&store, "singlepass", c);
Expand All @@ -174,22 +173,21 @@ 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(wasmer_compiler_llvm::LLVM::new()));
let mut store = Store::new(EngineBuilder::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(
let mut store = Store::new(EngineBuilder::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(
let mut store = Store::new(EngineBuilder::new(
wasmer_compiler_singlepass::Singlepass::new(),
));
run_basic_dynamic_function(&store, "singlepass", c);
Expand Down
2 changes: 1 addition & 1 deletion examples/compiler_cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,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));
let mut store = Store::new(EngineBuilder::new(compiler));

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/compiler_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,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));
let mut store = Store::new(EngineBuilder::new(compiler));

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/compiler_singlepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,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));
let mut store = Store::new(EngineBuilder::new(compiler));

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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
2 changes: 1 addition & 1 deletion examples/engine_cross_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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
2 changes: 1 addition & 1 deletion examples/engine_dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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
4 changes: 2 additions & 2 deletions examples/engine_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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...");
// Let's compile the Wasm module.
Expand All @@ -99,7 +99,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Creating headless Universal engine...");
// We create a headless Universal engine.
let engine = EngineBuilder::headless();
let mut store = Store::new_with_engine(engine);
let mut store = Store::new(engine);
let mut env = FunctionEnv::new(&mut store, ());

println!("Deserializing module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
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);
let mut store = Store::new(engine);
let mut env = FunctionEnv::new(&mut store, ());
let module = Module::new(&store, wasm_bytes)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +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()));
let mut store = Store::new(EngineBuilder::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.
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env1 = FunctionEnv::new(&mut store, ());
struct MyEnv;
let mut env2 = FunctionEnv::new(&mut store, MyEnv {});
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +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));
let mut store = Store::new(EngineBuilder::new(compiler_config));
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
Expand Down
2 changes: 1 addition & 1 deletion examples/platform_ios_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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.
Expand Down
2 changes: 1 addition & 1 deletion examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));
let mut env = FunctionEnv::new(&mut store, ());
// Then compile our Wasm.
let module = Module::new(&store, wasm_bytes)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/wasi_pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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()));
let mut store = Store::new(EngineBuilder::new(Cranelift::default()));

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/deterministic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,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();
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/equivalence_universal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl std::fmt::Debug for WasmSmithModule {
#[cfg(feature = "singlepass")]
fn maybe_instantiate_singlepass(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let compiler = Singlepass::default();
let mut store = Store::new_with_engine(EngineBuilder::new(compiler));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes);
let module = match module {
Ok(m) => m,
Expand All @@ -68,7 +68,7 @@ fn maybe_instantiate_cranelift(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let mut compiler = Cranelift::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new_with_engine(EngineBuilder::new(compiler));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;
Ok(Some(instance))
Expand All @@ -79,7 +79,7 @@ fn maybe_instantiate_llvm(wasm_bytes: &[u8]) -> Result<Option<Instance>> {
let mut compiler = LLVM::default();
compiler.canonicalize_nans(true);
compiler.enable_verifier();
let mut store = Store::new_with_engine(EngineBuilder::new(compiler));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;
Ok(Some(instance))
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,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));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&module, &imports! {}) {
Ok(_) => {}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/universal_cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,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));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&module, &imports! {}) {
Ok(_) => {}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/universal_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,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));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes).unwrap();
match Instance::new(&module, &imports! {}) {
Ok(_) => {}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/universal_singlepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fuzz_target!(|module: WasmSmithModule| {
}

let compiler = Singlepass::default();
let mut store = Store::new_with_engine(EngineBuilder::new(compiler));
let mut store = Store::new(EngineBuilder::new(compiler));
let module = Module::new(&store, &wasm_bytes);
let module = match module {
Ok(m) => m,
Expand Down
12 changes: 7 additions & 5 deletions lib/api/src/sys/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ pub struct Store {
impl Store {
#[cfg(feature = "compiler")]
/// Creates a new `Store` with a specific [`CompilerConfig`].
pub fn new(compiler_config: Box<dyn CompilerConfig>) -> Self {
let engine = EngineBuilder::new(compiler_config).engine();
pub fn new(engine: impl Into<Engine>) -> 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: impl Into<Engine>) -> Self {
let engine = engine.into();
let target = engine.target().clone();
Self::new_with_tunables(engine, BaseTunables::for_target(&target))
Self::new(engine)
}

/// Set the trap handler in this store.
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/src/wasm_c_api/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub unsafe extern "C" fn wasm_store_new(
engine: Option<&wasm_engine_t>,
) -> Option<Box<wasm_store_t>> {
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 {
Expand Down
Loading

0 comments on commit 2f43321

Please sign in to comment.