Idris2 icon indicating copy to clipboard operation
Idris2 copied to clipboard

Nested Structs in FFI not read correctly

Open edwinb opened this issue 4 years ago • 2 comments

Issue by vfrinken Tuesday Mar 24, 2020 at 04:39 GMT Originally opened as https://github.com/edwinb/Idris2-boot/issues/238


If I have 2 structs in C

typedef struct{
    int * a;
    int b;
    int * c;
}ExtraStruct;

typedef struct{
    ExtraStruct extra;
    int x;
    int y;
} TestStruct;

TestStruct * getTestStruct(){
    TestStruct * ts = (TestStruct*)malloc(sizeof(TestStruct));
    ts -> x = 1234567890;
    ts -> y = -1;
    return ts;
}

and corresponding types in idris

ExtraStruct : Type
ExtraStruct = Struct "ExtraStruct" [("a", Int), ("b", Int), ("c", Int)]

TestStruct : Type
TestStruct = Struct "TestStruct" [("extra", ExtraStruct), ("x", Int),  ("y", Int)]

I'm not able to read the correct values from an instance of TestStruct using a show function like this

Show TestStruct where
  show ts =
      let x : Int = getField ts "x"
          y : Int = getField ts "y"
      in
      "x: " ++ (show x) ++ ", y: " ++ (show y)

Steps to Reproduce

And example is here https://github.com/vfrinken/nestedStructsIdris2.git

Expected Behavior

x: 1234567890, y: -1

Observed Behavior

x: 0, y: 1234567890

edwinb avatar May 20 '20 12:05 edwinb