Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 57 additions & 63 deletions core/src/main/java/org/jruby/RubyArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,14 @@ public IRubyObject initialize_copy(IRubyObject orig) {
public IRubyObject dup() {
if (metaClass.getClassIndex() != ClassIndex.ARRAY) return super.dup();

RubyArray dup = dupImpl(metaClass.runtime.getArray());
Ruby runtime = metaClass.runtime;
RubyArray dup = dupImpl(runtime, runtime.getArray());
dup.flags |= flags & TAINTED_F; // from DUP_SETUP
return dup;
}

protected RubyArray dupImpl(RubyClass metaClass) {
RubyArray dup = new RubyArray(metaClass.runtime, metaClass, values, begin, realLength, true);
protected RubyArray dupImpl(Ruby runtime, RubyClass metaClass) {
RubyArray dup = new RubyArray(runtime, metaClass, values, begin, realLength, true);
dup.isShared = this.isShared = true;
return dup;
}
Expand All @@ -723,7 +724,8 @@ public RubyArray aryDup() {
// In 1.9, rb_ary_dup logic changed so that on subclasses of Array,
// dup returns an instance of Array, rather than an instance of the subclass
// Also, taintedness and trustedness are not inherited to duplicates
return dupImpl(metaClass.runtime.getArray());
Ruby runtime = metaClass.runtime;
return dupImpl(runtime, runtime.getArray());
}

/** rb_ary_replace
Expand Down Expand Up @@ -1256,13 +1258,7 @@ public RubyFixnum length() {
}

protected SizeFn enumLengthFn() {
final RubyArray self = this;
return new SizeFn() {
@Override
public IRubyObject size(IRubyObject[] args) {
return self.length();
}
};
return (context, args) -> this.length();
}

/** rb_ary_push - specialized rb_ary_store
Expand Down Expand Up @@ -2031,11 +2027,12 @@ public IRubyObject join19(ThreadContext context) {
*/
@JRubyMethod(name = "to_a")
@Override
public RubyArray to_a() {
public RubyArray to_a(ThreadContext context) {
final RubyClass metaClass = this.metaClass;
final RubyClass arrayClass = metaClass.runtime.getArray();
Ruby runtime = context.runtime;
final RubyClass arrayClass = runtime.getArray();
if (metaClass != arrayClass) {
return dupImpl(arrayClass);
return dupImpl(runtime, arrayClass);
}
return this;
}
Expand Down Expand Up @@ -3855,7 +3852,7 @@ private SizeFn cycleSizeFn(final ThreadContext context) {
return new SizeFn() {
CallSite op_times = sites(context).op_times;
@Override
public IRubyObject size(IRubyObject[] args) {
public IRubyObject size(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;
IRubyObject n = context.nil;

Expand Down Expand Up @@ -3954,7 +3951,7 @@ public IRubyObject product19(ThreadContext context, IRubyObject[] args, Block bl
@JRubyMethod(name = "combination")
public IRubyObject combination(ThreadContext context, IRubyObject num, Block block) {
Ruby runtime = context.runtime;
if (!block.isGiven()) return enumeratorizeWithSize(context, this, "combination", new IRubyObject[]{num}, combinationSize(context));
if (!block.isGiven()) return enumeratorizeWithSize(context, this, "combination", new IRubyObject[]{num}, combinationSize());

int n = RubyNumeric.num2int(num);

Expand Down Expand Up @@ -4012,17 +4009,13 @@ private static void rcombinate(ThreadContext context, int n, int r, int[] p, Rub
}
}

private SizeFn combinationSize(final ThreadContext context) {
final RubyArray self = this;
return new SizeFn() {
@Override
public IRubyObject size(IRubyObject[] args) {
long n = self.realLength;
assert args != null && args.length > 0 && args[0] instanceof RubyNumeric; // #combination ensures arg[0] is numeric
long k = ((RubyNumeric) args[0]).getLongValue();
private SizeFn combinationSize() {
return (context, args) -> {
long n = this.realLength;
assert args != null && args.length > 0 && args[0] instanceof RubyNumeric; // #combination ensures arg[0] is numeric
long k = ((RubyNumeric) args[0]).getLongValue();

return binomialCoefficient(context, k, n);
}
return binomialCoefficient(context, k, n);
};
}

Expand All @@ -4044,7 +4037,7 @@ private static IRubyObject binomialCoefficient(ThreadContext context, long comb,
@JRubyMethod(name = "repeated_combination")
public IRubyObject repeatedCombination(ThreadContext context, IRubyObject num, Block block) {
Ruby runtime = context.runtime;
if (!block.isGiven()) return enumeratorizeWithSize(context, this, "repeated_combination", new IRubyObject[] { num }, repeatedCombinationSize(context));
if (!block.isGiven()) return enumeratorizeWithSize(context, this, "repeated_combination", new IRubyObject[] { num }, repeatedCombinationSize());

int n = RubyNumeric.num2int(num);

Expand All @@ -4065,21 +4058,17 @@ public IRubyObject repeatedCombination(ThreadContext context, IRubyObject num, B
return this;
}

private SizeFn repeatedCombinationSize(final ThreadContext context) {
final RubyArray self = this;
return new SizeFn() {
@Override
public IRubyObject size(IRubyObject[] args) {
long n = self.realLength;
assert args != null && args.length > 0 && args[0] instanceof RubyNumeric; // #repeated_combination ensures arg[0] is numeric
long k = ((RubyNumeric) args[0]).getLongValue();

if (k == 0) {
return RubyFixnum.one(context.runtime);
}
private SizeFn repeatedCombinationSize() {
return (context, args) -> {
long n = this.realLength;
assert args != null && args.length > 0 && args[0] instanceof RubyNumeric; // #repeated_combination ensures arg[0] is numeric
long k = ((RubyNumeric) args[0]).getLongValue();

return binomialCoefficient(context, k, n + k - 1);
if (k == 0) {
return RubyFixnum.one(context.runtime);
}

return binomialCoefficient(context, k, n + k - 1);
};
}

Expand Down Expand Up @@ -4145,12 +4134,12 @@ private static void rpermute(ThreadContext context, int n, int r, int[] p, RubyA
*/
@JRubyMethod(name = "permutation")
public IRubyObject permutation(ThreadContext context, IRubyObject num, Block block) {
return block.isGiven() ? permutationCommon(context, RubyNumeric.num2int(num), false, block) : enumeratorizeWithSize(context, this, "permutation", new IRubyObject[] { num }, permutationSize(context));
return block.isGiven() ? permutationCommon(context, RubyNumeric.num2int(num), false, block) : enumeratorizeWithSize(context, this, "permutation", new IRubyObject[] { num }, permutationSize());
}

@JRubyMethod(name = "permutation")
public IRubyObject permutation(ThreadContext context, Block block) {
return block.isGiven() ? permutationCommon(context, realLength, false, block) : enumeratorizeWithSize(context, this, "permutation", permutationSize(context));
return block.isGiven() ? permutationCommon(context, realLength, false, block) : enumeratorizeWithSize(context, this, "permutation", permutationSize());
}

@JRubyMethod(name = "repeated_permutation")
Expand All @@ -4159,17 +4148,15 @@ public IRubyObject repeated_permutation(ThreadContext context, IRubyObject num,
}

private SizeFn repeatedPermutationSize(final ThreadContext context) {
final Ruby runtime = context.runtime;
final RubyArray self = this;

return new SizeFn() {
CallSite op_exp = sites(context).op_exp;
@Override
public IRubyObject size(IRubyObject[] args) {
RubyFixnum n = self.length();
public IRubyObject size(ThreadContext context, IRubyObject[] args) {
RubyFixnum n = RubyArray.this.length();
assert args != null && args.length > 0 && args[0] instanceof RubyNumeric; // #repeated_permutation ensures arg[0] is numeric
long k = ((RubyNumeric) args[0]).getLongValue();

Ruby runtime = context.runtime;
if (k < 0) {
return RubyFixnum.zero(runtime);
}
Expand Down Expand Up @@ -4205,24 +4192,19 @@ private IRubyObject permutationCommon(ThreadContext context, int r, boolean repe
return this;
}

private SizeFn permutationSize(final ThreadContext context) {
final RubyArray self = this;

return new SizeFn() {
@Override
public IRubyObject size(IRubyObject[] args) {
long n = self.realLength;
long k;

if (args != null && args.length > 0) {
assert args[0] instanceof RubyNumeric; // #permutation ensures arg[0] is numeric
k = ((RubyNumeric) args[0]).getLongValue();
} else {
k = n;
}
private SizeFn permutationSize() {
return (context, args) -> {
long n = this.realLength;
long k;

return descendingFactorial(context, n, k);
if (args != null && args.length > 0) {
assert args[0] instanceof RubyNumeric; // #permutation ensures arg[0] is numeric
k = ((RubyNumeric) args[0]).getLongValue();
} else {
k = n;
}

return descendingFactorial(context, n, k);
};
}

Expand Down Expand Up @@ -5461,4 +5443,16 @@ public void ensureCapacity(int minCapacity) {
public IRubyObject flatten_bang19(ThreadContext context) {
return flatten_bang(context);
}

@Deprecated
@Override
public RubyArray to_a() {
final RubyClass metaClass = this.metaClass;
Ruby runtime = metaClass.runtime;
final RubyClass arrayClass = runtime.getArray();
if (metaClass != arrayClass) {
return dupImpl(runtime, arrayClass);
}
return this;
}
}
12 changes: 9 additions & 3 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2572,9 +2572,10 @@ public IRubyObject to_s() {
*
* The default to_a method is deprecated.
*/
public RubyArray to_a() {
getRuntime().getWarnings().warn(ID.DEPRECATED_METHOD, "default 'to_a' will be obsolete");
return getRuntime().newArray(this);
public RubyArray to_a(ThreadContext context) {
Ruby runtime = context.runtime;
runtime.getWarnings().warn(ID.DEPRECATED_METHOD, "default 'to_a' will be obsolete");
return runtime.newArray(this);
}

/** rb_obj_instance_eval
Expand Down Expand Up @@ -3226,6 +3227,11 @@ public IRubyObject op_match19(ThreadContext context, IRubyObject arg) {
return context.nil;
}

@Deprecated
public RubyArray to_a() {
return to_a(getRuntime().getCurrentContext());
}

@Deprecated
public static final int FL_USHIFT = 4;
@Deprecated
Expand Down
Loading