Class: Inliner::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, code = "", options = {}) ⇒ Builder

Returns a new instance of Builder.



126
127
128
129
130
131
132
133
134
# File 'lib/ffi-inliner/inliner.rb', line 126

def initialize(mod, code = "", options = {})
  make_pointer_types
  @mod = mod
  @code = code
  @sig = [parse_signature(@code)] unless @code.empty?
  options = { :use_compiler => Compilers::GCC }.merge(options)
  @compiler = options[:use_compiler]
  @libraries = []
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



125
126
127
# File 'lib/ffi-inliner/inliner.rb', line 125

def code
  @code
end

#compilerObject (readonly)

Returns the value of attribute compiler.



125
126
127
# File 'lib/ffi-inliner/inliner.rb', line 125

def compiler
  @compiler
end

Instance Method Details

#buildObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ffi-inliner/inliner.rb', line 169

def build
  @fm = FilenameManager.new(@mod, @code)
  @compiler = @compiler.check_and_create(@fm, @libraries)
  unless @fm.cached?
    write_files(@code, @sig)
    @compiler.compile
    @mod.instance_eval generate_ffi(@sig)
  else
    @mod.instance_eval(File.read(@fm.rb_fn))
  end
end

#c(code) ⇒ Object



148
149
150
151
# File 'lib/ffi-inliner/inliner.rb', line 148

def c(code)
  (@sig ||= []) << parse_signature(code)
  @code << (@compiler == Compilers::GPlusPlus ? "extern \"C\" {\n#{code}\n}" : code )
end

#c_raw(code) ⇒ Object



153
154
155
# File 'lib/ffi-inliner/inliner.rb', line 153

def c_raw(code)
  @code << code
end

#include(fn, options = {}) ⇒ Object



140
141
142
# File 'lib/ffi-inliner/inliner.rb', line 140

def include(fn, options = {})
  options[:quoted] ? @code << "#include \"#{fn}\"\n" : @code << "#include <#{fn}>\n"
end

#library(*libraries) ⇒ Object



144
145
146
# File 'lib/ffi-inliner/inliner.rb', line 144

def library(*libraries)
  (@libraries ||= []).concat(libraries)
end

#map(type_map) ⇒ Object



136
137
138
# File 'lib/ffi-inliner/inliner.rb', line 136

def map(type_map)
  @types.merge!(type_map)
end

#struct(ffi_struct) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/ffi-inliner/inliner.rb', line 161

def struct(ffi_struct)
  @code << "typedef struct {"
  ffi_struct.layout.fields.each do |field|
    @code << "#{field} #{field.name};\n"
  end
  @code << "} #{ffi_struct.class.name}"
end

#use_compiler(compiler) ⇒ Object



157
158
159
# File 'lib/ffi-inliner/inliner.rb', line 157

def use_compiler(compiler)
  @compiler = compiler
end