Skip to content

Commit

Permalink
Merge pull request #3013 from wasmerio/context_api-refactor
Browse files Browse the repository at this point in the history
Context api refactor
  • Loading branch information
syrusakbary authored Jul 18, 2022
2 parents 8278263 + 818e936 commit cb8f402
Show file tree
Hide file tree
Showing 214 changed files with 5,845 additions and 5,777 deletions.
12 changes: 6 additions & 6 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,22 @@ 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 store =
let mut store =
Store::new_with_engine(&Universal::new(wasmer_compiler_llvm::LLVM::new()).engine());
run_basic_static_function(&store, "llvm", c);
}

#[cfg(feature = "cranelift")]
{
let store = Store::new_with_engine(
let mut store = Store::new_with_engine(
&Universal::new(wasmer_compiler_cranelift::Cranelift::new()).engine(),
);
run_basic_static_function(&store, "cranelift", c);
}

#[cfg(feature = "singlepass")]
{
let store = Store::new_with_engine(
let mut store = Store::new_with_engine(
&Universal::new(wasmer_compiler_singlepass::Singlepass::new()).engine(),
);
run_basic_static_function(&store, "singlepass", c);
Expand All @@ -174,22 +174,22 @@ fn run_static_benchmarks(_c: &mut Criterion) {
fn run_dynamic_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let store =
let mut store =
Store::new_with_engine(&Universal::new(wasmer_compiler_llvm::LLVM::new()).engine());
run_basic_dynamic_function(&store, "llvm", c);
}

#[cfg(feature = "cranelift")]
{
let store = Store::new_with_engine(
let mut store = Store::new_with_engine(
&Universal::new(wasmer_compiler_cranelift::Cranelift::new()).engine(),
);
run_basic_dynamic_function(&store, "cranelift", c);
}

#[cfg(feature = "singlepass")]
{
let store = Store::new_with_engine(
let mut store = Store::new_with_engine(
&Universal::new(wasmer_compiler_singlepass::Singlepass::new()).engine(),
);
run_basic_dynamic_function(&store, "singlepass", c);
Expand Down
19 changes: 9 additions & 10 deletions docs/migration_to_3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TODO
You need a Store to create a context. Simple context is created using:

```rust
let ctx = Context::new(&store, ());
let ctx = FunctionEnv::new(&mut store, ());
```

For a Context with a custom Env, it will be similar:
Expand All @@ -59,7 +59,7 @@ For a Context with a custom Env, it will be similar:
struct Env {
counter: i32,
}
let ctx = Context::new(&store, Env{counter: 0});
let ctx = FunctionEnv::new(&mut store, Env{counter: 0});
```

### Managing imports
Expand All @@ -72,26 +72,25 @@ let import_object: Imports = imports! {
"host_function" => host_function,
},
};
let instance = Instance::new(&mut ctx, &module, &import_object).expect("Could not instantiate module.");
let instance = Instance::new(&mut store, &module, &import_object).expect("Could not instantiate module.");
```

You can also build the `Imports` object manually:

```rust
let mut import_object: Imports = Imports::new();
import_object.define("env", "host_function", host_function);
let instance = Instance::new(&mut ctx, &module, &import_object).expect("Could not instantiate module.");
let instance = Instance::new(&mut store, &module, &import_object).expect("Could not instantiate module.");
```

For WASI, don't forget to import memory to `WasiEnv`

```rust
let mut wasi_env = WasiState::new("hello").finalize()?;
let mut ctx = Context::new(&store, wasi_env.clone());
let import_object = wasi_env.import_object(&mut ctx.as_context_mut(), &module)?;
let instance = Instance::new(&mut ctx, &module, &import_object).expect("Could not instantiate module.");
let import_object = wasi_env.import_object(&mut store, &module)?;
let instance = Instance::new(&mut store, &module, &import_object).expect("Could not instantiate module.");
let memory = instance.exports.get_memory("memory")?;
ctx.data_mut().set_memory(memory.clone());
wasi_env.data_mut(&mut store).set_memory(memory.clone());
```

#### `ChainableNamedResolver` is removed
Expand Down Expand Up @@ -122,7 +121,7 @@ let wasm_bytes = wat2wasm(

let compiler_config = Cranelift::default();
let engine = Universal::new(compiler_config).engine();
let store = Store::new(&engine);
let mut store = Store::new(&engine);
let module = Module::new(&store, wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;
```
Expand All @@ -137,7 +136,7 @@ let wasm_bytes = wat2wasm(
)?;

let compiler_config = Cranelift::default();
let store = Store::new(&compiler_config);
let mut store = Store::new(&compiler_config);
let module = Module::new(&store, wasm_bytes)?;
let instance = Instance::new(&module, &imports! {})?;
```
Expand Down
9 changes: 4 additions & 5 deletions examples/compiler_cranelift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
//! Ready?
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand All @@ -33,8 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let compiler = Cranelift::default();

// Create the store
let store = Store::new_with_engine(&Universal::new(compiler).engine());
let mut ctx = Context::new(&store, ());
let mut store = Store::new_with_engine(&Universal::new(compiler).engine());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -45,14 +44,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&mut ctx, &module, &import_object)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

let sum = instance.exports.get_function("sum")?;

println!("Calling `sum` function...");
// Let's call the `sum` exported function. The parameters are a
// slice of `Value`s. The results are a boxed slice of `Value`s.
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut store, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
9 changes: 4 additions & 5 deletions examples/compiler_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
//! Ready?
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_llvm::LLVM;

Expand All @@ -33,8 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let compiler = LLVM::default();

// Create the store
let store = Store::new_with_engine(&Universal::new(compiler).engine());
let mut ctx = Context::new(&store, ());
let mut store = Store::new_with_engine(&Universal::new(compiler).engine());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -45,14 +44,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&mut ctx, &module, &import_object)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

let sum = instance.exports.get_function("sum")?;

println!("Calling `sum` function...");
// Let's call the `sum` exported function. The parameters are a
// slice of `Value`s. The results are a boxed slice of `Value`s.
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut store, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
9 changes: 4 additions & 5 deletions examples/compiler_singlepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
//! Ready?
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_singlepass::Singlepass;

Expand All @@ -33,8 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let compiler = Singlepass::default();

// Create the store
let store = Store::new_with_engine(&Universal::new(compiler).engine());
let mut ctx = Context::new(&store, ());
let mut store = Store::new_with_engine(&Universal::new(compiler).engine());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -45,14 +44,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&mut ctx, &module, &import_object)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

let sum = instance.exports.get_function("sum")?;

println!("Calling `sum` function...");
// Let's call the `sum` exported function. The parameters are a
// slice of `Value`s. The results are a boxed slice of `Value`s.
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut store, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
17 changes: 9 additions & 8 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use anyhow::bail;
use std::fmt;
use wasmer::{
imports, wat2wasm, Context, ContextMut, Function, Instance, Module, Store, TypedFunction,
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store,
TypedFunction,
};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;
Expand Down Expand Up @@ -57,39 +58,39 @@ 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 store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine());
let mut ctx = Context::new(&store, ());
let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine());
let env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
let module = Module::new(&store, wasm_bytes)?;

// We declare the host function that we'll use to terminate execution.
fn early_exit(_ctx: ContextMut<()>) -> Result<(), ExitCode> {
fn early_exit(_ctx: FunctionEnvMut<()>) -> Result<(), ExitCode> {
// This is where it happens.
Err(ExitCode(1))
}

// Create an import object.
let import_object = imports! {
"env" => {
"early_exit" => Function::new_native(&mut ctx, early_exit),
"early_exit" => Function::new_native(&mut store, &env, early_exit),
}
};

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&mut ctx, &module, &import_object)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

// Here we go.
//
// Get the `run` function which we'll use as our entrypoint.
println!("Calling `run` function...");
let run_func: TypedFunction<(i32, i32), i32> =
instance.exports.get_typed_function(&mut ctx, "run")?;
instance.exports.get_typed_function(&mut store, "run")?;

// When we call a function it can either succeed or fail. We expect it to fail.
match run_func.call(&mut ctx, 1, 7) {
match run_func.call(&mut store, 1, 7) {
Ok(result) => {
bail!(
"Expected early termination with `ExitCode`, found: {}",
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 @@ -79,7 +79,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.engine();

// Create a store, that holds the engine.
let store = Store::new_with_engine(&engine);
let mut store = Store::new_with_engine(&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).engine();
// Create a store, that holds the engine.
let store = Store::new_with_engine(&engine);
let mut store = Store::new_with_engine(&engine);
println!("Compiling module...");
// Here we go.
Expand Down
12 changes: 6 additions & 6 deletions examples/engine_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
use tempfile::NamedTempFile;
use wasmer::imports;
use wasmer::wat2wasm;
use wasmer::Context;
use wasmer::FunctionEnv;
use wasmer::Instance;
use wasmer::Module;
use wasmer::Store;
Expand Down Expand Up @@ -85,7 +85,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let engine = Universal::new(compiler_config).engine();

// Create a store, that holds the engine.
let store = Store::new_with_engine(&engine);
let mut store = Store::new_with_engine(&engine);

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -106,8 +106,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Creating headless Universal engine...");
// We create a headless Universal engine.
let engine = Universal::headless().engine();
let store = Store::new_with_engine(&engine);
let mut ctx = Context::new(&store, ());
let mut store = Store::new_with_engine(&engine);
let mut env = FunctionEnv::new(&mut store, ());

println!("Deserializing module...");
// Here we go.
Expand All @@ -127,12 +127,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&mut ctx, &module, &import_object)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

println!("Calling `sum` function...");
// The Wasm module exports a function called `sum`.
let sum = instance.exports.get_function("sum")?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut store, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
9 changes: 4 additions & 5 deletions examples/engine_universal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//!
//! Ready?
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand Down Expand Up @@ -52,8 +52,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let engine = Universal::new(compiler_config).engine();

// Create a store, that holds the engine.
let store = Store::new_with_engine(&engine);
let mut ctx = Context::new(&store, ());
let mut store = Store::new_with_engine(&engine);

println!("Compiling module...");
// Here we go.
Expand All @@ -73,12 +72,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Instantiating module...");
// And here we go again. Let's instantiate the Wasm module.
let instance = Instance::new(&mut ctx, &module, &import_object)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

println!("Calling `sum` function...");
// The Wasm module exports a function called `sum`.
let sum = instance.exports.get_function("sum")?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut store, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
Loading

0 comments on commit cb8f402

Please sign in to comment.