Class: TCC::State

Inherits:
Object
  • Object
show all
Defined in:
lib/tcc.rb

Overview

High-level, OO-style TCCState. Example:

state = TCC::State.new
state.compile_string(code)
state.run
state.destroy

Equivalent plain libtcc code:

state = TCC::new
TCC.compile_string(state, code)
TCC.run(state)
TCC.delete(state)

Constant Summary collapse

DEFAULT_ERROR_CALLBACK =
FFI::Function.new(:void, [:pointer, :string], proc {|_, msg| raise TCC::Error, msg})

Instance Method Summary collapse

Constructor Details

#initialize(output_type = TCC::OUTPUT_MEMORY) ⇒ State

Returns a new instance of State.



37
38
39
40
41
42
# File 'lib/tcc.rb', line 37

def initialize(output_type = TCC::OUTPUT_MEMORY)
  @state = TCC.send :new
  set_output_type output_type
  set_error_func(nil, DEFAULT_ERROR_CALLBACK)
  ObjectSpace.define_finalizer(self, proc { TCC.delete(@state) unless @state.nil? })
end

Instance Method Details

#compile(code) ⇒ Object



44
45
46
# File 'lib/tcc.rb', line 44

def compile(code)
  compile_string(code)
end

#destroyObject



48
49
50
51
# File 'lib/tcc.rb', line 48

def destroy
  TCC.delete @state
  @state = nil
end

#get_function(name, param_types = [], return_type = :void) ⇒ Object



53
54
55
# File 'lib/tcc.rb', line 53

def get_function(name, param_types = [], return_type = :void)
  FFI::Function.new(return_type, param_types, get_symbol(name))
end

#relocateObject



57
58
59
60
# File 'lib/tcc.rb', line 57

def relocate
  relocate_auto
  self
end