Skip to content

Commit

Permalink
Merge pull request #3 from wasmerio/host-side-rust-cleanup
Browse files Browse the repository at this point in the history
Clean up host side of Rust Wasm examples
  • Loading branch information
torch2424 authored Nov 20, 2019
2 parents e3a9245 + 4849327 commit 8a1fc34
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 115 deletions.
39 changes: 19 additions & 20 deletions rust/early-exit/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
// Import the Filesystem so we can read our .wasm file
use std::io::prelude::*;
use std::fs::File;
use std::io::prelude::*;

// Import the wasmer runtime so we can use it
use wasmer_runtime::{
instantiate,
imports,
Func,
error,
// Include the function macro
func,
imports,
instantiate,
// Include the Context for our Wasm Instance for passing imported host functions
Ctx
Ctx,
Func,
};

const WASM_FILE_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/example-rust-wasm-crate/early-exit-import/pkg/early_exit_import_bg.wasm"
);

// Our entry point to our application
fn main() -> error::Result<()> {

// Let's read in our .wasm file as bytes

// Let's open the file.
// The file path may be different depending where you run `cargo run`, and where you place the file.
let mut file = File::open("./example-rust-wasm-crate/early-exit-import/pkg/early_exit_import_bg.wasm").expect("Incorrect file path to wasm module.");
// Let's open the file.
let mut file = File::open(WASM_FILE_PATH).expect(&format!("wasm file at {}", WASM_FILE_PATH));

// Let's read the file into a Vec
let mut wasm_vec = Vec::new();
file.read_to_end(&mut wasm_vec).expect("Error reading the wasm file");

// Let's get our byte slice ( [u8] ) from ouw wasm_vec.
let wasm_bytes = wasm_vec.as_slice();
file.read_to_end(&mut wasm_vec)
.expect("Error reading the wasm file");

// Now that we have the wasm file as bytes, let's run it with the wasmer runtime

Expand All @@ -47,23 +48,23 @@ fn main() -> error::Result<()> {
};

// Let's create an instance of wasm module running in the wasmer-runtime
let instance = instantiate(wasm_bytes, &import_object)?;
let instance = instantiate(&wasm_vec, &import_object)?;

// Let's call the exported "exit_early" function on the wasm module.
let exit_early_func: Func<(), i32> = instance
let exit_early_func: Func<(), i32> = instance
.func("exit_early")
.expect("exit_early functioon not found");
.expect("exit_early function not found");
let response = exit_early_func.call();

match response {
Ok(value) => {
// This should have thrown an error, return an error
panic!("exit_early did not error. Returned the value: {}", value);
},
}
Err(e) => {
// Log the error
println!("Error from exit_early: {}", e);
},
}
}

// Log a success message.
Expand All @@ -81,5 +82,3 @@ fn interrupt_execution(_ctx: &mut Ctx) -> Result<(), ()> {
// Return an error, which will immediately stop execution of the wasm module
Err(())
}


36 changes: 16 additions & 20 deletions rust/handling-errors/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
// Import the Filesystem so we can read our .wasm file
use std::io::prelude::*;
use std::fs::File;
use std::io::prelude::*;

// Import the wasmer runtime so we can use it
use wasmer_runtime::{
instantiate,
Func,
imports,
error,
};
use wasmer_runtime::{error, imports, instantiate, Func};

const WASM_FILE_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/example-rust-wasm-crate/throw-wasm-error/pkg/throw_wasm_error_bg.wasm"
);

// Our entry point to our application
fn main() -> error::Result<()> {

// Let's read in our .wasm file as bytes

// Let's open the file.
// The file path may be different depending where you run `cargo run`, and where you place the file.
let mut file = File::open("./example-rust-wasm-crate/throw-wasm-error/pkg/throw_wasm_error_bg.wasm").expect("Incorrect file path to wasm module.");
// Let's open the file.
let mut file = File::open(WASM_FILE_PATH).expect(&format!("wasm file at {}", WASM_FILE_PATH));

// Let's read the file into a Vec
let mut wasm_vec = Vec::new();
file.read_to_end(&mut wasm_vec).expect("Error reading the wasm file");

// Let's get our byte slice ( [u8] ) from ouw wasm_vec.
let wasm_bytes = wasm_vec.as_slice();
file.read_to_end(&mut wasm_vec)
.expect("Error reading the wasm file");

// Now that we have the wasm file as bytes, let's run it with the wasmer runtime

// Our import object, that allows exposing functions to our wasm module.
// We're not importing anything, so make an empty import object.
let import_object = imports!{};
let import_object = imports! {};

// Let's create an instance of wasm module running in the wasmer-runtime
let instance = instantiate(wasm_bytes, &import_object)?;
let instance = instantiate(&wasm_vec, &import_object)?;

// Let's call the exported "throw_error" function ont the wasm module.
let throw_error_func: Func<(), ()> = instance
.func("throw_wasm_error")
.expect("throw_wasm_error function was not found");

// Unwrapping here, so that the error is thrown here
let _response = throw_error_func.call().unwrap();
// We return the error with `?`
let _response = throw_error_func.call()?;

/*
Commenting the pattern matching, to show the unwrapped error above.
Commenting the pattern matching, to show the unwrapped error above.
match response {
Ok(_) => {
Expand Down
45 changes: 20 additions & 25 deletions rust/hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,53 @@
// Import the Filesystem so we can read our .wasm file
use std::io::prelude::*;
use std::fs::File;
use std::io::prelude::*;

// Import the wasmer runtime so we can use it
use wasmer_runtime::{
instantiate,
Value,
imports,
error,
};
use wasmer_runtime::{error, imports, instantiate, Func};

const WASM_FILE_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/example-rust-wasm-crate/add-one/pkg/add_one_bg.wasm"
);

// Our entry point to our application
fn main() -> error::Result<()> {

// Let's read in our .wasm file as bytes

// Let's open the file.
// The file path may be different depending where you run `cargo run`, and where you place the file.
let mut file = File::open("./example-rust-wasm-crate/add-one/pkg/add_one_bg.wasm").expect("Incorrect file path to wasm module.");
// Let's open the file.
let mut file = File::open(WASM_FILE_PATH).expect(&format!("wasm file at {}", WASM_FILE_PATH));

// Let's read the file into a Vec
let mut wasm_vec = Vec::new();
file.read_to_end(&mut wasm_vec).expect("Error reading the wasm file");

// Let's get our byte slice ( [u8] ) from ouw wasm_vec.
let wasm_bytes = wasm_vec.as_slice();
file.read_to_end(&mut wasm_vec)
.expect("Error reading the wasm file");

// Now that we have the wasm file as bytes, let's run it with the wasmer runtime

// Our import object, that allows exposing functions to our wasm module.
// We're not importing anything, so make an empty import object.
let import_object = imports!{};
let import_object = imports! {};

// Let's create an instance of wasm module running in the wasmer-runtime
let instance = instantiate(wasm_bytes, &import_object)?;
let instance = instantiate(&wasm_vec, &import_object)?;

// Let's get a number we want to add one to
let value_to_add = 42;
println!("Original Value: {}", value_to_add);

// Let's call the exported "add_one" function ont the wasm module.
let values = instance
.dyn_func("add_one")?
.call(&[Value::I32(value_to_add)])?;

// Asserting that the returned value from the function is our expected value.
assert_eq!(values[0], Value::I32(43));
// Let's get `add_one` as a function which takes one `u32` and returns one `u32`
let add_one: Func<u32, u32> = instance.func("add_one")?;
let result = add_one.call(value_to_add)?;

// Log the new value
println!("New Value: {}", 43);

// Asserting that the returned value from the function is our expected value.
assert_eq!(result, 43);

// Log a success message.
println!("Success!");

// Return OK since everything executed successfully!
Ok(())
}
57 changes: 28 additions & 29 deletions rust/host-functions/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
// Import the Filesystem so we can read our .wasm file
use std::io::prelude::*;
use std::fs::File;
use std::io::prelude::*;

// Import the wasmer runtime so we can use it
use wasmer_runtime::{
instantiate,
Value,
imports,
error,
// Include the function macro
func,
imports,
instantiate,
// Include the Context for our Wasm Instance for passing imported host functions
Ctx
Ctx,
Func,
};

const WASM_FILE_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/example-rust-wasm-crate/host-counter/pkg/host_counter_bg.wasm"
);

static mut COUNTER: i32 = 0;

// Our entry point to our application
fn main() -> error::Result<()> {

// Let's read in our .wasm file as bytes

// Let's open the file.
// The file path may be different depending where you run `cargo run`, and where you place the file.
let mut file = File::open("./example-rust-wasm-crate/host-counter/pkg/host_counter_bg.wasm").expect("Incorrect file path to wasm module.");
// Let's open the file.
let mut file = File::open(WASM_FILE_PATH).expect(&format!("wasm file at {}", WASM_FILE_PATH));

// Let's read the file into a Vec
let mut wasm_vec = Vec::new();
file.read_to_end(&mut wasm_vec).expect("Error reading the wasm file");

// Let's get our byte slice ( [u8] ) from ouw wasm_vec.
let wasm_bytes = wasm_vec.as_slice();
file.read_to_end(&mut wasm_vec)
.expect("Error reading the wasm file");

// Now that we have the wasm file as bytes, let's run it with the wasmer runtime

Expand All @@ -50,26 +51,26 @@ fn main() -> error::Result<()> {
};

// Let's create an instance of wasm module running in the wasmer-runtime
let instance = instantiate(wasm_bytes, &import_object)?;
let instance = instantiate(&wasm_vec, &import_object)?;

// Define the number of times we want to loop our increment
let number_of_times_to_loop: i32 = 5;

// Let's call the exported "increment_counter_loop" function ont the wasm module.
let values = instance
.dyn_func("increment_counter_loop")?
.call(&[Value::I32(number_of_times_to_loop)])?;
// Let's get `increment_counter_loop` as a function which takes one `i32` and returns one `i32`
let increment_counter_loop: Func<i32, i32> = instance.func("increment_counter_loop")?;
let result = increment_counter_loop.call(number_of_times_to_loop)?;

unsafe {
// Assert our counter is the expected value
assert_eq!(number_of_times_to_loop, COUNTER);
// Let's get a copy of the value in `COUNTER`
let counter_value = unsafe { COUNTER };

// Asserting that the returned value from the function is our expected value.
assert_eq!(values[0], Value::I32(COUNTER));
// Assert our counter is the expected value
assert_eq!(number_of_times_to_loop, counter_value);

// Log the new value
println!("New Counter Value: {}", COUNTER);
}
// Asserting that the returned value from the function is our expected value.
assert_eq!(result, counter_value);

// Log the new value
println!("New Counter Value: {}", counter_value);

// Log a success message.
println!("Success!");
Expand All @@ -84,9 +85,7 @@ fn main() -> error::Result<()> {
//
// This function returns our global counter.
fn get_counter(_ctx: &mut Ctx) -> i32 {
unsafe {
COUNTER
}
unsafe { COUNTER }
}

// Define a Host function that will be imported to the wasm module
Expand Down
4 changes: 1 addition & 3 deletions rust/passing-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ edition = "2018"

[dependencies]
# Add the wasmer-runtime as a dependency
wasmer-runtime = "0.9.0"
# Add the runtime core as a dependency for WasmPtr
wasmer-runtime-core = "0.9.0"
wasmer-runtime = "0.10.2"
Loading

0 comments on commit 8a1fc34

Please sign in to comment.