Module: Mirah::JVM::Types::ArgumentConversion

Included in:
Intrinsic, JavaCallable
Defined in:
lib/mirah/jvm/types/methods.rb

Instance Method Summary collapse

Instance Method Details

#convert_args(compiler, values, types = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mirah/jvm/types/methods.rb', line 38

def convert_args(compiler, values, types=nil)
  # TODO boxing/unboxing
  types ||= argument_types
  needs_to_build_varargs_array = false
  
  if respond_to?(:varargs?) && varargs?
    non_varargs_types = types[0..-2]
    non_varargs_values = values.first non_varargs_types.size

    varargs_values = values.to_a.last(values.size - non_varargs_values.size)
    varargs_type = types.last

    unless varargs_values.length == 1 &&
      varargs_type.compatible?(compiler.inferred_type(varargs_values.first))
      needs_to_build_varargs_array = true
      values = non_varargs_values
    end
  end

  values_and_types = values.zip(types)
  
  values_and_types.each do |value, type|
    compiler.visit(value, true)
    in_type = compiler.inferred_type(value)
    if in_type.primitive? && type != in_type
      in_type.compile_widen(compiler.method, type)
    end
  end

  if needs_to_build_varargs_array
    compiler.visitVarargsArray(varargs_type, varargs_values)
  end
end