Skip to content

Commit

Permalink
Improved examples
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Mar 16, 2023
1 parent 58828df commit 195d652
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 119 deletions.
57 changes: 29 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ autoexamples = false
wasmer = { version = "=3.2.0-alpha.1", path = "lib/api", default-features = false }
wasmer-compiler = { version = "=3.2.0-alpha.1", path = "lib/compiler", features = [
"compiler",
] }
], optional=true }
wasmer-compiler-cranelift = { version = "=3.2.0-alpha.1", path = "lib/compiler-cranelift", optional = true }
wasmer-compiler-singlepass = { version = "=3.2.0-alpha.1", path = "lib/compiler-singlepass", optional = true }
wasmer-compiler-llvm = { version = "=3.2.0-alpha.1", path = "lib/compiler-llvm", optional = true }
Expand Down Expand Up @@ -74,7 +74,7 @@ rustc_version = "0.4"

[dev-dependencies]
wasmer = { version = "=3.2.0-alpha.1", path = "lib/api", default-features = false, features = [
"cranelift",
"jsc",
] }
anyhow = "1.0"
criterion = "0.3"
Expand Down Expand Up @@ -102,6 +102,7 @@ default = [
"emscripten",
"middlewares",
]
jsc = ["wasmer/jsc", "wat", "wasmer/std"]
engine = ["universal"]
universal = []
cache = ["wasmer-cache"]
Expand Down Expand Up @@ -140,10 +141,10 @@ split-debuginfo = "unpacked"
name = "static_and_dynamic_functions"
harness = false

[[example]]
name = "early-exit"
path = "examples/early_exit.rs"
required-features = ["cranelift"]
# [[example]]
# name = "early-exit"
# path = "examples/early_exit.rs"
# required-features = []

[[example]]
name = "engine"
Expand Down Expand Up @@ -183,27 +184,27 @@ required-features = ["llvm"]
[[example]]
name = "exported-function"
path = "examples/exports_function.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "exported-global"
path = "examples/exports_global.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "exported-memory"
path = "examples/exports_memory.rs"
required-features = ["cranelift"]
# [[example]]
# name = "exported-memory"
# path = "examples/exports_memory.rs"
# required-features = []

[[example]]
name = "imported-function"
path = "examples/imports_function.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "imported-global"
path = "examples/imports_global.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "tunables-limit-memory"
Expand All @@ -213,52 +214,52 @@ required-features = ["cranelift"]
[[example]]
name = "wasi"
path = "examples/wasi.rs"
required-features = ["cranelift", "wasi"]
required-features = ["wasi"]

[[example]]
name = "wasi-manual-setup"
path = "examples/wasi_manual_setup.rs"
required-features = ["cranelift", "wasi"]
required-features = ["wasi"]

[[example]]
name = "wasi-pipes"
path = "examples/wasi_pipes.rs"
required-features = ["cranelift", "wasi"]
required-features = ["wasi"]

[[example]]
name = "table"
path = "examples/table.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "memory"
path = "examples/memory.rs"
required-features = ["cranelift"]
# [[example]]
# name = "memory"
# path = "examples/memory.rs"
# required-features = []

[[example]]
name = "instance"
path = "examples/instance.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "errors"
path = "examples/errors.rs"
required-features = ["cranelift"]
required-features = ["sys"]

[[example]]
name = "imported-function-env"
path = "examples/imports_function_env.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "imported-function-env-global"
path = "examples/imports_function_env_global.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "hello-world"
path = "examples/hello_world.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "metering"
Expand All @@ -268,7 +269,7 @@ required-features = ["cranelift"]
[[example]]
name = "imports-exports"
path = "examples/imports_exports.rs"
required-features = ["cranelift"]
required-features = []

[[example]]
name = "features"
Expand Down
6 changes: 1 addition & 5 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use anyhow::bail;
use std::fmt;
use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;

// First we need to create an error type that we'll use to signal the end of execution.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -51,10 +50,7 @@ fn main() -> anyhow::Result<()> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
3 changes: 1 addition & 2 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, Instance, Module, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -38,7 +37,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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
6 changes: 1 addition & 5 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, Instance, Module, Store, TypedFunction, Value};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -36,10 +35,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
16 changes: 7 additions & 9 deletions examples/exports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//! Ready?
use wasmer::{imports, wat2wasm, Instance, Module, Mutability, Store, Type, TypedFunction, Value};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -34,10 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down Expand Up @@ -105,10 +101,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Trying to set the value of a immutable global (`const`)
// will result in a `RuntimeError`.
let result = one.set(&mut store, Value::F32(42.0));
assert_eq!(
result.expect_err("Expected an error").message(),
"Attempted to set an immutable global"
);
// The global is immutable
assert!(result.is_err());
// assert_eq!(
// result.expect_err("Expected an error").message(),
// "Attempted to set an immutable global"
// );

let one_result = one.get(&mut store);
println!("`one` value after `set`: {:?}", one_result);
Expand Down
6 changes: 1 addition & 5 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, Instance, Module, Store, TypedFunction, WasmPtr};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -33,10 +32,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
13 changes: 2 additions & 11 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! ```
use wasmer::{imports, wat2wasm, Function, Instance, Module, Store, TypedFunction};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> anyhow::Result<()> {
// First we create a simple Wasm program to use with Wasmer.
Expand All @@ -34,16 +33,8 @@ fn main() -> anyhow::Result<()> {
"#,
)?;

// Next we create the `Store`, the top level type in the Wasmer API.
//
// 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.
//
// 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(Cranelift::default());
// Create a Store.
let mut store = Store::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
6 changes: 1 addition & 5 deletions examples/imports_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use wasmer::{
imports, wat2wasm, Function, FunctionType, Global, Instance, Memory, Module, Store, Table,
Type, Value,
};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module.
Expand All @@ -40,10 +39,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
7 changes: 2 additions & 5 deletions examples/imports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, FunctionType, Instance, Module,
Store, Type, TypedFunction, Value,
};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -41,10 +40,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

struct MyEnv;
let env = FunctionEnv::new(&mut store, MyEnv {});

Expand Down
6 changes: 1 addition & 5 deletions examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Instance, Module, Store,
TypedFunction,
};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -48,10 +47,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
6 changes: 1 addition & 5 deletions examples/imports_function_env_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use wasmer::{
imports, wat2wasm, Function, FunctionEnv, FunctionEnvMut, Global, Instance, Module, Store,
TypedFunction, Value,
};
use wasmer_compiler_cranelift::Cranelift;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Let's declare the Wasm module with the text representation.
Expand All @@ -50,10 +49,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;

// Create a Store.
// 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(Cranelift::default());
let mut store = Store::default();

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
Loading

0 comments on commit 195d652

Please sign in to comment.