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.



101
102
103
104
105
106
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 101

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

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



99
100
101
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 99

def callbacks
  @callbacks
end

#functionsObject (readonly)

Returns the value of attribute functions.



99
100
101
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 99

def functions
  @functions
end

#modObject

Returns the value of attribute mod.



98
99
100
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 98

def mod
  @mod
end

#structsObject (readonly)

Returns the value of attribute structs.



99
100
101
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 99

def structs
  @structs
end

Instance Method Details

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



108
109
110
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 108

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

#callback(name, cb) ⇒ Object



116
117
118
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 116

def callback(name, cb)
  @callbacks[name] = cb
end

#dump(out_file) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 120

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

    @callbacks.each do |name, cb|
      f.puts "typedef #{cb.name(name)};"
    end
    @structs.each do |s|
      f.puts "struct #{s[:name].gsub('::', '_')} {"
      s[:fields].each do |field|
        if field[:type].is_a?(CallbackInfo)
          type = field[:type].name(field[:name].to_s)
        else
          type = "#{field[:type].name} #{field[:name].to_s}"
        end
        f.puts "#{' ' * 4}#{type};"
      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



112
113
114
# File 'lib/ffi-compiler/fake_ffi/ffi.rb', line 112

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