Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated al examples to new Context API #2996

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand All @@ -34,6 +34,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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

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

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&module, &import_object)?;
let instance = Instance::new(&mut ctx, &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(&[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
7 changes: 4 additions & 3 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, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_llvm::LLVM;

Expand All @@ -34,6 +34,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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

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

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&module, &import_object)?;
let instance = Instance::new(&mut ctx, &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(&[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
7 changes: 4 additions & 3 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, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_singlepass::Singlepass;

Expand All @@ -34,6 +34,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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

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

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&module, &import_object)?;
let instance = Instance::new(&mut ctx, &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(&[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
16 changes: 10 additions & 6 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

use anyhow::bail;
use std::fmt;
use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction};
use wasmer::{
imports, wat2wasm, Context, ContextMut, Function, Instance, Module, Store, TypedFunction,
};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand Down Expand Up @@ -56,36 +58,38 @@ fn main() -> anyhow::Result<()> {
// 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, ());

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() -> Result<(), ExitCode> {
fn early_exit(_ctx: ContextMut<()>) -> Result<(), ExitCode> {
// This is where it happens.
Err(ExitCode(1))
}

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

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&module, &import_object)?;
let instance = Instance::new(&mut ctx, &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("run")?;
let run_func: TypedFunction<(i32, i32), i32> =
instance.exports.get_typed_function(&mut ctx, "run")?;

// When we call a function it can either succeed or fail. We expect it to fail.
match run_func.call(1, 7) {
match run_func.call(&mut ctx, 1, 7) {
Ok(result) => {
bail!(
"Expected early termination with `ExitCode`, found: {}",
Expand Down
6 changes: 4 additions & 2 deletions examples/engine_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use tempfile::NamedTempFile;
use wasmer::imports;
use wasmer::wat2wasm;
use wasmer::Context;
use wasmer::Instance;
use wasmer::Module;
use wasmer::Store;
Expand Down Expand Up @@ -106,6 +107,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// We create a headless Universal engine.
let engine = Universal::headless().engine();
let store = Store::new_with_engine(&engine);
let mut ctx = Context::new(&store, ());

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

println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&module, &import_object)?;
let instance = Instance::new(&mut ctx, &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(&[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
7 changes: 4 additions & 3 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, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand Down Expand Up @@ -53,6 +53,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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

println!("Compiling module...");
// Here we go.
Expand All @@ -72,12 +73,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(&module, &import_object)?;
let instance = Instance::new(&mut ctx, &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(&[Value::I32(1), Value::I32(2)])?;
let results = sum.call(&mut ctx, &[Value::I32(1), Value::I32(2)])?;

println!("Results: {:?}", results);
assert_eq!(results.to_vec(), vec![Value::I32(3)]);
Expand Down
11 changes: 6 additions & 5 deletions examples/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!
//! Ready?

use wasmer::{imports, wat2wasm, Instance, Module, Store};
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, TypedFunction};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand All @@ -40,6 +40,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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, ());

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

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

// Here we go.
//
Expand All @@ -59,14 +60,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// produce an error.
//
// Let's get it.
let div_by_zero = instance
let div_by_zero: TypedFunction<(), i32> = instance
.exports
.get_function("div_by_zero")?
.native::<(), i32>()?;
.native(&mut ctx)?;

println!("Calling `div_by_zero` function...");
// Let's call the `div_by_zero` exported function.
let result = div_by_zero.call();
let result = div_by_zero.call(&mut ctx);

// When we call a function it can either succeed or fail. We expect it to fail.
match result {
Expand Down
11 changes: 6 additions & 5 deletions examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//!
//! Ready?

use wasmer::{imports, wat2wasm, Instance, Module, Store, Value};
use wasmer::{imports, wat2wasm, Context, Instance, Module, Store, TypedFunction, Value};
use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

Expand All @@ -41,6 +41,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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, ());

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

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

// Here we go.
//
Expand All @@ -73,7 +74,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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 args = [Value::I32(1), Value::I32(2)];
let result = sum.call(&args)?;
let result = sum.call(&mut ctx, &args)?;

println!("Results: {:?}", result);
assert_eq!(result.to_vec(), vec![Value::I32(3)]);
Expand All @@ -86,13 +87,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// `Rets`, respectively for the parameters and the results. If
// those values don't match the exported function signature, an
// error will be raised.
let sum_native = sum.native::<(i32, i32), i32>()?;
let sum_native: TypedFunction<(i32, i32), i32> = sum.native(&mut ctx)?;

println!("Calling `sum` function (natively)...");
// Let's call the `sum` exported function. The parameters are
// statically typed Rust values of type `i32` and `i32`. The
// result, in this case particular case, in a unit of type `i32`.
let result = sum_native.call(3, 4)?;
let result = sum_native.call(&mut ctx, 3, 4)?;

println!("Results: {:?}", result);
assert_eq!(result, 7);
Expand Down
Loading