2 // ignore-wasm32-bare no libc to test ffi with
3 #![feature(c_variadic)]
7 #[link(name = "rust_test_helpers", kind = "static")]
9 fn rust_interesting_average(_: u64, ...) -> f64;
11 // FIXME: we need to disable this lint for `VaList`,
12 // since it contains a `MaybeUninit<i32>` on the asmjs target,
13 // and this type isn't FFI-safe. This is OK for now,
14 // since the type is layout-compatible with `i32`.
15 #[cfg_attr(target_arch = "asmjs", allow(improper_ctypes))]
16 fn rust_valist_interesting_average(_: u64, _: VaList) -> f64;
19 pub unsafe extern "C" fn test_valist_forward(n: u64, mut ap: ...) -> f64 {
20 rust_valist_interesting_average(n, ap.as_va_list())
23 pub unsafe extern "C" fn test_va_copy(_: u64, mut ap: ...) {
24 let mut ap2 = ap.clone();
25 assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 30);
27 // Advance one pair in the copy before checking
28 let mut ap2 = ap.clone();
29 let _ = ap2.arg::<u64>();
30 let _ = ap2.arg::<f64>();
31 assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 50);
33 // Advance one pair in the original
34 let _ = ap.arg::<u64>();
35 let _ = ap.arg::<f64>();
37 let mut ap2 = ap.clone();
38 assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 50);
40 let mut ap2 = ap.clone();
41 let _ = ap2.arg::<u64>();
42 let _ = ap2.arg::<f64>();
43 assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 70);
47 // Call without variadic arguments
49 assert!(rust_interesting_average(0).is_nan());
52 // Call with direct arguments
54 assert_eq!(rust_interesting_average(1, 10i64, 10.0f64) as i64, 20);
57 // Call with named arguments, variable number of them
58 let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
60 assert_eq!(rust_interesting_average(2, x1, x2, x3, x4) as i64, 30);
63 // A function that takes a function pointer
64 unsafe fn call(fp: unsafe extern "C" fn(u64, ...) -> f64) {
65 let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
66 assert_eq!(fp(2, x1, x2, x3, x4) as i64, 30);
70 call(rust_interesting_average);
72 // Make a function pointer, pass indirectly
73 let x: unsafe extern "C" fn(u64, ...) -> f64 = rust_interesting_average;
78 assert_eq!(test_valist_forward(2, 10i64, 10f64, 20i64, 20f64) as i64, 30);
82 test_va_copy(4, 10i64, 10f64, 20i64, 20f64, 30i64, 30f64, 40i64, 40f64);