error in scripts that evaluate to a Complex number: could not coerce Complex to 
class java.lang.Object
------------------------------------------------------------------------------------------------------

                 Key: JRUBY-4998
                 URL: http://jira.codehaus.org/browse/JRUBY-4998
             Project: JRuby
          Issue Type: Bug
          Components: Embedding
    Affects Versions: JRuby 1.5.1
         Environment: OS X 10.5.8
            Reporter: Adam Murray
            Priority: Minor
             Fix For: JRuby 1.5.2


The following program fails with the error "could not coerce Complex to class 
java.lang.Object (TypeError)"

import org.jruby.embed.ScriptingContainer;
public class jruby_test {
        public static void main(String[] args) {
                System.setProperty("jruby.home", "<<YOUR JRUBY HOME>>");
                ScriptingContainer container = new ScriptingContainer();
                container.runScriptlet("require 'complex'; Complex.new(3,4)");
        }
}


Also, attempting this instead of container.runScriptlet() has the same problem:

EvalUnit unit = container.parse("require 'complex'; return Complex.new(3,4)");
IRubyObject ret = unit.run();


---------
Notes from Charlie from a discussion on the mailing list:

runScriptlet's contract is that it will produce a coerced/converted Java 
object. For
return values that can't coerce, it will error.

However, in most cases, this works ok. It's trying to coerce things to
java.lang.Object, I believe, which for custom user code just passes
the IRubyObject/RubyObject out directly. For coercible types, they'll
return the coerced value.

See this pastie for the behavioral difference:

&#10132; jruby -rcomplex -rjava -e "Complex.new(1,1).to_java"
-e:1:in `to_java': could not coerce Complex to class java.lang.Object
(TypeError)
       from -e:1

&#10132; jruby -rcomplex -rjava -e "class Foo; end; Foo.new.to_java"

So the problem then is specific to Complex. Or actually, specific to
subclasses of Numeric:

&#10132; jruby -rrational -rjava -e "Rational(1, 2).to_java"
-e:1:in `to_java': could not coerce Rational to class java.lang.Object
(TypeError)
       from -e:1

I tracked this to the implementation of toJava in RubyNumeric:

   @Override
   public Object toJava(Class target) {
       return JavaUtil.getNumericConverter(target).coerce(this, target);
   }

This is probably fixable; we need to modify this coercion to allow passing out 
the actual object when the coercion target is java.lang.Object or similar.


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to