mthom / scryer-prolog

A modern Prolog implementation written mostly in Rust.
BSD 3-Clause "New" or "Revised" License
1.93k stars 116 forks source link

Panicked when calling function from ffi that use struct with float #2416

Closed aerphanas closed 3 days ago

aerphanas commented 3 weeks ago

scryer-prolog version: 0.9.4 OS: Linux Debian 12 bookworm

I am trying to use raylib with scryer-prolog. When I use DrawRectangleRect, it panicked. Then I did some experiments and wrote a simple C code to test it.

typedef struct Rectangle {
    float x;                // Rectangle top-left corner position x
    float y;                // Rectangle top-left corner position y
    float width;            // Rectangle width
    float height;           // Rectangle height
} Rectangle;

Rectangle test_rect(void)
{
  return (Rectangle){ 0.0f, 0.0f, 50.0f, 50.0f };
}

float test_float(void)
{
  return 3.0f;
}
$ cc ret.c -o ret.so -shared
$ scryer-prolog 
?- use_module(library(ffi)).
   true.
?- foreign_struct(rectangle, [f32, f32, f32, f32]).
   true.
?- use_foreign_module("./ret.so", ['test_float'([], f32), 'test_rect'([], rectangle)]).
   true.
?- ffi:'test_float'(X).
   X = 3.0.
?- ffi:'test_rect'(X).
thread 'main' panicked at src/ffi.rs:452:25:
internal error: entered unreachable code
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
aarroyoc commented 3 weeks ago

True, the read_struct function is missing implementation for floats and doubles. I'll prepare a fix

aerphanas commented 3 days ago

i tested it today, now it works, thanks