Class: FFI::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-compiler/fake_ffi/ffi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Exporter

Returns a new instance of Exporter.



61
62
63
64
65
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 61

def initialize(mod)
  @mod = mod
  @functions = []
  @structs = []
end

Instance Attribute Details

#functionsObject (readonly)

Returns the value of attribute functions.



59
60
61
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 59

def functions
  @functions
end

#modObject (readonly)

Returns the value of attribute mod.



59
60
61
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 59

def mod
  @mod
end

Instance Method Details

#attach(mname, fname, result_type, param_types) ⇒ Object



67
68
69
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 67

def attach(mname, fname, result_type, param_types)
  @functions << { mname: mname, fname: fname, result_type: result_type, params: param_types.dup }
end

#dump(out_file) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 75

def dump(out_file)
  File.open(out_file, 'w') do |f|
    guard = File.basename(out_file).upcase.gsub('.', '_').gsub('/', '_')
    f.puts <<-HEADER
#ifndef #{guard}
#define #{guard} 1

#ifndef RBFFI_EXPORT
# ifdef __cplusplus
#  define RBFFI_EXPORT extern "C"
# else
#  define RBFFI_EXPORT
# endif
#endif

    HEADER
    
    @structs.each do |s|
      f.puts "struct #{s[:name].gsub('::', '_')} {"
      s[:fields].each do |field|
        f.puts "#{' ' * 4}#{field[:type].name} #{field[:name].to_s};"
      end
      f.puts '};'
      f.puts
    end
    @functions.each do |fn|
      param_string = fn[:params].empty? ? 'void' : fn[:params].map(&:name).join(', ')
      f.puts "RBFFI_EXPORT #{fn[:result_type].name} #{fn[:fname]}(#{param_string});"
    end
    f.puts <<-EPILOG

#endif /* #{guard} */
    EPILOG
  end
end

#struct(name, fields) ⇒ Object



71
72
73
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 71

def struct(name, fields)
  @structs << { name: name, fields: fields.dup }
end