Skip to content
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
8 changes: 4 additions & 4 deletions benches/run.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use criterion::{Criterion, criterion_group, criterion_main};
use wasmer::{sys::*, *};
use wasmer::*;

static BENCHMARKS_ARTIFACTS_BASE_URL: &str = "https://pub-083d1a0568d446d1aa5b2e07bd16983b.r2.dev";

#[allow(unreachable_code)]
fn get_engine() -> Engine {
#[cfg(feature = "llvm")]
return LLVM::new().into();
return wasmer_compiler_llvm::LLVM::new().into();

#[cfg(feature = "singlepass")]
return Singlepass::new().into();
return wasmer_compiler_singlepass::Singlepass::new().into();

#[cfg(feature = "cranelift")]
return Cranelift::new().into();
return wasmer_compiler_cranelift::Cranelift::new().into();

#[cfg(not(any(feature = "cranelift", feature = "llvm", feature = "singlepass")))]
return Default::default();
Expand Down
48 changes: 23 additions & 25 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,29 @@ pub fn run_basic_static_function(store: &mut Store, compiler_name: &str, c: &mut
});

let dyn_f_many: &Function = instance.exports.get("add20").unwrap();
let f_many: TypedFunction<
(
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
),
i32,
> = dyn_f_many.typed(store).unwrap();
type TupleWithMany = (
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
i32,
);
let f_many: TypedFunction<TupleWithMany, i32> = dyn_f_many.typed(store).unwrap();
c.bench_function(
&format!("basic static func with many args {compiler_name}"),
|b| {
Expand Down
2 changes: 1 addition & 1 deletion examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() -> anyhow::Result<()> {
// 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(&mut store, "run")?;
instance.exports.get_typed_function(&store, "run")?;

// When we call a function it can either succeed or fail. We expect it to fail.
match run_func.call(&mut store, 1, 7) {
Expand Down
8 changes: 4 additions & 4 deletions examples/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let div_by_zero: TypedFunction<(), i32> = instance
.exports
.get_function("div_by_zero")?
.typed(&mut store)?;
.typed(&store)?;

println!("Calling `div_by_zero` function...");
// Let's call the `div_by_zero` exported function.
Expand All @@ -81,12 +81,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let frames = e.trace();
let frames_len = frames.len();

for i in 0..frames_len {
for (i, frame) in frames.iter().enumerate() {
println!(
" Frame #{}: {:?}::{:?}",
frames_len - i,
frames[i].module_name(),
frames[i].function_name().or(Some("<func>")).unwrap()
frame.module_name(),
frame.function_name().unwrap_or("<func>")
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ 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_typed: TypedFunction<(i32, i32), i32> = sum.typed(&mut store)?;
let sum_typed: TypedFunction<(i32, i32), i32> = sum.typed(&store)?;

println!("Calling `sum` function (natively)...");
// Let's call the `sum` exported function. The parameters are
Expand Down
12 changes: 4 additions & 8 deletions examples/exports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//
// We will use an exported function for the `one` global
// and the Global API for `some`.
let get_one: TypedFunction<(), f32> = instance
.exports
.get_function("get_one")?
.typed(&mut store)?;
let get_one: TypedFunction<(), f32> =
instance.exports.get_function("get_one")?.typed(&store)?;

let one_value = get_one.call(&mut store)?;
let some_value = some.get(&mut store);
Expand Down Expand Up @@ -117,10 +115,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 2. Using the Global API directly.
//
// We will use both for the `some` global.
let set_some: TypedFunction<f32, ()> = instance
.exports
.get_function("set_some")?
.typed(&mut store)?;
let set_some: TypedFunction<f32, ()> =
instance.exports.get_function("set_some")?.typed(&store)?;
set_some.call(&mut store, 21.0)?;
let some_result = some.get(&mut store);
println!("`some` value after `set_some`: {:?}", some_result);
Expand Down
6 changes: 3 additions & 3 deletions examples/exports_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let instance = Instance::new(&mut store, &module, &import_object)?;

let load: TypedFunction<(), (WasmPtr<u8>, i32)> =
instance.exports.get_typed_function(&mut store, "load")?;
instance.exports.get_typed_function(&store, "load")?;

// Here we go.
//
Expand Down Expand Up @@ -86,8 +86,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// of each element.
let new_str = b"Hello, Wasmer!";
let values = ptr.slice(&memory_view, new_str.len() as u32).unwrap();
for i in 0..new_str.len() {
values.index(i as u64).write(new_str[i]).unwrap();
for (i, &str) in new_str.iter().enumerate() {
values.index(i as u64).write(str).unwrap();
}

// And now, let's see the result.
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> anyhow::Result<()> {
//
// Recall that the Wasm module exported a function named "run", this is getting
// that exported function from the `Instance`.
let run_func: TypedFunction<(), ()> = instance.exports.get_typed_function(&mut store, "run")?;
let run_func: TypedFunction<(), ()> = instance.exports.get_typed_function(&store, "run")?;

// Finally, we call our exported Wasm function which will call our "say_hello"
// function and return.
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//
// The Wasm module exports a function called `sum`. Let's get it.
let sum: TypedFunction<(i32, i32), i32> =
instance.exports.get_function("sum")?.typed(&mut store)?;
instance.exports.get_function("sum")?.typed(&store)?;

println!("Calling `sum` function...");
// Let's call the `sum` exported function. It will call each
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let increment_counter_loop: TypedFunction<i32, i32> = instance
.exports
.get_function("increment_counter_loop")?
.typed(&mut store)?;
.typed(&store)?;

let counter_value: i32 = *shared_counter.lock().unwrap();
println!("Initial ounter value: {:?}", counter_value);
Expand Down
2 changes: 1 addition & 1 deletion examples/imports_function_env_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let increment_counter_loop: TypedFunction<i32, i32> = instance
.exports
.get_function("increment_counter_loop")?
.typed(&mut store)?;
.typed(&store)?;

let counter_value: i32 = *shared_counter.lock().unwrap();
println!("Initial ounter value: {:?}", counter_value);
Expand Down
18 changes: 6 additions & 12 deletions examples/imports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//
// The Wasm module only imports some globals. We'll have to interact
// with them either using the Global API or exported functions.
let get_some: TypedFunction<(), f32> = instance
.exports
.get_function("get_some")?
.typed(&mut store)?;
let get_other: TypedFunction<(), f32> = instance
.exports
.get_function("get_other")?
.typed(&mut store)?;
let get_some: TypedFunction<(), f32> =
instance.exports.get_function("get_some")?.typed(&store)?;
let get_other: TypedFunction<(), f32> =
instance.exports.get_function("get_other")?.typed(&store)?;

let some_result = get_some.call(&mut store)?;
let other_result = get_other.call(&mut store)?;
Expand All @@ -94,10 +90,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Altering global values through exported functions...");
// Changes made to global through exported functions will
// be reflected on the host side.
let set_other: TypedFunction<f32, ()> = instance
.exports
.get_function("set_other")?
.typed(&mut store)?;
let set_other: TypedFunction<f32, ()> =
instance.exports.get_function("set_other")?.typed(&store)?;
set_other.call(&mut store, 42.0)?;

println!("other value (via Global API): {:?}", other.get(&mut store));
Expand Down
6 changes: 2 additions & 4 deletions examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Here we are retrieving the exported function. We won't go into details here
// as the main focus of this example is to show how to create an instance out
// of a Wasm module and have basic interactions with it.
let add_one: TypedFunction<i32, i32> = instance
.exports
.get_function("add_one")?
.typed(&mut store)?;
let add_one: TypedFunction<i32, i32> =
instance.exports.get_function("add_one")?.typed(&store)?;

println!("Calling `add_one` function...");
let result = add_one.call(&mut store, 1)?;
Expand Down
13 changes: 6 additions & 7 deletions examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//!
//! Ready?

#[cfg(not(feature = "wamr"))]
use std::mem;
use wasmer::{Bytes, Instance, Module, Pages, Store, TypedFunction, imports, wat2wasm};

Expand Down Expand Up @@ -68,13 +69,11 @@ fn main() -> anyhow::Result<()> {
// The module exports some utility functions, let's get them.
//
// These function will be used later in this example.
let mem_size: TypedFunction<(), i32> = instance
.exports
.get_typed_function(&mut store, "mem_size")?;
let get_at: TypedFunction<i32, i32> =
instance.exports.get_typed_function(&mut store, "get_at")?;
let mem_size: TypedFunction<(), i32> =
instance.exports.get_typed_function(&store, "mem_size")?;
let get_at: TypedFunction<i32, i32> = instance.exports.get_typed_function(&store, "get_at")?;
let set_at: TypedFunction<(i32, i32), ()> =
instance.exports.get_typed_function(&mut store, "set_at")?;
instance.exports.get_typed_function(&store, "set_at")?;
let memory = instance.exports.get_memory("memory")?;

// We now have an instance ready to be used.
Expand All @@ -90,7 +89,7 @@ fn main() -> anyhow::Result<()> {
println!("Querying memory size...");
let memory_view = memory.view(&store);
assert_eq!(memory_view.size(), Pages::from(1));
assert_eq!(memory_view.size().bytes(), Bytes::from(65536 as usize));
assert_eq!(memory_view.size().bytes(), Bytes::from(65536_usize));
assert_eq!(memory_view.data_size(), 65536);

// Sometimes, the guest module may also export a function to let you
Expand Down
6 changes: 2 additions & 4 deletions examples/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ fn main() -> anyhow::Result<()> {
//
// Our module exports a single `add_one` function. We want to
// measure the cost of executing this function.
let add_one: TypedFunction<i32, i32> = instance
.exports
.get_function("add_one")?
.typed(&mut store)?;
let add_one: TypedFunction<i32, i32> =
instance.exports.get_function("add_one")?.typed(&store)?;

println!("Calling `add_one` function once...");
add_one.call(&mut store, 1)?;
Expand Down
6 changes: 3 additions & 3 deletions examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> {
// to be passed to the function found in the table.
let call_via_table: TypedFunction<(i32, i32, i32), i32> = instance
.exports
.get_typed_function(&mut store, "call_callback")?;
.get_typed_function(&store, "call_callback")?;

// And then call it with table index 1 and arguments 2 and 7.
let result = call_via_table.call(&mut store, 1, 2, 7)?;
Expand All @@ -72,7 +72,7 @@ fn main() -> anyhow::Result<()> {
// We then get the table from the instance.
let guest_table = instance.exports.get_table("__indirect_function_table")?;
// And demonstrate that it has the properties that we set in the Wasm.
assert_eq!(guest_table.size(&mut store), 3);
assert_eq!(guest_table.size(&store), 3);
assert_eq!(
guest_table.ty(&store),
TableType {
Expand Down Expand Up @@ -106,7 +106,7 @@ fn main() -> anyhow::Result<()> {
let previous_size = guest_table.grow(&mut store, 3, func.into())?;
assert_eq!(previous_size, 3);

assert_eq!(guest_table.size(&mut store), 6);
assert_eq!(guest_table.size(&store), 6);
assert_eq!(
guest_table.ty(&store),
TableType {
Expand Down
2 changes: 1 addition & 1 deletion examples/tunables_limit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<T: Tunables> LimitingTunables<T> {
/// valid. However, this can produce invalid types, such that
/// validate_memory must be called before creating the memory.
fn adjust_memory(&self, requested: &MemoryType) -> MemoryType {
let mut adjusted = requested.clone();
let mut adjusted = *requested;
if requested.maximum.is_none() {
adjusted.maximum = Some(self.limit);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/api/tests/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn table_set() -> Result<(), String> {
let v = v.unwrap();

let v = if let Value::ExternRef(Some(ext)) = v {
ext.downcast::<u32>(&mut store)
ext.downcast::<u32>(&store)
} else {
return Err("table.get does not match `ExternRef(Some(..))`!".into());
};
Expand All @@ -175,7 +175,7 @@ fn table_set() -> Result<(), String> {
let v = v.unwrap();

let v = if let Value::ExternRef(Some(ext)) = v {
ext.downcast::<u32>(&mut store)
ext.downcast::<u32>(&store)
} else {
return Err("table.get does not match `ExternRef(Some(..))`!".into());
};
Expand All @@ -194,7 +194,7 @@ fn table_set() -> Result<(), String> {
let v = v.unwrap();

let v = if let Value::ExternRef(Some(ext)) = v {
ext.downcast::<u32>(&mut store)
ext.downcast::<u32>(&store)
} else {
return Err("table.get does not match `ExternRef(Some(..))`!".into());
};
Expand Down Expand Up @@ -250,7 +250,7 @@ fn table_copy() -> Result<(), String> {
fn memory_new() -> Result<(), String> {
let mut store = Store::default();
let memory_type = MemoryType {
shared: if cfg!(feature = "wamr") { true } else { false },
shared: cfg!(feature = "wamr"),
minimum: Pages(0),
maximum: Some(Pages(10)),
};
Expand Down
8 changes: 4 additions & 4 deletions lib/api/tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ fn calling_host_functions_with_negative_values_works() -> Result<(), String> {
"host" => {
"host_func1" => Function::new_typed(&mut store, |p: u64| {
println!("host_func1: Found number {p}");
assert_eq!(p, u64::max_value());
assert_eq!(p, u64::MAX);
}),
"host_func2" => Function::new_typed(&mut store, |p: u32| {
println!("host_func2: Found number {p}");
assert_eq!(p, u32::max_value());
assert_eq!(p, u32::MAX);
}),
"host_func3" => Function::new_typed(&mut store, |p: i64| {
println!("host_func3: Found number {p}");
Expand All @@ -215,15 +215,15 @@ fn calling_host_functions_with_negative_values_works() -> Result<(), String> {
}),
"host_func6" => Function::new_typed(&mut store, |p: u16| {
println!("host_func6: Found number {p}");
assert_eq!(p, u16::max_value());
assert_eq!(p, u16::MAX);
}),
"host_func7" => Function::new_typed(&mut store, |p: i8| {
println!("host_func7: Found number {p}");
assert_eq!(p, -1);
}),
"host_func8" => Function::new_typed(&mut store, |p: u8| {
println!("host_func8: Found number {p}");
assert_eq!(p, u8::max_value());
assert_eq!(p, u8::MAX);
}),
}
};
Expand Down
Loading
Loading