]> Repositorios git - scryer-prolog.git/commitdiff
fix all but {i,u}64
authorBennet Bleßmann <[email protected]>
Tue, 21 Jan 2025 23:03:18 +0000 (00:03 +0100)
committerBennet Bleßmann <[email protected]>
Fri, 1 Aug 2025 16:37:35 +0000 (18:37 +0200)
src/ffi.rs
tests/scryer/ffi.rs

index 926a14ec471d696c5d256c9f9ab9828fa5f7617d..340131894e3016e20d32f8e6f9d6a1a26161c4fb 100644 (file)
@@ -335,7 +335,7 @@ impl ForeignFunctionTable {
         unsafe {
             macro_rules! call_and_return {
                 ($type:ty) => {{
-                    let mut n: Box<u8> = 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<f32> = Box::new(0.0);
                     libffi::raw::ffi_call(
index e79e636d6e59c230ce48d2fc87d5417d48483c11..9897bb7828f9c28b0f7f11321b46c1681a66b511 100644 (file)
@@ -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]