class FFI::Exporter

Attributes

functions[R]
mod[R]

Public Class Methods

new(mod) click to toggle source
# File lib/ffi-compiler/fake_ffi/ffi.rb, line 61
def initialize(mod)
  @mod = mod
  @functions = []
  @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 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) click to toggle source
# 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) click to toggle source
# File lib/ffi-compiler/fake_ffi/ffi.rb, line 71
def struct(name, fields)
  @structs << { name: name, fields: fields.dup }
end