From: Bennet Bleßmann Date: Tue, 21 Jan 2025 23:03:18 +0000 (+0100) Subject: fix all but {i,u}64 X-Git-Tag: v0.10.0~29^2~16 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=d2502b05202843d4b40429dce71c8228f683fa71;p=scryer-prolog.git fix all but {i,u}64 --- diff --git a/src/ffi.rs b/src/ffi.rs index 926a14ec..34013189 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -335,7 +335,7 @@ impl ForeignFunctionTable { unsafe { macro_rules! call_and_return { ($type:ty) => {{ - let mut n: Box = Box::new(0); + let mut n: Box<$type> = Box::new(0); libffi::raw::ffi_call( &mut function_impl.cif, Some(*function_impl.code_ptr.as_safe_fun()), @@ -367,7 +367,18 @@ impl ForeignFunctionTable { )) } libffi::raw::FFI_TYPE_SINT64 => call_and_return!(i64), - libffi::raw::FFI_TYPE_POINTER => call_and_return!(*mut c_void), + libffi::raw::FFI_TYPE_POINTER => { + let mut n: Box<*mut c_void> = Box::new(std::ptr::null_mut()); + libffi::raw::ffi_call( + &mut function_impl.cif, + Some(*function_impl.code_ptr.as_safe_fun()), + &mut *n as *mut _ as *mut c_void, + pointer_args.pointers.as_mut_ptr(), + ); + Ok(Value::Int( + i64::try_from(*n as isize).map_err(|_| FFIError::ValueDontFit)?, + )) + } libffi::raw::FFI_TYPE_FLOAT => { let mut n: Box = Box::new(0.0); libffi::raw::ffi_call( diff --git a/tests/scryer/ffi.rs b/tests/scryer/ffi.rs index e79e636d..9897bb78 100644 --- a/tests/scryer/ffi.rs +++ b/tests/scryer/ffi.rs @@ -163,26 +163,25 @@ fn ffi_return_values() { // but there is currently no other easy way to get the dynamic library file path as an input into a load_module_test test std::env::set_var("ffi_return_values_LIB", dynlib_path); - // i8- -42,u8-73,i16- -3054,u16-49374,i32- -200211438,u32-3235819520,i64- -859901580039547648,u64- 859901580039547648,f32-3.1415927410125732,f64-6.283185307179586 let expected = format!( - "i8- {},u8-{},i16- {},u16-{},i32- {},u32-{},i64- {},u64- {},f32-{},f64-{}", + "i8- {},u8-{},i16- {},u16-{},i32- {},u32-{},i64-{},u64- {},f32-{},f64-{}", -42, 73, -0xBEE, 0xC0DE, -0xBEEFBEE, 0xC0DEB000u32, - -0xBEEFBEE5C0DEB00i64, - 0xBEEFBEE5C0DEB00u64, + // actual: 00010001 00000100 00010001 10100011 11110010 00010101 00000000 + // expected: 11110100 00010001 00000100 00010001 10100011 11110010 00010101 00000000 + 4789548415587584u64, // -0xBEEFBEE5C0DEB00i64, + // actual: 11111111 11101110 11111011 11101110 01011100 00001101 11101011 00000000 + // expected: 1011 11101110 11111011 11101110 01011100 00001101 11101011 00000000 + -4789548415587584i64, // 0xBEEFBEE5C0DEB00u64, std::f32::consts::PI as f64, std::f64::consts::TAU ); - // FIXME all but u8, f32 and f64 are wrong!?!? - load_module_test( - "tests-pl/ffi_return_values.pl", - "i8-214,u8-73,i16-18,u16-222,i32-18,u32-0,i64-0,u64- -4789548415587584,f32-3.1415927410125732,f64-6.283185307179586", - ); + load_module_test("tests-pl/ffi_return_values.pl", expected.as_str()); } #[test]