class GirFFI::Builders::CallbackArgumentBuilder

Convertor for arguments for ruby callbacks. Used when building the argument mapper for callbacks.

Public Instance Methods

allow_none?() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 22
def allow_none?
  false
end
block_argument?() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 18
def block_argument?
  false
end
call_argument_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 39
def call_argument_name
  pre_converted_name if [:in, :inout].include?(direction) && !array_arg
end
capture_variable_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 43
def capture_variable_name
  result_name if [:out, :inout].include?(direction) && !array_arg
end
method_argument_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 14
def method_argument_name
  @method_argument_name ||= name || new_variable
end
out_parameter_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 30
def out_parameter_name
  @out_parameter_name ||=
    if direction == :inout
      new_variable
    else
      pre_converted_name
    end
end
post_conversion() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 60
def post_conversion
  case direction
  when :out, :inout
    [value_to_pointer_conversion]
  when :error
    [
      "rescue => #{result_name}",
      value_to_pointer_conversion,
      "end"
    ]
  else
    []
  end
end
pre_conversion() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 47
def pre_conversion
  case direction
  when :in
    [ingoing_pre_conversion]
  when :out
    [out_parameter_preparation]
  when :inout
    [out_parameter_preparation, ingoing_pre_conversion]
  when :error
    [out_parameter_preparation, "begin"]
  end
end
pre_converted_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 26
def pre_converted_name
  @pre_converted_name ||= new_variable
end

Private Instance Methods

allocated_by_us?() click to toggle source

Check if an out argument needs to be allocated by us, the callee. Since caller_allocates is false by default, we must also check that the type is a pointer. For example, an out parameter of type gint8* will always be allocate by the caller.

# File lib/gir_ffi/builders/callback_argument_builder.rb, line 161
def allocated_by_us?
  direction == :out &&
    !@arginfo.caller_allocates? &&
    type_info.pointer? &&
    ![:object, :zero_terminated].include?(specialized_type_tag)
end
ingoing_pre_conversion() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 122
def ingoing_pre_conversion
  "#{pre_converted_name} = #{pre_convertor.conversion}"
end
length_argument_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 168
def length_argument_name
  length_arg&.pre_converted_name
end
needs_c_to_ruby_conversion?() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 118
def needs_c_to_ruby_conversion?
  type_info.needs_c_to_ruby_conversion_for_callbacks?
end
out_parameter_preparation() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 142
def out_parameter_preparation
  value = if allocated_by_us?
            ffi_type = TypeMap.type_specification_to_ffi_type type_spec.last
            "FFI::MemoryPointer.new(#{ffi_type.inspect})" \
              ".tap { |ptr| #{method_argument_name}.put_pointer 0, ptr }"
          else
            method_argument_name
          end
  "#{out_parameter_name} = #{value}"
end
pointer_to_value_conversion() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 97
def pointer_to_value_conversion
  pointer_value_convertor.pointer_to_value(out_parameter_name)
end
pointer_value_convertor() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 89
def pointer_value_convertor
  @pointer_value_convertor ||= if allocated_by_us?
                                 PointerValueConvertor.new(type_spec[1])
                               else
                                 PointerValueConvertor.new(type_spec)
                               end
end
post_convertor() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 126
def post_convertor
  @post_convertor ||= if type_info.needs_ruby_to_c_conversion_for_callbacks?
                        RubyToCConvertor.new(type_info, post_convertor_argument)
                      else
                        NullConvertor.new(post_convertor_argument)
                      end
end
post_convertor_argument() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 134
def post_convertor_argument
  if array_arg
    "#{array_arg.capture_variable_name}.length"
  else
    result_name
  end
end
pre_convertor() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 106
def pre_convertor
  @pre_convertor ||= if user_data?
                       ClosureConvertor.new(pre_convertor_argument)
                     elsif needs_c_to_ruby_conversion?
                       CToRubyConvertor.new(type_info,
                                            pre_convertor_argument,
                                            length_argument_name)
                     else
                       NullConvertor.new(pre_convertor_argument)
                     end
end
pre_convertor_argument() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 81
def pre_convertor_argument
  if direction == :inout
    pointer_to_value_conversion
  else
    method_argument_name
  end
end
result_name() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 77
def result_name
  @result_name ||= new_variable
end
type_spec() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 153
def type_spec
  type_info.tag_or_class
end
value_to_pointer_conversion() click to toggle source
# File lib/gir_ffi/builders/callback_argument_builder.rb, line 101
def value_to_pointer_conversion
  pointer_value_convertor.value_to_pointer(out_parameter_name,
                                           post_convertor.conversion)
end