Ruby Fiddle中的嵌套结构

Ríš*_*ner 39 ruby dll shared-libraries fiddle

  • 是否有可能在Ruby的Fiddle lib中定义嵌套结构签名?
  • 或者在这一点上通过我不熟悉的任何其他最新图书馆?

这是一个绝望的问题,因为我搜索了文档和Fiddle的代码,发现没有关于嵌套结构的线索(尽管FFI库能够做到这一点,而Fiddle应该是FFI的包装器).

bli*_*iof 1

怎么样:

数据.c

#include <stdio.h>

struct A
{
    int a_id;
};

struct B
{
    int b_id;
    struct A a_nested;
};

void dump_b(struct B* b) {
    printf("B.b_id: %d\n", b->b_id);
    printf("B.a_nested.a_id: %d\n", b->a_nested.a_id);
}
Run Code Online (Sandbox Code Playgroud)

小提琴.rb

# frozen_string_literal: true

require 'fiddle'
require 'fiddle/import'

module Test
  extend Fiddle::Importer

  dlload './data.dylib'

  B = struct [
    "int id",
    "int a_id"
  ]

  extern 'void dump_b(struct B*)'
end

b = Test::B.malloc

b.id = 123
b.a_id = 13

puts b.id
puts b.a_id

Test.dump_b(b)
Run Code Online (Sandbox Code Playgroud)

或 ffi.rb

require 'ffi'

module Test
  extend FFI::Library

  ffi_lib 'data.dylib'

  class A < FFI::Struct
    layout :id,  :int
  end

  class B < FFI::Struct
    layout :id,  :int,
           :a_nested, A
  end

  attach_function :dump_b, [:pointer], :void
end

a = Test::A.new

a[:id] = 3000

b = Test::B.new

b[:id] = 1000
b[:a_nested] = a

puts b[:id]
puts b[:a_nested][:id]

Test.dump_b(b)
Run Code Online (Sandbox Code Playgroud)

结果

$ gcc -c data.c
$ gcc -dynamiclib data.o -o data.dylib
$ ruby fiddle.rb
123
13
B.b_id: 123
B.a_nested.a_id: 13
$ ruby ffi.rb
1000
3000
B.b_id: 1000
B.a_nested.a_id: 3000
Run Code Online (Sandbox Code Playgroud)