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 587
def initialize(info, method_name, full_method_name)
  @info = info
  @method_name = method_name
  @full_method_name = full_method_name
  @prepared = false
end

Public Instance Methods

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

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

  arguments, block = build(receiver, arguments, block)
  unless valid?(arguments)
    if @on_invalid == :fallback
      return @value_on_invalid
    else
      raise ArgumentError, invalid_error_message(arguments)
    end
  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

Private Instance Methods

build(receiver, arguments, block) click to toggle source
# File lib/gobject-introspection/loader.rb, line 670
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
  if 0 < n_missing_arguments
    @in_arg_nil_indexes.each_with_index do |nil_index, i|
      next if i <= n_missing_arguments
      arguments.insert(nil_index, nil)
    end
  end

  return arguments, block
end
ensure_prepared() click to toggle source
# File lib/gobject-introspection/loader.rb, line 627
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_nil_indexes = []
  @in_args.each_with_index do |arg, i|
    @in_arg_types << arg.type
    @in_arg_nil_indexes << i if arg.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 697
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
prepare_on_invalid() click to toggle source
# File lib/gobject-introspection/loader.rb, line 657
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
valid?(arguments) click to toggle source
# File lib/gobject-introspection/loader.rb, line 687
def valid?(arguments)
  return false unless @valid_n_args_range.cover?(arguments.size)
  if @on_invalid == :fallback
    arguments.zip(@in_arg_types) do |argument, type|
      return false unless type.match?(argument)
    end
  end
  true
end