2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
2017-10-23 03:01:00 +00:00
|
|
|
// ignore-wasm32-bare no libc to test ffi with
|
2019-03-25 21:28:03 +00:00
|
|
|
#![feature(c_variadic)]
|
|
|
|
|
|
|
|
use std::ffi::VaList;
|
2017-10-23 03:01:00 +00:00
|
|
|
|
2016-11-24 00:09:51 +00:00
|
|
|
#[link(name = "rust_test_helpers", kind = "static")]
|
2020-09-01 21:12:52 +00:00
|
|
|
extern "C" {
|
2016-06-02 14:18:11 +00:00
|
|
|
fn rust_interesting_average(_: u64, ...) -> f64;
|
2019-03-25 21:28:03 +00:00
|
|
|
|
|
|
|
// FIXME: we need to disable this lint for `VaList`,
|
|
|
|
// since it contains a `MaybeUninit<i32>` on the asmjs target,
|
|
|
|
// and this type isn't FFI-safe. This is OK for now,
|
|
|
|
// since the type is layout-compatible with `i32`.
|
|
|
|
#[cfg_attr(target_arch = "asmjs", allow(improper_ctypes))]
|
|
|
|
fn rust_valist_interesting_average(_: u64, _: VaList) -> f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe extern "C" fn test_valist_forward(n: u64, mut ap: ...) -> f64 {
|
|
|
|
rust_valist_interesting_average(n, ap.as_va_list())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe extern "C" fn test_va_copy(_: u64, mut ap: ...) {
|
|
|
|
let mut ap2 = ap.clone();
|
|
|
|
assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 30);
|
|
|
|
|
|
|
|
// Advance one pair in the copy before checking
|
|
|
|
let mut ap2 = ap.clone();
|
|
|
|
let _ = ap2.arg::<u64>();
|
|
|
|
let _ = ap2.arg::<f64>();
|
|
|
|
assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 50);
|
|
|
|
|
|
|
|
// Advance one pair in the original
|
|
|
|
let _ = ap.arg::<u64>();
|
|
|
|
let _ = ap.arg::<f64>();
|
|
|
|
|
|
|
|
let mut ap2 = ap.clone();
|
|
|
|
assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 50);
|
|
|
|
|
|
|
|
let mut ap2 = ap.clone();
|
|
|
|
let _ = ap2.arg::<u64>();
|
|
|
|
let _ = ap2.arg::<f64>();
|
|
|
|
assert_eq!(rust_valist_interesting_average(2, ap2.as_va_list()) as i64, 70);
|
2013-11-04 21:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2016-05-27 00:05:52 +00:00
|
|
|
// Call without variadic arguments
|
2013-11-04 21:34:07 +00:00
|
|
|
unsafe {
|
2016-05-27 00:05:52 +00:00
|
|
|
assert!(rust_interesting_average(0).is_nan());
|
|
|
|
}
|
2013-11-04 21:34:07 +00:00
|
|
|
|
2016-05-27 00:05:52 +00:00
|
|
|
// Call with direct arguments
|
|
|
|
unsafe {
|
2016-06-02 14:18:11 +00:00
|
|
|
assert_eq!(rust_interesting_average(1, 10i64, 10.0f64) as i64, 20);
|
2016-05-27 00:05:52 +00:00
|
|
|
}
|
2013-11-04 21:34:07 +00:00
|
|
|
|
2016-05-27 00:05:52 +00:00
|
|
|
// Call with named arguments, variable number of them
|
2016-06-02 14:18:11 +00:00
|
|
|
let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
|
2016-05-27 00:05:52 +00:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(rust_interesting_average(2, x1, x2, x3, x4) as i64, 30);
|
|
|
|
}
|
2013-11-04 21:34:07 +00:00
|
|
|
|
2016-05-27 00:05:52 +00:00
|
|
|
// A function that takes a function pointer
|
2020-09-01 21:12:52 +00:00
|
|
|
unsafe fn call(fp: unsafe extern "C" fn(u64, ...) -> f64) {
|
2016-06-02 14:18:11 +00:00
|
|
|
let (x1, x2, x3, x4) = (10i64, 10.0f64, 20i64, 20.0f64);
|
2016-05-27 00:05:52 +00:00
|
|
|
assert_eq!(fp(2, x1, x2, x3, x4) as i64, 30);
|
|
|
|
}
|
2013-11-04 21:34:07 +00:00
|
|
|
|
2016-05-27 00:05:52 +00:00
|
|
|
unsafe {
|
|
|
|
call(rust_interesting_average);
|
2013-11-04 21:34:07 +00:00
|
|
|
|
2016-05-27 00:05:52 +00:00
|
|
|
// Make a function pointer, pass indirectly
|
2020-09-01 21:12:52 +00:00
|
|
|
let x: unsafe extern "C" fn(u64, ...) -> f64 = rust_interesting_average;
|
2013-11-04 21:34:07 +00:00
|
|
|
call(x);
|
|
|
|
}
|
2019-03-25 21:28:03 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(test_valist_forward(2, 10i64, 10f64, 20i64, 20f64) as i64, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
test_va_copy(4, 10i64, 10f64, 20i64, 20f64, 30i64, 30f64, 40i64, 40f64);
|
|
|
|
}
|
2013-11-04 21:34:07 +00:00
|
|
|
}
|