Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e4ac257b42c1
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e7bf9d18e57f
Choose a head ref
  • 4 commits
  • 2 files changed
  • 1 contributor

Commits on Apr 16, 2018

  1. Verified

    This commit was signed with the committer’s verified signature.
    wyattjoh Wyatt Johnson
    Copy the full SHA
    54dcae4 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a4e0955 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    wyattjoh Wyatt Johnson
    Copy the full SHA
    706f102 View commit details
  4. Copy the full SHA
    e7bf9d1 View commit details
Showing with 58 additions and 48 deletions.
  1. +56 −46 core/src/main/java/org/jruby/RubyArray.java
  2. +2 −2 core/src/main/java/org/jruby/specialized/RubyArrayOneObject.java
102 changes: 56 additions & 46 deletions core/src/main/java/org/jruby/RubyArray.java
Original file line number Diff line number Diff line change
@@ -951,54 +951,62 @@ public static RubyArray aryToAry(IRubyObject obj) {
return obj.getRuntime().newArray(obj);
}

/** rb_ary_splice
*
*/
private final void splice(long beg, long len, IRubyObject rpl, boolean oneNine) {
if (len < 0) throw getRuntime().newIndexError("negative length (" + len + ")");
if (beg < 0 && (beg += realLength) < 0) throw getRuntime().newIndexError("index " + (beg - realLength) + " out of array");
private void splice(final Ruby runtime, int beg, int len, IRubyObject rpl) {
if (len < 0) throw runtime.newIndexError("negative length (" + len + ")");
if (beg < 0 && (beg += realLength) < 0)
throw runtime.newIndexError("index " + (beg - realLength) + " out of array");

final RubyArray rplArr;
final int rlen;

if (rpl == null || (rpl.isNil() && !oneNine)) {
if (rpl == null) {
rplArr = null;
rlen = 0;
} else if (rpl.isNil()) {
// 1.9 replaces with nil
rplArr = newArray(getRuntime(), rpl);
rplArr = newArray(runtime, rpl);
rlen = 1;
} else {
rplArr = aryToAry(rpl);
rlen = rplArr.realLength;
}

splice(runtime, beg, len, rplArr, rlen);
}

/** rb_ary_splice
*
*/
private void splice(final Ruby runtime, int beg, int len, final RubyArray rplArr, final int rlen) {
if (len < 0) throw runtime.newIndexError("negative length (" + len + ")");
if (beg < 0 && (beg += realLength) < 0) throw runtime.newIndexError("index " + (beg - realLength) + " out of array");

unpack();
modify();

int valuesLength = values.length - begin;
if (beg >= realLength) {
len = beg + rlen;
if (len >= valuesLength) spliceRealloc((int)len, valuesLength);
if (len >= valuesLength) spliceRealloc(len, valuesLength);
try {
Helpers.fillNil(values, begin + realLength, begin + ((int) beg), getRuntime());
Helpers.fillNil(values, begin + realLength, begin + beg, runtime);
} catch (ArrayIndexOutOfBoundsException e) {
throw concurrentModification(getRuntime(), e);
throw concurrentModification(runtime, e);
}
realLength = (int) len;
realLength = len;
} else {
if (beg + len > realLength) len = realLength - beg;
int alen = realLength + rlen - (int)len;
int alen = realLength + rlen - len;
if (alen >= valuesLength) spliceRealloc(alen, valuesLength);

if (len != rlen) {
safeArrayCopy(values, begin + (int) (beg + len), values, begin + (int) beg + rlen, realLength - (int) (beg + len));
safeArrayCopy(values, begin + (beg + len), values, begin + beg + rlen, realLength - (beg + len));
realLength = alen;
}
}

if (rlen > 0) {
rplArr.copyInto(values, begin + (int) beg, rlen);
rplArr.copyInto(values, begin + beg, rlen);
}
}

@@ -1028,7 +1036,7 @@ private final void spliceOne(long beg, IRubyObject rpl) {
}
}

safeArraySet(values, begin + (int)beg, rpl);
safeArraySet(values, begin + (int) beg, rpl);
}

private void spliceRealloc(int length, int valuesLength) {
@@ -1095,12 +1103,14 @@ public IRubyObject insert(IRubyObject[] args) {
if (pos == -1) pos = realLength;
if (pos < 0) pos++;

RubyArray inserted = new RubyArray(getRuntime(), false);
final Ruby runtime = getRuntime();

RubyArray inserted = new RubyArray(runtime, false);
inserted.values = args;
inserted.begin = 1;
inserted.realLength = args.length - 1;

splice(pos, 0, inserted, false); // rb_ary_new4
splice(runtime, (int) pos, 0, inserted, inserted.realLength); // rb_ary_new4

return this;
}
@@ -1532,17 +1542,17 @@ public IRubyObject aset(IRubyObject arg0, IRubyObject arg1) {
store(((RubyFixnum)arg0).getLongValue(), arg1);
} else if (arg0 instanceof RubyRange) {
RubyRange range = (RubyRange)arg0;
long beg = range.begLen0(realLength);
splice(beg, range.begLen1(realLength, beg), arg1, true);
int beg = (int) range.begLen0(realLength);
splice(getRuntime(), beg, (int) range.begLen1(realLength, beg), arg1);
} else {
ThreadContext context = getRuntime().getCurrentContext();
ArraySites sites = sites(context);

if (RubyRange.isRangeLike(context, arg0, sites.begin_checked, sites.end_checked, sites.exclude_end_checked)) {
RubyRange range = RubyRange.rangeFromRangeLike(context, arg0, sites.begin, sites.end, sites.exclude_end);

long beg = range.begLen0(realLength);
splice(beg, range.begLen1(realLength, beg), arg1, true);
int beg = (int) range.begLen0(realLength);
splice(getRuntime(), beg, (int) range.begLen1(realLength, beg), arg1);
} else {
store(RubyNumeric.num2long(arg0), arg1);
}
@@ -1561,7 +1571,7 @@ public IRubyObject aset19(IRubyObject arg0, IRubyObject arg1) {
@JRubyMethod(name = "[]=")
public IRubyObject aset(IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
modifyCheck();
splice(RubyNumeric.num2long(arg0), RubyNumeric.num2long(arg1), arg2, true);
splice(getRuntime(), RubyNumeric.num2int(arg0), RubyNumeric.num2int(arg1), arg2);
return arg2;
}

@@ -1585,14 +1595,17 @@ public IRubyObject at(IRubyObject pos) {
public RubyArray concat(ThreadContext context, IRubyObject obj) {
modifyCheck();

RubyArray ary = obj.convertToArray();
concat(context.runtime, obj.convertToArray());
return this;
}

return aryAppend(ary);
private void concat(final Ruby runtime, RubyArray obj) {
splice(runtime, realLength, 0, obj, obj.realLength);
}

// MRI: ary_append
public RubyArray aryAppend(RubyArray y) {
if (y.realLength > 0) splice(realLength, 0, y, false);
if (y.realLength > 0) splice(getRuntime(), realLength, 0, y, y.realLength);

return this;
}
@@ -1610,10 +1623,10 @@ public RubyArray concat(ThreadContext context, IRubyObject[] objs) {
RubyArray tmp = newArray(runtime, objs.length);

for (IRubyObject obj : objs) {
tmp.concat(context, obj);
tmp.concat(runtime, obj.convertToArray());
}

aryAppend(tmp);
return aryAppend(tmp);
}

return this;
@@ -2957,29 +2970,27 @@ public IRubyObject slice_bang(IRubyObject[] args) {
}
}

private IRubyObject slice_internal(long pos, long len, Ruby runtime) {
if(len < 0) return runtime.getNil();
private IRubyObject slice_internal(final Ruby runtime, int pos, int len) {
if (len < 0) return runtime.getNil();
int orig_len = realLength;
if(pos < 0) {
if (pos < 0) {
pos += orig_len;
if(pos < 0) {
return runtime.getNil();
}
} else if(orig_len < pos) {
if (pos < 0) return runtime.getNil();
} else if (orig_len < pos) {
return runtime.getNil();
}

if(orig_len < pos + len) {
if (orig_len < pos + len) {
len = orig_len - pos;
}
if(len == 0) {
if (len == 0) {
return runtime.newEmptyArray();
}

unpack();

IRubyObject result = makeShared(begin + (int)pos, (int)len, getMetaClass());
splice(pos, len, null, false);
RubyArray result = makeShared(begin + pos, len, getMetaClass());
splice(runtime, pos, len, null, 0);

return result;
}
@@ -2997,11 +3008,11 @@ public IRubyObject slice_bang(IRubyObject arg0) {
return runtime.getNil();
}

long pos = range.begLen0(realLength);
long len = range.begLen1(realLength, pos);
return slice_internal(pos, len, runtime);
int pos = (int) range.begLen0(realLength);
int len = (int) range.begLen1(realLength, pos);
return slice_internal(runtime, pos, len);
}
return delete_at((int) RubyNumeric.num2long(arg0));
return delete_at(RubyNumeric.num2int(arg0));
}

/** rb_ary_slice_bang
@@ -3010,9 +3021,8 @@ public IRubyObject slice_bang(IRubyObject arg0) {
@JRubyMethod(name = "slice!")
public IRubyObject slice_bang(IRubyObject arg0, IRubyObject arg1) {
modifyCheck();
long pos = RubyNumeric.num2long(arg0);
long len = RubyNumeric.num2long(arg1);
return slice_internal(pos, len, getRuntime());

return slice_internal(getRuntime(), RubyNumeric.num2int(arg0), RubyNumeric.num2int(arg1));
}

/** rb_ary_assoc
Original file line number Diff line number Diff line change
@@ -91,12 +91,12 @@ public void copyInto(IRubyObject[] target, int start) {
@Override
public void copyInto(IRubyObject[] target, int start, int len) {
if (!packed()) {
super.copyInto(target, start);
super.copyInto(target, start, len);
return;
}
if (len != 1) {
unpack();
super.copyInto(target, start);
super.copyInto(target, start, len);
return;
}
target[start] = value;