[graalvm-users] Strange error

Rodrigo Botafogo rodrigo.a.botafogo at gmail.com
Mon Jul 16 11:49:15 PDT 2018


Hello,

I've being struggling with this issue for a couple of days, trying to get
to a reproducible error.  I must be blind to something really obvious, but
I can't find it.  I tried to reduce the code as much as possible and here
it is:

module R


#----------------------------------------------------------------------------------------
  #

#----------------------------------------------------------------------------------------

  def self.eval(string)
    Polyglot.eval("R", string)
  end


#----------------------------------------------------------------------------------------
  #

#----------------------------------------------------------------------------------------

  def self.parse_arg(arg)
    # if this is an R object, leave it alone
    if (Truffle::Interop.foreign?(arg) == true)
      return arg
      # convert integer to double, this is how R understands a number.
    elsif (arg.is_a? Integer)
      arg.to_f
    end
  end


#----------------------------------------------------------------------------------------
  # Parse all arguments into an R list of arguments

#----------------------------------------------------------------------------------------

  def self.parse2list(*args)
    params = Polyglot.eval("R", "list()")
    args.each_with_index do |arg, i|
      params = R.eval("`[[<-`").call(params, i+1, parse_arg(arg))
    end
    params
  end


#----------------------------------------------------------------------------------------
  # @param function [R function (Interop)] R function to execute
  # @args [Argument list] arguments for the function

#----------------------------------------------------------------------------------------

  def self.exec_function(function, *args)
    pl = R.parse2list(*args)
    # calls the function in R with the parameter list
    R.eval("do.call").call(function, pl)
  end

end

#========
# First example: the return value is an array
#=======
func = Polyglot.eval("R", "c")
vect = R.exec_function(func, 1, 2, 3, 4, 5)

func = Polyglot.eval("R", "`[`")
v1 = R.exec_function(func, vect, -2)

v2 = R.exec_function(func, v1, 2)
p v2.to_s
#=========
The printed value here is:
>> [3]

#========
# Second example: the return value is a scalar.  The only difference
# between the first example and this one is that function ex_f
# is defined on the global scope.  This function is an exact copy
# of function exec_function defined in the R module
#========
def ex_f(function, *args)
  pl = R.parse2list(*args)
  R.eval("do.call").call(function, pl)
end

func = Polyglot.eval("R", "c")
vect = R.exec_function(func, 1, 2, 3, 4, 5)

func = Polyglot.eval("R", "`[`")
v1 = R.exec_function(func, vect, -2)

v2 = ex_f(func, v1, 2)
p v2.to_s
#========
The printed result here is:
>> 3.0


Note that the only difference between example 1 and 2 is the definition of
function ex_f, copy of execute_function, in the global scope.  I can't
understand the printed values and the change from Array to double.

I have executed this as:

ruby --polyglot --jvm -Xsingle_threaded strange.rb

-- 
Rodrigo Botafogo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20180716/d2ab4589/attachment.html 


More information about the GraalVM-Users mailing list