mirror of
https://github.com/mii443/wasmer.git
synced 2025-09-02 07:29:21 +00:00
Improved examples
This commit is contained in:
57
Cargo.toml
57
Cargo.toml
@ -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 }
|
||||
@ -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"
|
||||
@ -102,6 +102,7 @@ default = [
|
||||
"emscripten",
|
||||
"middlewares",
|
||||
]
|
||||
jsc = ["wasmer/jsc", "wat", "wasmer/std"]
|
||||
engine = ["universal"]
|
||||
universal = []
|
||||
cache = ["wasmer-cache"]
|
||||
@ -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"
|
||||
@ -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"
|
||||
@ -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"
|
||||
@ -268,7 +269,7 @@ required-features = ["cranelift"]
|
||||
[[example]]
|
||||
name = "imports-exports"
|
||||
path = "examples/imports_exports.rs"
|
||||
required-features = ["cranelift"]
|
||||
required-features = []
|
||||
|
||||
[[example]]
|
||||
name = "features"
|
||||
|
@ -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)]
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
@ -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);
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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 {});
|
||||
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -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.
|
||||
@ -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.
|
||||
|
@ -16,7 +16,6 @@
|
||||
//! Ready?
|
||||
|
||||
use wasmer::{imports, wat2wasm, 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.
|
||||
@ -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.
|
||||
@ -88,10 +84,7 @@ 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 = some.set(&mut store, Value::F32(42.0));
|
||||
assert_eq!(
|
||||
result.expect_err("Expected an error").message(),
|
||||
"Attempted to set an immutable global"
|
||||
);
|
||||
assert!(result.is_err());
|
||||
|
||||
other.set(&mut store, Value::F32(21.0))?;
|
||||
let other_result = other.get(&mut store);
|
||||
|
@ -15,7 +15,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.
|
||||
@ -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.
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
use std::mem;
|
||||
use wasmer::{imports, wat2wasm, Bytes, Instance, Module, Pages, Store, TypedFunction};
|
||||
use wasmer_compiler_cranelift::Cranelift;
|
||||
|
||||
// this example is a work in progress:
|
||||
// TODO: clean it up and comment it https://github.com/wasmerio/wasmer/issues/1749
|
||||
@ -53,10 +52,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.
|
||||
|
@ -1,7 +1,6 @@
|
||||
use wasmer::{
|
||||
imports, wat2wasm, Function, Instance, Module, Store, TableType, Type, TypedFunction, Value,
|
||||
};
|
||||
use wasmer_compiler_cranelift::Cranelift;
|
||||
|
||||
/// A function we'll call through a table.
|
||||
fn host_callback(arg1: i32, arg2: i32) -> i32 {
|
||||
@ -49,8 +48,8 @@ fn main() -> anyhow::Result<()> {
|
||||
.as_bytes(),
|
||||
)?;
|
||||
|
||||
// We set up our store with an engine and a compiler.
|
||||
let mut store = Store::new(Cranelift::default());
|
||||
// Create a Store.
|
||||
let mut store = Store::default();
|
||||
// Then compile our Wasm.
|
||||
let module = Module::new(&store, wasm_bytes)?;
|
||||
let import_object = imports! {};
|
||||
|
@ -17,7 +17,6 @@
|
||||
use std::io::Read;
|
||||
|
||||
use wasmer::{Module, Store};
|
||||
use wasmer_compiler_cranelift::Cranelift;
|
||||
use wasmer_wasi::{Pipe, WasiEnv};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -29,10 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let wasm_bytes = std::fs::read(wasm_path)?;
|
||||
|
||||
// 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.
|
||||
|
@ -16,7 +16,6 @@
|
||||
//! Ready?
|
||||
|
||||
use wasmer::{Instance, Module, Store};
|
||||
use wasmer_compiler_cranelift::Cranelift;
|
||||
use wasmer_wasi::WasiEnv;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -28,10 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let wasm_bytes = std::fs::read(wasm_path)?;
|
||||
|
||||
// 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.
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use wasmer::{Module, Store};
|
||||
use wasmer_compiler_cranelift::Cranelift;
|
||||
use wasmer_wasi::{Pipe, WasiEnv};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -25,10 +24,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let wasm_bytes = std::fs::read(wasm_path)?;
|
||||
|
||||
// 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.
|
||||
|
Reference in New Issue
Block a user