Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

dll - Nested structures in Ruby Fiddle

  • Is it somehow possible to define nested structure signatures in Ruby's Fiddle lib?
  • Or at this point via any other up-to-date library which I'm not familiar with?

It is kind of a desperate question since I've searched through the documentation and Fiddle's code and found no clue about nested structs (although the FFI library was able to do that apparently and Fiddle should be a wrapper for FFI).

question from:https://stackoverflow.com/questions/45005733/nested-structures-in-ruby-fiddle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about:

data.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
", b->b_id);
    printf("B.a_nested.a_id: %d
", b->a_nested.a_id);
}

fiddle.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)

or 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)

Result

$ 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...