class GObjectIntrospection::Loader::Invoker

Public Class Methods

new(info, method_name, full_method_name) click to toggle source
# File lib/gobject-introspection/loader.rb, line 680
def initialize(info, method_name, full_method_name)
  @info = info
  @method_name = method_name
  @full_method_name = full_method_name
  @prepared = false
  ensure_prepared if defined?(Ractor)
end

Public Instance Methods

invoke(receiver, arguments, block, abort_tag=nil) click to toggle source
# File lib/gobject-introspection/loader.rb, line 688
def invoke(receiver, arguments, block, abort_tag=nil)
  ensure_prepared

  if receiver and @function_info_p
    arguments.unshift(receiver)
  end

  arguments, block = build(receiver, arguments, block)
  if wrong_number_of_arguments?(arguments)
    if abort_tag
      throw(abort_tag)
    else
      raise ArgumentError, invalid_error_message(arguments)
    end
  end
  unless normalize_arguments!(arguments, abort_tag)
    return @value_on_invalid
  end

  if block.nil? and @require_callback_p
    receiver.to_enum(@method_name, *arguments)
  else
    if @function_info_p
      return_value = @info.invoke(arguments, &block)
    else
      return_value = @info.invoke(receiver, arguments, &block)
    end
    if @have_return_value_p
      return_value
    else
      receiver
    end
  end
end
signature() click to toggle source
# File lib/gobject-introspection/loader.rb, line 723
def signature
  ensure_prepared
  argument_signatures = @in_args.collect(&:signature)
  "(" + argument_signatures.join(", ") + ")"
end

Private Instance Methods

build(receiver, arguments, block) click to toggle source
# File lib/gobject-introspection/loader.rb, line 776
def build(receiver, arguments, block)
  if block and @last_in_arg_is_gclosure
    arguments << block
    block = nil
  end

  n_missing_arguments = @n_in_args - arguments.size
  n_in_arg_nil_indexes = @in_arg_nil_indexes.size
  if 0 < n_missing_arguments and n_missing_arguments < n_in_arg_nil_indexes
    @in_arg_nil_indexes[-n_missing_arguments..-1].each do |nil_index|
      arguments.insert(nil_index, nil)
    end
  end

  return arguments, block
end
ensure_prepared() click to toggle source
# File lib/gobject-introspection/loader.rb, line 730
def ensure_prepared
  return if @prepared

  @in_args = @info.in_args
  @n_in_args = @in_args.size
  @n_required_in_args = @info.n_required_in_args
  @last_in_arg = @in_args.last
  if @last_in_arg
    @last_in_arg_is_gclosure = @last_in_arg.gclosure?
  else
    @last_in_arg_is_gclosure = false
  end
  @valid_n_args_range = (@n_required_in_args..@n_in_args)

  @in_arg_types = []
  @in_arg_nils = []
  @in_arg_nil_indexes = []
  @in_args.each_with_index do |arg, i|
    @in_arg_types << arg.type
    may_be_null = arg.may_be_null?
    @in_arg_nils << may_be_null
    @in_arg_nil_indexes << i if may_be_null
  end

  @function_info_p = (@info.class == FunctionInfo)
  @have_return_value_p = @info.have_return_value?
  @require_callback_p = @info.require_callback?

  prepare_on_invalid

  @prepared = true
end
invalid_error_message(arguments) click to toggle source
# File lib/gobject-introspection/loader.rb, line 817
def invalid_error_message(arguments)
  detail = "#{arguments.size} for "
  if @n_in_args == @n_required_in_args
    detail << "#{@n_in_args}"
  else
    detail << "#{@n_required_in_args}..#{@n_in_args}"
  end
  "#{@full_method_name}: wrong number of arguments (#{detail})"
end
normalize_arguments!(arguments, abort_tag) click to toggle source
# File lib/gobject-introspection/loader.rb, line 797
def normalize_arguments!(arguments, abort_tag)
  arguments.size.times do |i|
    argument = arguments[i]
    type = @in_arg_types[i]
    converted_argument = type.try_convert(argument)
    if converted_argument.nil?
      next if argument.nil? and @in_arg_nils[i]
      if abort_tag
        throw(abort_tag)
      elsif @on_invalid == :fallback
        return false
      else
        next
      end
    end
    arguments[i] = converted_argument
  end
  true
end
prepare_on_invalid() click to toggle source
# File lib/gobject-introspection/loader.rb, line 763
def prepare_on_invalid
  case @method_name
  when "=="
    @value_on_invalid = false
    @on_invalid = :fallback
  when "!="
    @value_on_invalid = true
    @on_invalid = :fallback
  else
    @on_invalid = :raise
  end
end
wrong_number_of_arguments?(arguments) click to toggle source
# File lib/gobject-introspection/loader.rb, line 793
def wrong_number_of_arguments?(arguments)
  not @valid_n_args_range.cover?(arguments.size)
end