Skip to content

Commit

Permalink
context tweaks and some mild naming changes
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Nov 5, 2014
1 parent cd044f4 commit 80727e1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
30 changes: 17 additions & 13 deletions core/src/main/java/org/jruby/RubyArray.java
Expand Up @@ -1314,17 +1314,20 @@ public IRubyObject aref19(IRubyObject arg0) {
}

private IRubyObject arefCommon(IRubyObject arg0) {
Ruby runtime = getRuntime();

if (arg0 instanceof RubyRange) {
long[] beglen = ((RubyRange) arg0).begLen(realLength, 0);
return beglen == null ? getRuntime().getNil() : subseq(beglen[0], beglen[1]);
return beglen == null ? runtime.getNil() : subseq(beglen[0], beglen[1]);
} else if (arg0.respondsTo("begin") && arg0.respondsTo("end")) {
IRubyObject begin = arg0.callMethod(getRuntime().getCurrentContext(), "begin");
IRubyObject end = arg0.callMethod(getRuntime().getCurrentContext(), "end");
IRubyObject excl = arg0.callMethod(getRuntime().getCurrentContext(), "exclude_end?");
RubyRange rng = RubyRange.newRange(getRuntime(), getRuntime().getCurrentContext(), begin, end, ((RubyBoolean)excl).isTrue() );
ThreadContext context = getRuntime().getCurrentContext();
IRubyObject begin = arg0.callMethod(context, "begin");
IRubyObject end = arg0.callMethod(context, "end");
IRubyObject excl = arg0.callMethod(context, "exclude_end?");
RubyRange range = RubyRange.newRange(runtime, context, begin, end, excl.isTrue());

long[] beglen = rng.begLen(realLength, 0);
return beglen == null ? getRuntime().getNil() : subseq(beglen[0], beglen[1]);
long[] beglen = range.begLen(realLength, 0);
return beglen == null ? runtime.getNil() : subseq(beglen[0], beglen[1]);
}
return entry(RubyNumeric.num2long(arg0));
}
Expand Down Expand Up @@ -1373,13 +1376,14 @@ public IRubyObject aset19(IRubyObject arg0, IRubyObject arg1) {
long beg = range.begLen0(realLength);
splice(beg, range.begLen1(realLength, beg), arg1, true);
} else if (arg0.respondsTo("begin") && arg0.respondsTo("end")) {
IRubyObject begin = arg0.callMethod(getRuntime().getCurrentContext(), "begin");
IRubyObject end = arg0.callMethod(getRuntime().getCurrentContext(), "end");
IRubyObject excl = arg0.callMethod(getRuntime().getCurrentContext(), "exclude_end?");
RubyRange rng = RubyRange.newRange(getRuntime(), getRuntime().getCurrentContext(), begin, end, ((RubyBoolean)excl).isTrue() );
ThreadContext context = getRuntime().getCurrentContext();
IRubyObject begin = arg0.callMethod(context, "begin");
IRubyObject end = arg0.callMethod(context, "end");
IRubyObject excl = arg0.callMethod(context, "exclude_end?");
RubyRange range = RubyRange.newRange(context.runtime, context, begin, end, excl.isTrue());

long beg = rng.begLen0(realLength);
splice(beg, rng.begLen1(realLength, beg), arg1, true);
long beg = range.begLen0(realLength);
splice(beg, range.begLen1(realLength, beg), arg1, true);
} else {
store(RubyNumeric.num2long(arg0), arg1);
}
Expand Down
22 changes: 11 additions & 11 deletions core/src/main/java/org/jruby/RubyRange.java
Expand Up @@ -253,13 +253,13 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block u
}

@JRubyMethod(required = 1, visibility = PRIVATE)
public IRubyObject initialize_copy(IRubyObject orig) {
public IRubyObject initialize_copy(ThreadContext context, IRubyObject original) {
if (!begin.isNil() || !end.isNil()) {
throw getRuntime().newNameError("`initialize' called twice", "initialize");
throw context.runtime.newNameError("`initialize' called twice", "initialize");
}

RubyRange other = (RubyRange)orig;
init(getRuntime().getCurrentContext(), other.begin, other.end, other.isExclusive);
RubyRange other = (RubyRange) original;
init(context, other.begin, other.end, other.isExclusive);
return getRuntime().getNil();
}

Expand All @@ -279,22 +279,22 @@ public RubyFixnum hash(ThreadContext context) {
private static byte[] DOTDOTDOT = "...".getBytes();
private static byte[] DOTDOT = "..".getBytes();

private IRubyObject inspectValue(IRubyObject value) {
private IRubyObject inspectValue(final ThreadContext context, IRubyObject value) {
return getRuntime().execRecursiveOuter(new Ruby.RecursiveFunction() {
public IRubyObject call(IRubyObject obj, boolean recur) {
if(recur) {
return RubyString.newString(getRuntime(), isExclusive ? "(... ... ...)" : "(... .. ...)");
return RubyString.newString(context.runtime, isExclusive ? "(... ... ...)" : "(... .. ...)");
} else {
return inspect(getRuntime().getCurrentContext(), obj);
return inspect(context, obj);
}
}
}, value);
}

@JRubyMethod(name = {"inspect", "to_s"})
public IRubyObject inspect(final ThreadContext context) {
RubyString i1 = ((RubyString) inspectValue(begin)).strDup(context.runtime);
RubyString i2 = (RubyString) inspectValue(end);
RubyString i1 = ((RubyString) inspectValue(context, begin)).strDup(context.runtime);
RubyString i2 = (RubyString) inspectValue(context, end);
i1.cat(isExclusive ? DOTDOTDOT : DOTDOT);
i1.append(i2);
i1.infectBy(i2);
Expand Down Expand Up @@ -458,8 +458,8 @@ public IRubyObject each19(final ThreadContext context, final Block block) {
if(!tmp.isNil()) {
((RubyString) tmp).uptoCommon19(context, end, isExclusive, block);
} else {
if (!begin.respondsTo("succ")) throw getRuntime().newTypeError(
"can't iterate from " + begin.getMetaClass().getName());
if (!begin.respondsTo("succ")) throw getRuntime().newTypeError("can't iterate from " +
begin.getMetaClass().getName());
rangeEach(context, new RangeCallBack() {
@Override
void call(ThreadContext context, IRubyObject arg) {
Expand Down
29 changes: 15 additions & 14 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -3057,12 +3057,13 @@ private IRubyObject byteARef(Ruby runtime, IRubyObject idx) {
} else if (idx instanceof RubyFixnum) {
index = RubyNumeric.fix2int((RubyFixnum)idx);
} else if (idx.respondsTo("begin") && idx.respondsTo("end")) {
IRubyObject begin = idx.callMethod(getRuntime().getCurrentContext(), "begin");
IRubyObject end = idx.callMethod(getRuntime().getCurrentContext(), "end");
IRubyObject excl = idx.callMethod(getRuntime().getCurrentContext(), "exclude_end?");
RubyRange rng = RubyRange.newRange(getRuntime(), getRuntime().getCurrentContext(), begin, end, ((RubyBoolean)excl).isTrue() );
ThreadContext context = runtime.getCurrentContext();
IRubyObject begin = idx.callMethod(context, "begin");
IRubyObject end = idx.callMethod(context, "end");
IRubyObject excl = idx.callMethod(context, "exclude_end?");
RubyRange range = RubyRange.newRange(runtime, context, begin, end, excl.isTrue());

int[] begLen = rng.begLenInt(getByteList().length(), 0);
int[] begLen = range.begLenInt(getByteList().length(), 0);
return begLen == null ? runtime.getNil() : byteSubstr(runtime, begLen[0], begLen[1]);
} else {
index = RubyNumeric.num2int(idx);
Expand Down Expand Up @@ -3221,12 +3222,12 @@ public IRubyObject op_aref19(ThreadContext context, IRubyObject arg) {
return begLen == null ? runtime.getNil() : substr19(runtime, begLen[0], begLen[1]);
} else if (arg.respondsTo("begin") && arg.respondsTo("end")) {
int len = strLength();
IRubyObject begin = arg.callMethod(getRuntime().getCurrentContext(), "begin");
IRubyObject end = arg.callMethod(getRuntime().getCurrentContext(), "end");
IRubyObject excl = arg.callMethod(getRuntime().getCurrentContext(), "exclude_end?");
RubyRange rng = RubyRange.newRange(getRuntime(), getRuntime().getCurrentContext(), begin, end, ((RubyBoolean)excl).isTrue() );
IRubyObject begin = arg.callMethod(context, "begin");
IRubyObject end = arg.callMethod(context, "end");
IRubyObject excl = arg.callMethod(context, "exclude_end?");
RubyRange range = RubyRange.newRange(runtime, context, begin, end, excl.isTrue());

int[] begLen = rng.begLenInt(len, 0);
int[] begLen = range.begLenInt(len, 0);
return begLen == null ? runtime.getNil() : substr19(runtime, begLen[0], begLen[1]);
}
return op_aref19(runtime, RubyNumeric.num2int(arg));
Expand Down Expand Up @@ -3346,10 +3347,10 @@ public IRubyObject op_aset19(ThreadContext context, IRubyObject arg0, IRubyObjec
replaceInternal19(begLen[0], begLen[1], arg1.convertToString());
return arg1;
} else if (arg0.respondsTo("begin") && arg0.respondsTo("end")) {
IRubyObject begin = arg0.callMethod(getRuntime().getCurrentContext(), "begin");
IRubyObject end = arg0.callMethod(getRuntime().getCurrentContext(), "end");
IRubyObject excl = arg0.callMethod(getRuntime().getCurrentContext(), "exclude_end?");
RubyRange rng = RubyRange.newRange(getRuntime(), getRuntime().getCurrentContext(), begin, end, ((RubyBoolean)excl).isTrue() );
IRubyObject begin = arg0.callMethod(context, "begin");
IRubyObject end = arg0.callMethod(context, "end");
IRubyObject excl = arg0.callMethod(context, "exclude_end?");
RubyRange rng = RubyRange.newRange(context.runtime, context, begin, end, excl.isTrue());

int[] begLen = rng.begLenInt(strLength(), 2);
replaceInternal19(begLen[0], begLen[1], arg1.convertToString());
Expand Down

0 comments on commit 80727e1

Please sign in to comment.