Skip to content

Commit

Permalink
Finished up the second example
Browse files Browse the repository at this point in the history
  • Loading branch information
torch2424 committed Oct 24, 2019
1 parent 18b5718 commit 9f54155
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
13 changes: 10 additions & 3 deletions hello-world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,28 @@ fn main() -> error::Result<()> {

// 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'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(42)])?;
.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));

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

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

// Return OK since everything executed successfully!
Ok(())
Expand Down
24 changes: 12 additions & 12 deletions passing-data/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::fs::File;
// Import the wasmer runtime so we can use it
use wasmer_runtime::{
instantiate,
Value,
imports,
error,
Func
Expand All @@ -21,29 +20,29 @@ fn main() -> error::Result<()> {

// 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/strings-wasm-is-cool/pkg/strings_wasm_is_cool_bg.wasm").expect("Incorrect file path to wasm module.");
let mut file = File::open("./example-rust-wasm-crate/strings-wasm-is-cool/pkg/strings_wasm_is_cool_bg.wasm").expect("Incorrect file path to wasm module.");

// 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's get our byte slice ( [u8] ) from our wasm_vec.
let wasm_bytes = wasm_vec.as_slice();

// 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)?;

// Lets get the contextr and memory of our Wasm Instance
// Lets get the context and memory of our Wasm Instance
let wasm_instance_context = instance.context();
let wasm_instance_memory = wasm_instance_context.memory(0);

// Let's get the pointer to to our buffer in the wasm memory
// Let's get the pointer to the buffer defined by the wasm module in the wasm memory
let get_wasm_memory_buffer_pointer: Func<(), i32> =
instance
.func("get_wasm_memory_buffer_pointer")
Expand All @@ -53,27 +52,28 @@ fn main() -> error::Result<()> {

// Let's write a string to the wasm memory
let original_string = "Did you know";
println!("The original string is: {}", original_string);
let memory_writer = wasm_buffer_pointer.deref(wasm_instance_memory, 0, original_string.len() as u32).unwrap();
for (i, b) in original_string.bytes().enumerate() {
memory_writer[i].set(b);
}

// Let's call the exported function that concatenates a phrase to our string.
let add_wasm_is_cool: Func<u32, i32> = instance.func("add_wasm_is_cool").expect("Wasm is cool export");
let new_string_length = add_wasm_is_cool.call(original_string.len() as u32).unwrap();

// Get our pointer again, since memory may have shifted around
let new_pointer_response = get_wasm_memory_buffer_pointer.call().unwrap() as u32;
let new_wasm_buffer_pointer: WasmPtr<u8, Array> = WasmPtr::new(new_pointer_response);

// Read the string from that pointer.
if let Some(my_str) = new_wasm_buffer_pointer.get_utf8_string(wasm_instance_memory, new_string_length as u32) {
println!("yooo {}", my_str);
}
// Read the string from that new pointer.
let new_string = new_wasm_buffer_pointer.get_utf8_string(wasm_instance_memory, new_string_length as u32).unwrap();
println!("The new string is: {}", new_string);

// Asserting that the returned value from the function is our expected value.
// assert_eq!(values[0], Value::I32(43));
assert_eq!(new_string, "Did you know Wasm is cool!");

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

// Return OK since everything executed successfully!
Expand Down
Binary file removed passing-data/strings_wasm_is_cool.wasm
Binary file not shown.

0 comments on commit 9f54155

Please sign in to comment.