rust-diplomat / diplomat

Experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code
https://rust-diplomat.github.io/book/
Other
512 stars 48 forks source link

_intoFFI padding #656

Closed ambiguousname closed 1 month ago

ambiguousname commented 1 month ago

Should double check with LLVM output, but basically:

struct Foo {
  a : u8,
  b : u8,
  c : u32
}

Pads with 2 x i8s. But

struct Bar {
  b : u16,
  c : u32
}

Pads with 1 x i16. The padding is determined by the previous type, and then it all gets converted into i32s for WASM, regardless of how much sense that makes.

So, the fix should go in generate_fields, to grab what the previous field's align is. Then update the padding to be use something like (next_offset - curr_offset - field_layout.size()) / previous_align

Manishearth commented 1 month ago

The padding is determined by the previous type

Or the previous set of types, (u8, u16, u32) will have an i8 padding. It's unclear to me if this is a global decision or per-alignment-section. I'll write out a bunch of test structs to reverse engineer this.

Also the example was incorrect, Bar should have no u8s.

Manishearth commented 1 month ago

Or the previous set of types, (u8, u16, u32) will have an i8 padding. It's unclear to me if this is a global decision or per-alignment-section. I'll write out a bunch of test structs to reverse engineer this.

Nope, it pads with an i8 and then an i16. Previous type is right, the u16 needs to be padded too.