Skip to content

Commit dbc1b4e

Browse files
author
Mark McCaskey
committed
Add tests for smaller sizes, change logic to truncate parameters
1 parent d23cebf commit dbc1b4e

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

lib/api/src/externals/function.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -597,24 +597,12 @@ mod inner {
597597

598598
#[inline]
599599
fn from_native(native: Self::Native) -> Self {
600-
native.try_into().expect(concat!(
601-
"out of range type conversion attempt (tried to convert `",
602-
stringify!($native_type),
603-
"` to `",
604-
stringify!($type),
605-
"`)",
606-
))
600+
native as Self
607601
}
608602

609603
#[inline]
610604
fn to_native(self) -> Self::Native {
611-
self.try_into().expect(concat!(
612-
"out of range type conversion attempt (tried to convert `",
613-
stringify!($type),
614-
"` to `",
615-
stringify!($native_type),
616-
"`)",
617-
))
605+
self as Self::Native
618606
}
619607
}
620608
)*

lib/api/tests/module.rs

+36
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
165165
(import "host" "host_func2" (func (param i32)))
166166
(import "host" "host_func3" (func (param i64)))
167167
(import "host" "host_func4" (func (param i32)))
168+
(import "host" "host_func5" (func (param i32)))
169+
(import "host" "host_func6" (func (param i32)))
170+
(import "host" "host_func7" (func (param i32)))
171+
(import "host" "host_func8" (func (param i32)))
168172
169173
(func (export "call_host_func1")
170174
(call 0 (i64.const -1)))
@@ -174,6 +178,14 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
174178
(call 2 (i64.const -1)))
175179
(func (export "call_host_func4")
176180
(call 3 (i32.const -1)))
181+
(func (export "call_host_func5")
182+
(call 4 (i32.const -1)))
183+
(func (export "call_host_func6")
184+
(call 5 (i32.const -1)))
185+
(func (export "call_host_func7")
186+
(call 6 (i32.const -1)))
187+
(func (export "call_host_func8")
188+
(call 7 (i32.const -1)))
177189
)"#;
178190
let module = Module::new(&store, wat)?;
179191
let imports = imports! {
@@ -194,6 +206,22 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
194206
println!("host_func4: Found number {}", p);
195207
assert_eq!(p, -1);
196208
}),
209+
"host_func5" => Function::new_native(&store, |p: i16| {
210+
println!("host_func5: Found number {}", p);
211+
assert_eq!(p, -1);
212+
}),
213+
"host_func6" => Function::new_native(&store, |p: u16| {
214+
println!("host_func6: Found number {}", p);
215+
assert_eq!(p, u16::max_value());
216+
}),
217+
"host_func7" => Function::new_native(&store, |p: i8| {
218+
println!("host_func7: Found number {}", p);
219+
assert_eq!(p, -1);
220+
}),
221+
"host_func8" => Function::new_native(&store, |p: u8| {
222+
println!("host_func8: Found number {}", p);
223+
assert_eq!(p, u8::max_value());
224+
}),
197225
}
198226
};
199227
let instance = Instance::new(&module, &imports)?;
@@ -202,11 +230,19 @@ fn calling_host_functions_with_negative_values_works() -> Result<()> {
202230
let f2: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func2")?;
203231
let f3: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func3")?;
204232
let f4: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func4")?;
233+
let f5: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func5")?;
234+
let f6: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func6")?;
235+
let f7: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func7")?;
236+
let f8: NativeFunc<(), ()> = instance.exports.get_native_function("call_host_func8")?;
205237

206238
f1.call()?;
207239
f2.call()?;
208240
f3.call()?;
209241
f4.call()?;
242+
f5.call()?;
243+
f6.call()?;
244+
f7.call()?;
245+
f8.call()?;
210246

211247
Ok(())
212248
}

0 commit comments

Comments
 (0)