class FFI::Exporter

Attributes

callbacks[R]
functions[R]
mod[RW]
structs[R]

Public Class Methods

new(mod) click to toggle source
# File lib/ffi-compiler/fake_ffi/ffi.rb, line 101
def initialize(mod)
  @mod = mod
  @functions = []
  @callbacks = {}
  @structs = []
end

Public Instance Methods

attach(mname, fname, result_type, param_types) click to toggle source
# 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) click to toggle source
# File lib/ffi-compiler/fake_ffi/ffi.rb, line 116
def callback(name, cb)
  @callbacks[name] = cb
end
dump(out_file) click to toggle source
# 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) click to toggle source
# File lib/ffi-compiler/fake_ffi/ffi.rb, line 112
def struct(name, fields)
  @structs << { name: name, fields: fields.dup }
end