Skip to content

Commit

Permalink
Updated tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jul 19, 2022
1 parent edd4a91 commit 85b3801
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 106 deletions.
2 changes: 1 addition & 1 deletion examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() -> anyhow::Result<()> {
let module = Module::new(&store, wasm_bytes)?;

// We declare the host function that we'll use to terminate execution.
fn early_exit(_ctx: FunctionEnvMut<()>) -> Result<(), ExitCode> {
fn early_exit(_env: FunctionEnvMut<()>) -> Result<(), ExitCode> {
// This is where it happens.
Err(ExitCode(1))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> {

// We define a function to act as our "env" "say_hello" function imported in the
// Wasm program above.
fn say_hello_world(_ctx: FunctionEnvMut<'_, ()>) {
fn say_hello_world(_env: FunctionEnvMut<'_, ()>) {
println!("Hello, world!")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/imports_exports.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>> {
// covered in more detail in other examples.
println!("Creating the imported function...");
let host_function_signature = FunctionType::new(vec![], vec![Type::I32]);
let host_function = Function::new(&mut store, &env, &host_function_signature, |_ctx, _args| {
let host_function = Function::new(&mut store, &env, &host_function_signature, |_env, _args| {
Ok(vec![Value::I32(42)])
});

Expand Down
12 changes: 6 additions & 6 deletions examples/imports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine());
let mut ctx1 = FunctionEnv::new(&mut store, ());
let mut env1 = FunctionEnv::new(&mut store, ());
struct MyEnv;
let mut ctx2 = FunctionEnv::new(&mut store, MyEnv {});
let mut env2 = FunctionEnv::new(&mut store, MyEnv {});

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -58,9 +58,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let multiply_dynamic_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]);
let multiply_dynamic = Function::new(
&mut store,
&ctx1,
&env1,
&multiply_dynamic_signature,
|_ctx, args| {
|_env, args| {
println!("Calling `multiply_dynamic`...");

let result = args[0].unwrap_i32() * 2;
Expand All @@ -71,15 +71,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
);

fn multiply(_ctx: FunctionEnvMut<MyEnv>, a: i32) -> i32 {
fn multiply(_env: FunctionEnvMut<MyEnv>, a: i32) -> i32 {
println!("Calling `multiply_native`...");
let result = a * 3;

println!("Result of `multiply_native`: {:?}", result);

result
}
let multiply_native = Function::new_native(&mut store, &ctx2, multiply);
let multiply_native = Function::new_native(&mut store, &env2, multiply);

// Create an import object.
let import_object = imports! {
Expand Down
8 changes: 4 additions & 4 deletions examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

// Create the functions
fn get_counter(ctx: FunctionEnvMut<Env>) -> i32 {
*ctx.data().counter.lock().unwrap()
fn get_counter(env: FunctionEnvMut<Env>) -> i32 {
*env.data().counter.lock().unwrap()
}
fn add_to_counter(mut ctx: FunctionEnvMut<Env>, add: i32) -> i32 {
let mut counter_ref = ctx.data_mut().counter.lock().unwrap();
fn add_to_counter(mut env: FunctionEnvMut<Env>, add: i32) -> i32 {
let mut counter_ref = env.data_mut().counter.lock().unwrap();

*counter_ref += add;
*counter_ref
Expand Down
2 changes: 1 addition & 1 deletion examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

/// A function we'll call through a table.
fn host_callback(_ctx: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 {
fn host_callback(_env: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 {
arg1 + arg2
}

Expand Down
6 changes: 3 additions & 3 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@
//!
//! ```
//! # use wasmer::{imports, Function, FunctionEnv, FunctionEnvMut, Memory, MemoryType, Store, Imports};
//! # fn imports_example(mut ctx: FunctionEnv<()>, mut store: &mut Store) -> Imports {
//! # fn imports_example(mut env: FunctionEnv<()>, mut store: &mut Store) -> Imports {
//! let memory = Memory::new(&mut store, MemoryType::new(1, None, false)).unwrap();
//! imports! {
//! "env" => {
//! "my_function" => Function::new_native(&mut store, &ctx, |_ctx: FunctionEnvMut<()>| println!("Hello")),
//! "my_function" => Function::new_native(&mut store, &env, |_env: FunctionEnvMut<()>| println!("Hello")),
//! "memory" => memory,
//! }
//! }
Expand All @@ -168,7 +168,7 @@
//!
//! ```
//! # use wasmer::{imports, Instance, FunctionEnv, Memory, TypedFunction, Store};
//! # fn exports_example(mut ctx: FunctionEnv<()>, mut store: &mut Store, instance: &Instance) -> anyhow::Result<()> {
//! # fn exports_example(mut env: FunctionEnv<()>, mut store: &mut Store, instance: &Instance) -> anyhow::Result<()> {
//! let memory = instance.exports.get_memory("memory")?;
//! let memory: &Memory = instance.exports.get("some_other_memory")?;
//! let add: TypedFunction<(i32, i32), i32> = instance.exports.get_typed_function(&mut store, "add")?;
Expand Down
34 changes: 17 additions & 17 deletions lib/api/tests/js_externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,28 +183,28 @@ mod js {
fn function_new() {
let mut store = Store::default();
let mut env = FunctionEnv::new(&mut store, ());
let function = Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>| {});
let function = Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>| {});
assert_eq!(
function.ty(&store).clone(),
FunctionType::new(vec![], vec![])
);
let function =
Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>, _a: i32| {});
Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>, _a: i32| {});
assert_eq!(
function.ty(&store).clone(),
FunctionType::new(vec![Type::I32], vec![])
);
let function = Function::new_native(
&mut store,
&env,
|_ctx: FunctionEnvMut<'_, ()>, _a: i32, _b: i64, _c: f32, _d: f64| {},
|_env: FunctionEnvMut<'_, ()>, _a: i32, _b: i64, _c: f32, _d: f64| {},
);
assert_eq!(
function.ty(&store).clone(),
FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
);
let function =
Function::new_native(&mut store, &env, |_ctx: FunctionEnvMut<'_, ()>| -> i32 {
Function::new_native(&mut store, &env, |_env: FunctionEnvMut<'_, ()>| -> i32 {
1
});
assert_eq!(
Expand All @@ -214,7 +214,7 @@ mod js {
let function = Function::new_native(
&mut store,
&env,
|_ctx: FunctionEnvMut<'_, ()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) },
|_env: FunctionEnvMut<'_, ()>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) },
);
assert_eq!(
function.ty(&store).clone(),
Expand Down Expand Up @@ -281,15 +281,15 @@ mod js {
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type = FunctionType::new(vec![Type::I32], vec![]);
let function = Function::new(
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type =
Expand All @@ -298,15 +298,15 @@ mod js {
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type = FunctionType::new(vec![], vec![Type::I32]);
let function = Function::new(
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type =
Expand All @@ -315,7 +315,7 @@ mod js {
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);

Expand All @@ -325,7 +325,7 @@ mod js {
&mut store,
&env,
function_type,
|_ctx: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, ()>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).params(), [Type::V128]);
assert_eq!(
Expand All @@ -349,15 +349,15 @@ mod js {
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type = FunctionType::new(vec![Type::I32], vec![]);
let function = Function::new(
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type =
Expand All @@ -366,15 +366,15 @@ mod js {
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type = FunctionType::new(vec![], vec![Type::I32]);
let function = Function::new(
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);
let function_type =
Expand All @@ -383,7 +383,7 @@ mod js {
&mut store,
&env,
&function_type,
|_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).clone(), function_type);

Expand All @@ -393,7 +393,7 @@ mod js {
&mut store,
&env,
function_type,
|_ctx: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
|_env: FunctionEnvMut<'_, MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&store).params(), [Type::V128]);
assert_eq!(
Expand Down
32 changes: 16 additions & 16 deletions lib/api/tests/js_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ mod js {
let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 });

let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]);
let imported = Function::new(&mut store, &env, &imported_signature, |ctx, args| {
let imported = Function::new(&mut store, &env, &imported_signature, |env, args| {
log!("Calling `imported`...");
let result = args[0].unwrap_i32() * ctx.data().multiplier;
let result = args[0].unwrap_i32() * env.data().multiplier;
log!("Result of `imported`: {:?}", result);
Ok(vec![Value::I32(result)])
});
Expand Down Expand Up @@ -342,10 +342,10 @@ mod js {
multiplier: u32,
}

fn imported_fn(ctx: FunctionEnvMut<'_, Env>, arg: u32) -> u32 {
log!("inside imported_fn: ctx.data is {:?}", ctx.data());
// log!("inside call id is {:?}", ctx.as_store_ref().objects().id);
return ctx.data().multiplier * arg;
fn imported_fn(env: FunctionEnvMut<'_, Env>, arg: u32) -> u32 {
log!("inside imported_fn: env.data is {:?}", env.data());
// log!("inside call id is {:?}", env.as_store_ref().objects().id);
return env.data().multiplier * arg;
}

let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 });
Expand Down Expand Up @@ -401,10 +401,10 @@ mod js {
memory: Option<Memory>,
}

fn imported_fn(ctx: FunctionEnvMut<'_, Env>, arg: u32) -> u32 {
let memory: &Memory = ctx.data().memory.as_ref().unwrap();
let memory_val = memory.uint8view(&ctx).get_index(0);
return (memory_val as u32) * ctx.data().multiplier * arg;
fn imported_fn(env: FunctionEnvMut<'_, Env>, arg: u32) -> u32 {
let memory: &Memory = env.data().memory.as_ref().unwrap();
let memory_val = memory.uint8view(&env).get_index(0);
return (memory_val as u32) * env.data().multiplier * arg;
}

let mut env = FunctionEnv::new(
Expand Down Expand Up @@ -457,10 +457,10 @@ mod js {
let mut env = FunctionEnv::new(&mut store, Env { multiplier: 3 });

fn imported_fn(
ctx: FunctionEnvMut<'_, Env>,
env: FunctionEnvMut<'_, Env>,
args: &[Val],
) -> Result<Vec<Val>, RuntimeError> {
let value = ctx.data().multiplier * args[0].unwrap_i32() as u32;
let value = env.data().multiplier * args[0].unwrap_i32() as u32;
return Ok(vec![Val::I32(value as _)]);
}

Expand Down Expand Up @@ -507,12 +507,12 @@ mod js {
}

fn imported_fn(
ctx: FunctionEnvMut<'_, Env>,
env: FunctionEnvMut<'_, Env>,
args: &[Val],
) -> Result<Vec<Val>, RuntimeError> {
let memory: &Memory = ctx.data().memory.as_ref().unwrap();
let memory_val = memory.uint8view(&ctx).get_index(0);
let value = (memory_val as u32) * ctx.data().multiplier * args[0].unwrap_i32() as u32;
let memory: &Memory = env.data().memory.as_ref().unwrap();
let memory_val = memory.uint8view(&env).get_index(0);
let value = (memory_val as u32) * env.data().multiplier * args[0].unwrap_i32() as u32;
return Ok(vec![Val::I32(value as _)]);
}

Expand Down
Loading

0 comments on commit 85b3801

Please sign in to comment.