You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Java integration does not support int-like objects:
class I
def initialize(value)
@value = value
return
end
def to_int
return @value
end
end
n = I.new(2)
a = Java::byte[n].new # works: JRuby calls n.to_int
value = I.new(1)
a[1] = value # fails with TypeError: JRuby calls value.toJava(Byte.class)
index = I.new(1)
a[index] # fails with TypeError: JRuby expects RubyInteger or RubyRange
index = I.new(1)
a[index] = 1 # fails with ClassCastException: JRuby casts index to RubyInteger
It's also a problem for InfraRuby compatibility:
require "infraruby-java"
n = Int32[2]
a = Java::byte[n].new # works: JRuby calls n.to_int
value = Byte[1]
a[1] = value # works: JRuby calls value.toJava(Byte.class)
index = Int32[1]
a[index] # fails with TypeError: JRuby expects RubyInteger or RubyRange
index = Int32[1]
a[index] = 1 # fails with ClassCastException: JRuby casts index to RubyInteger
A patch for core/src/main/java/org/jruby/RubyBasicObject.java:
A patch for core/src/main/java/org/jruby/java/proxies/ArrayJavaProxy.java:
@@ -73,19 +73,19 @@
@JRubyMethod(name = "[]")
public IRubyObject op_aref(ThreadContext context, IRubyObject arg) {
- if (arg instanceof RubyInteger) {
- int index = (int)((RubyInteger)arg).getLongValue();
- return ArrayUtils.arefDirect(context.runtime, getObject(), converter, index);
- } else {
+ if (arg instanceof RubyRange) {
return getRange(context, arg);
+ } else {
+ int i = arg.toJava(Integer.class);
+ return ArrayUtils.arefDirect(context.runtime, getObject(), converter, i);
}
}
@JRubyMethod(name = "[]", required = 1, rest = true)
public IRubyObject op_aref(ThreadContext context, IRubyObject[] args) {
- if (args.length == 1 && args[0] instanceof RubyInteger) {
- int index = (int)((RubyInteger)args[0]).getLongValue();
- return ArrayUtils.arefDirect(context.runtime, getObject(), converter, index);
+ if (args.length == 1) {
+ IRubyObject arg = args[0];
+ return op_aref(context, arg);
} else {
return getRange(context, args);
}
@@ -93,7 +93,8 @@
@JRubyMethod(name = "[]=")
public IRubyObject op_aset(ThreadContext context, IRubyObject index, IRubyObject value) {
- ArrayUtils.asetDirect(context.runtime, getObject(), converter, (int)((RubyInteger)index).getLongValue(), value);
+ int i = index.toJava(Integer.class);
+ ArrayUtils.asetDirect(context.runtime, getObject(), converter, i, value);
return value;
}
The text was updated successfully, but these errors were encountered:
InfraRuby
changed the title
Java integration should call toJava or to_int for array index
Java integration should call toJava or to_int for array index and value
Mar 6, 2015
kares
added a commit
to kares/jruby
that referenced
this issue
Mar 11, 2015
@InfraRuby similar issue (java.lang.Integer indexes not-working) bothered me as well ...
taken some of your patches and put it on jruby-1_7 (including some asserts) d45a1c8 thank you!
Java integration does not support int-like objects:
It's also a problem for InfraRuby compatibility:
A patch for core/src/main/java/org/jruby/RubyBasicObject.java:
A patch for core/src/main/java/org/jruby/java/proxies/ArrayJavaProxy.java:
The text was updated successfully, but these errors were encountered: