Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context api tests #2974

Merged
merged 2 commits into from
Jun 30, 2022
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
178 changes: 96 additions & 82 deletions lib/api/src/sys/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,53 +1229,61 @@ mod inner {
#[cfg(test)]
mod test_wasm_type_list {
use super::*;
use crate::Context as WasmerContext;
use crate::Store;
use wasmer_types::Type;

/*
#[test]
fn test_from_array() {
assert_eq!(<()>::from_array([]), ());
assert_eq!(<i32>::from_array([1]), (1i32));
assert_eq!(<(i32, i64)>::from_array([1, 2]), (1i32, 2i64));
let store = Store::default();
let mut ctx = WasmerContext::new(&store, ());
assert_eq!(<()>::from_array(&mut ctx, []), ());
assert_eq!(<i32>::from_array(&mut ctx, [RawValue{i32: 1}]), (1i32));
assert_eq!(<(i32, i64)>::from_array(&mut ctx, [RawValue{i32:1}, RawValue{i64:2}]), (1i32, 2i64));
assert_eq!(
<(i32, i64, f32, f64)>::from_array([
1,
2,
(3.1f32).to_bits().into(),
(4.2f64).to_bits().into()
<(i32, i64, f32, f64)>::from_array(&mut ctx, [
RawValue{i32:1},
RawValue{i64:2},
RawValue{f32: 3.1f32},
RawValue{f64: 4.2f64}
]),
(1, 2, 3.1f32, 4.2f64)
);
}

#[test]
fn test_into_array() {
assert_eq!(().into_array(), [0i128; 0]);
assert_eq!((1).into_array(), [1]);
assert_eq!((1i32, 2i64).into_array(), [1, 2]);
let store = Store::default();
let mut ctx = WasmerContext::new(&store, ());
assert_eq!(().into_array(&mut ctx), [0i128; 0]);
assert_eq!((1i32).into_array(&mut ctx), [1i32]);
assert_eq!((1i32, 2i64).into_array(&mut ctx), [RawValue{i32: 1}, RawValue{i64: 2}]);
assert_eq!(
(1i32, 2i32, 3.1f32, 4.2f64).into_array(),
[1, 2, (3.1f32).to_bits().into(), (4.2f64).to_bits().into()]
(1i32, 2i32, 3.1f32, 4.2f64).into_array(&mut ctx),
[RawValue{i32: 1}, RawValue{i32: 2}, RawValue{ f32: 3.1f32}, RawValue{f64: 4.2f64}]
);
}

*/
#[test]
fn test_empty_array() {
assert_eq!(<()>::empty_array().len(), 0);
assert_eq!(<i32>::empty_array().len(), 1);
assert_eq!(<(i32, i64)>::empty_array().len(), 2);
}

/*
#[test]
fn test_from_c_struct() {
assert_eq!(<()>::from_c_struct(S0()), ());
assert_eq!(<i32>::from_c_struct(S1(1)), (1i32));
assert_eq!(<(i32, i64)>::from_c_struct(S2(1, 2)), (1i32, 2i64));
let store = Store::default();
let mut ctx = WasmerContext::new(&store, ());
assert_eq!(<()>::from_c_struct(&mut ctx, S0()), ());
assert_eq!(<i32>::from_c_struct(&mut ctx, S1(1)), (1i32));
assert_eq!(<(i32, i64)>::from_c_struct(&mut ctx, S2(1, 2)), (1i32, 2i64));
assert_eq!(
<(i32, i64, f32, f64)>::from_c_struct(S4(1, 2, 3.1, 4.2)),
<(i32, i64, f32, f64)>::from_c_struct(&mut ctx, S4(1, 2, 3.1, 4.2)),
(1i32, 2i64, 3.1f32, 4.2f64)
);
}

*/
#[test]
fn test_wasm_types_for_uni_values() {
assert_eq!(<i32>::wasm_types(), [Type::I32]);
Expand All @@ -1297,68 +1305,74 @@ mod inner {
);
}
}
/*
#[allow(non_snake_case)]
#[cfg(test)]
mod test_function {
use super::*;
use crate::Store;
use crate::Context as WasmerContext;
use wasmer_types::Type;

fn func() {}
fn func__i32() -> i32 {
0
}
fn func_i32( _a: i32) {}
fn func_i32__i32( a: i32) -> i32 {
a * 2
}
fn func_i32_i32__i32( a: i32, b: i32) -> i32 {
a + b
}
fn func_i32_i32__i32_i32( a: i32, b: i32) -> (i32, i32) {
(a, b)
}
fn func_f32_i32__i32_f32( a: f32, b: i32) -> (i32, f32) {
(b, a)
}

#[allow(non_snake_case)]
#[cfg(test)]
mod test_function {
use super::*;
use wasmer_types::Type;

fn func() {}
fn func__i32() -> i32 {
0
}
fn func_i32(_a: i32) {}
fn func_i32__i32(a: i32) -> i32 {
a * 2
}
fn func_i32_i32__i32(a: i32, b: i32) -> i32 {
a + b
}
fn func_i32_i32__i32_i32(a: i32, b: i32) -> (i32, i32) {
(a, b)
}
fn func_f32_i32__i32_f32(a: f32, b: i32) -> (i32, f32) {
(b, a)
}

#[test]
fn test_function_types() {
assert_eq!(
StaticFunction::new(func).ty(),
FunctionType::new(vec![], vec![])
);
assert_eq!(
StaticFunction::new(func__i32).ty(),
FunctionType::new(vec![], vec![Type::I32])
);
assert_eq!(
StaticFunction::new(func_i32).ty(),
FunctionType::new(vec![Type::I32], vec![])
);
assert_eq!(
StaticFunction::new(func_i32__i32).ty(),
FunctionType::new(vec![Type::I32], vec![Type::I32])
);
assert_eq!(
StaticFunction::new(func_i32_i32__i32).ty(),
FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32])
);
assert_eq!(
StaticFunction::new(func_i32_i32__i32_i32).ty(),
FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32, Type::I32])
);
assert_eq!(
StaticFunction::new(func_f32_i32__i32_f32).ty(),
FunctionType::new(vec![Type::F32, Type::I32], vec![Type::I32, Type::F32])
);
}
#[test]
fn test_function_types() {
let store = Store::default();
let mut ctx = WasmerContext::new(&store, ());
use wasmer_types::FunctionType;
assert_eq!(
StaticFunction::new(func).ty(&mut ctx),
FunctionType::new(vec![], vec![])
);
assert_eq!(
StaticFunction::new(func__i32).ty(&mut ctx),
FunctionType::new(vec![], vec![Type::I32])
);
assert_eq!(
StaticFunction::new(func_i32).ty(),
FunctionType::new(vec![Type::I32], vec![])
);
assert_eq!(
StaticFunction::new(func_i32__i32).ty(),
FunctionType::new(vec![Type::I32], vec![Type::I32])
);
assert_eq!(
StaticFunction::new(func_i32_i32__i32).ty(),
FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32])
);
assert_eq!(
StaticFunction::new(func_i32_i32__i32_i32).ty(),
FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32, Type::I32])
);
assert_eq!(
StaticFunction::new(func_f32_i32__i32_f32).ty(),
FunctionType::new(vec![Type::F32, Type::I32], vec![Type::I32, Type::F32])
);
}

#[test]
fn test_function_pointer() {
let f = StaticFunction::new(func_i32__i32);
let function = unsafe { std::mem::transmute::<_, fn(usize, i32) -> i32>(f.address) };
assert_eq!(function(0, 3), 6);
#[test]
fn test_function_pointer() {
let f = StaticFunction::new(func_i32__i32);
let function = unsafe { std::mem::transmute::<_, fn(usize, i32) -> i32>(f.address) };
assert_eq!(function(0, 3), 6);
}
}
}
*/
}
66 changes: 38 additions & 28 deletions lib/api/src/sys/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,17 @@ macro_rules! import_namespace {
#[cfg(test)]
mod test {
use crate::sys::exports::Exportable;
use crate::sys::Export;
use crate::sys::{Global, Store, Val};
use crate::sys::Context as WasmerContext;
use crate::sys::Exports;
use crate::sys::{Global, Store, Value};
use wasmer_types::Type;

use wasmer_vm::VMExtern;
/*
#[test]
fn namespace() {
let store = Store::default();
let g1 = Global::new(&store, Val::I32(0));
let mut ctx = WasmerContext::new(&store, ());
let g1 = Global::new(&mut ctx, Value::I32(0));
let namespace = namespace! {
"happy" => g1
};
Expand All @@ -286,70 +289,74 @@ mod test {
let happy_dog_entry = imports1.get_export("dog", "happy").unwrap();

assert!(
if let Export::Global(happy_dog_global) = happy_dog_entry.to_vm_extern() {
happy_dog_global.from.ty().ty == Type::I32
if let VMExtern::Global(happy_dog_global) = happy_dog_entry.to_vm_extern() {
happy_dog_global.get(&mut ctx).ty == Type::I32
} else {
false
}
);
}

*/
#[test]
fn imports_macro_allows_trailing_comma_and_none() {
use crate::sys::ContextMut;
use crate::sys::Function;

let store = Default::default();
let mut ctx = WasmerContext::new(&store, ());

fn func(arg: i32) -> i32 {
fn func(_ctx: ContextMut<()>, arg: i32) -> i32 {
arg + 1
}

let _ = imports! {
"env" => {
"func" => Function::new_native(&store, func),
"func" => Function::new_native(&mut ctx, func),
},
};
let _ = imports! {
"env" => {
"func" => Function::new_native(&store, func),
"func" => Function::new_native(&mut ctx, func),
}
};
let _ = imports! {
"env" => {
"func" => Function::new_native(&store, func),
"func" => Function::new_native(&mut ctx, func),
},
"abc" => {
"def" => Function::new_native(&store, func),
"def" => Function::new_native(&mut ctx, func),
}
};
let _ = imports! {
"env" => {
"func" => Function::new_native(&store, func)
"func" => Function::new_native(&mut ctx, func)
},
};
let _ = imports! {
"env" => {
"func" => Function::new_native(&store, func)
"func" => Function::new_native(&mut ctx, func)
}
};
let _ = imports! {
"env" => {
"func1" => Function::new_native(&store, func),
"func2" => Function::new_native(&store, func)
"func1" => Function::new_native(&mut ctx, func),
"func2" => Function::new_native(&mut ctx, func)
}
};
let _ = imports! {
"env" => {
"func1" => Function::new_native(&store, func),
"func2" => Function::new_native(&store, func),
"func1" => Function::new_native(&mut ctx, func),
"func2" => Function::new_native(&mut ctx, func),
}
};
}

#[test]
fn chaining_works() {
let store = Store::default();
let g = Global::new(&store, Val::I32(0));
let mut ctx = WasmerContext::new(&store, ());

let g = Global::new(&mut ctx, Value::I32(0));

let mut imports1 = imports! {
"dog" => {
Expand Down Expand Up @@ -380,8 +387,9 @@ mod test {
#[test]
fn extending_conflict_overwrites() {
let store = Store::default();
let g1 = Global::new(&store, Val::I32(0));
let g2 = Global::new(&store, Val::I64(0));
let mut ctx = WasmerContext::new(&store, ());
let g1 = Global::new(&mut ctx, Value::I32(0));
let g2 = Global::new(&mut ctx, Value::I64(0));

let mut imports1 = imports! {
"dog" => {
Expand All @@ -397,19 +405,20 @@ mod test {

imports1.extend(&imports2);
let happy_dog_entry = imports1.get_export("dog", "happy").unwrap();

/*
assert!(
if let Export::Global(happy_dog_global) = happy_dog_entry.to_vm_extern() {
if let Exports::Global(happy_dog_global) = happy_dog_entry.to_vm_extern() {
happy_dog_global.from.ty().ty == Type::I64
} else {
false
}
);

*/
// now test it in reverse
let store = Store::default();
let g1 = Global::new(&store, Val::I32(0));
let g2 = Global::new(&store, Val::I64(0));
let mut ctx = WasmerContext::new(&store, ());
let g1 = Global::new(&mut ctx, Value::I32(0));
let g2 = Global::new(&mut ctx, Value::I64(0));

let imports1 = imports! {
"dog" => {
Expand All @@ -425,13 +434,14 @@ mod test {

imports2.extend(&imports1);
let happy_dog_entry = imports2.get_export("dog", "happy").unwrap();

/*
assert!(
if let Export::Global(happy_dog_global) = happy_dog_entry.to_vm_extern() {
if let Exports::Global(happy_dog_global) = happy_dog_entry.to_vm_extern() {
happy_dog_global.from.ty().ty == Type::I32
} else {
false
}
);
*/
}
}
Loading