Skip to content

Commit

Permalink
Add tests for smaller sizes, change logic to truncate parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Sep 10, 2020
1 parent d23cebf commit dbc1b4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
16 changes: 2 additions & 14 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,24 +597,12 @@ mod inner {

#[inline]
fn from_native(native: Self::Native) -> Self {
native.try_into().expect(concat!(
"out of range type conversion attempt (tried to convert `",
stringify!($native_type),
"` to `",
stringify!($type),
"`)",
))
native as Self
}

#[inline]
fn to_native(self) -> Self::Native {
self.try_into().expect(concat!(
"out of range type conversion attempt (tried to convert `",
stringify!($type),
"` to `",
stringify!($native_type),
"`)",
))
self as Self::Native
}
}
)*
Expand Down
36 changes: 36 additions & 0 deletions lib/api/tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
(import "host" "host_func2" (func (param i32)))
(import "host" "host_func3" (func (param i64)))
(import "host" "host_func4" (func (param i32)))
(import "host" "host_func5" (func (param i32)))
(import "host" "host_func6" (func (param i32)))
(import "host" "host_func7" (func (param i32)))
(import "host" "host_func8" (func (param i32)))
(func (export "call_host_func1")
(call 0 (i64.const -1)))
Expand All @@ -174,6 +178,14 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
(call 2 (i64.const -1)))
(func (export "call_host_func4")
(call 3 (i32.const -1)))
(func (export "call_host_func5")
(call 4 (i32.const -1)))
(func (export "call_host_func6")
(call 5 (i32.const -1)))
(func (export "call_host_func7")
(call 6 (i32.const -1)))
(func (export "call_host_func8")
(call 7 (i32.const -1)))
)"#;
let module = Module::new(&store, wat)?;
let imports = imports! {
Expand All @@ -194,6 +206,22 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
println!("host_func4: Found number {}", p);
assert_eq!(p, -1);
}),
"host_func5" => Function::new_native(&store, |p: i16| {
println!("host_func5: Found number {}", p);
assert_eq!(p, -1);
}),
"host_func6" => Function::new_native(&store, |p: u16| {
println!("host_func6: Found number {}", p);
assert_eq!(p, u16::max_value());
}),
"host_func7" => Function::new_native(&store, |p: i8| {
println!("host_func7: Found number {}", p);
assert_eq!(p, -1);
}),
"host_func8" => Function::new_native(&store, |p: u8| {
println!("host_func8: Found number {}", p);
assert_eq!(p, u8::max_value());
}),
}
};
let instance = Instance::new(&module, &imports)?;
Expand All @@ -202,11 +230,19 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
let f2: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func2")?;
let f3: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func3")?;
let f4: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func4")?;
let f5: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func5")?;
let f6: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func6")?;
let f7: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func7")?;
let f8: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func8")?;

f1.call()?;
f2.call()?;
f3.call()?;
f4.call()?;
f5.call()?;
f6.call()?;
f7.call()?;
f8.call()?;

Ok(())
}

0 comments on commit dbc1b4e

Please sign in to comment.