Skip to content

Comments

Expose properties for types#14

Open
davispuh wants to merge 1 commit intoMVV90:mainfrom
davispuh:props
Open

Expose properties for types#14
davispuh wants to merge 1 commit intoMVV90:mainfrom
davispuh:props

Conversation

@davispuh
Copy link

@davispuh davispuh commented Feb 2, 2026

Expose properties for types. They're not used by ffi_generator but other projects can use this library.
For example, I created CStructParser which uses ffi_generator to convert C structs into BinData records.

This allows to do this:

require 'cstruct_parser'
require 'bindata'

sourceCode = %{
  #include <stdint.h>

  #define EI_NIDENT (16)

  typedef uint64_t Elf64_Addr;
  typedef uint16_t Elf64_Half;
  typedef uint32_t Elf64_Word;

  typedef struct
  {
    unsigned char e_ident[EI_NIDENT];
    Elf64_Half e_type;
    Elf64_Half e_machine;
    Elf64_Word e_version;
    Elf64_Addr e_entry;
  } Elf64_Ehdr;
}

records = CStructParser.to_bindata(sourceCode)
# class Elf64Ehdr < BinData::Record
#  endian :little
#  array :e_ident, type: :uint8, initial_length: 16
#  uint16 :e_type
#  uint16 :e_machine
#  uint32 :e_version
#  uint64 :e_entry
#end
eval(records)
Elf64Ehdr.read(File.open('/bin/ruby'))
# => {e_ident: [127, 69, 76, 70, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], e_type: 3, e_machine: 62, e_version: 1, e_entry: 4272}

They're not used by `ffi_generator` but other projects can use this library.
Copilot AI review requested due to automatic review settings February 2, 2026 21:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR exposes internal properties of type classes to enable external projects to introspect and use ffi_generator's type information. The changes support use cases like CStructParser, which converts C structs into BinData records by accessing these previously private properties.

Changes:

  • Added attr_reader declarations to expose internal properties across multiple type classes
  • Added struct? and union? helper methods to distinguish between struct and union types

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
lib/ffi_generator/generators/from_c_generator/struct_or_union.rb Added struct? and union? methods for type identification
lib/ffi_generator/generators/from_c_generator/primitive_type.rb Exposed clang_type property via attr_reader
lib/ffi_generator/generators/from_c_generator/by_value_type.rb Exposed inner_type property via attr_reader
lib/ffi_generator/generators/from_c_generator/array_type.rb Exposed element_type and constant_size properties via attr_reader

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant