Skip to content

Commit

Permalink
JRUBY-2801: Incorrect argument splatting/array construction passing s…
Browse files Browse the repository at this point in the history
…platted args through a block that yields

More RubyObject.getRuntime() reduction

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@7170 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
enebo committed Jul 14, 2008
1 parent 1e7775f commit 83cf236
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 94 deletions.
23 changes: 9 additions & 14 deletions src/org/jruby/RubyClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ public IRubyObject invoke(ThreadContext context, IRubyObject self, int methodInd
public IRubyObject invoke(ThreadContext context, IRubyObject self, String name,
IRubyObject[] args, CallType callType, Block block) {
assert args != null;
DynamicMethod method = null;
method = searchMethod(name);
DynamicMethod method = searchMethod(name);


if (method.isUndefined() || (!name.equals("method_missing") && !method.isCallableFrom(context.getFrameSelf(), callType))) {
Expand All @@ -246,28 +245,25 @@ public IRubyObject invoke(ThreadContext context, IRubyObject self, String name,

public IRubyObject invoke(ThreadContext context, IRubyObject self, String name,
IRubyObject arg, CallType callType, Block block) {
DynamicMethod method = null;
method = searchMethod(name);

DynamicMethod method = searchMethod(name);

if (method.isUndefined() || (!name.equals("method_missing") && !method.isCallableFrom(context.getFrameSelf(), callType))) {
return RuntimeHelpers.callMethodMissing(context, self, method, name, new IRubyObject[] {arg}, context.getFrameSelf(), callType, block);
return RuntimeHelpers.callMethodMissing(context, self, method, name,
new IRubyObject[] {arg}, context.getFrameSelf(), callType, block);
}

return method.call(context, self, this, name, arg, block);
}

public IRubyObject invokeInherited(ThreadContext context, IRubyObject self,
IRubyObject subclass) {
String name = "inherited";
DynamicMethod method = getMetaClass().searchMethod(name);
IRubyObject[] args = new IRubyObject[] {subclass};
public IRubyObject invokeInherited(ThreadContext context, IRubyObject self, IRubyObject subclass) {
DynamicMethod method = getMetaClass().searchMethod("inherited");

if (method.isUndefined()) {
return RuntimeHelpers.callMethodMissing(context, self, method, name, args, context.getFrameSelf(), CallType.FUNCTIONAL, Block.NULL_BLOCK);
return RuntimeHelpers.callMethodMissing(context, self, method, "inherited",
new IRubyObject[] {subclass}, context.getFrameSelf(), CallType.FUNCTIONAL, Block.NULL_BLOCK);
}

return method.call(context, self, getMetaClass(), name, args, Block.NULL_BLOCK);
return method.call(context, self, getMetaClass(), "inherited", subclass, Block.NULL_BLOCK);
}

/** rb_class_new_instance
Expand All @@ -286,7 +282,6 @@ public IRubyObject newInstance(ThreadContext context, IRubyObject[] args, Block
@JRubyMethod(name = "initialize", optional = 1, frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(IRubyObject[] args, Block block) {
if (superClass != null) {
System.out.println(classId);
throw getRuntime().newTypeError("already initialized class");
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public EachWithIndex(ThreadContext ctx, Block block) {
}

public IRubyObject call(ThreadContext context, IRubyObject[] iargs, Block block) {
this.block.yield(context, runtime.newArray(iargs[0], runtime.newFixnum(index++)));
this.block.call(context, new IRubyObject[] { runtime.newArray(iargs[0], runtime.newFixnum(index++)) });
return runtime.getNil();
}
}
Expand Down
37 changes: 19 additions & 18 deletions src/org/jruby/RubyMatchData.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public final boolean used() {
return (flags & MATCH_BUSY) != 0;
}

private RubyArray match_array(int start) {
private RubyArray match_array(Ruby runtime, int start) {
if (regs == null) {
if (start != 0) return getRuntime().newEmptyArray();
if (start != 0) return runtime.newEmptyArray();
if (begin == -1) {
return getRuntime().newArray(getRuntime().getNil());
return getRuntime().newArray(runtime.getNil());
} else {
RubyString ss = str.makeShared(begin, end - begin);
RubyString ss = str.makeShared(runtime, begin, end - begin);
if (isTaint()) ss.setTaint(true);
return getRuntime().newArray(ss);
}
Expand All @@ -108,7 +108,7 @@ private RubyArray match_array(int start) {
if (regs.beg[i] == -1) {
arr.append(getRuntime().getNil());
} else {
RubyString ss = str.makeShared(regs.beg[i], regs.end[i] - regs.beg[i]);
RubyString ss = str.makeShared(runtime, regs.beg[i], regs.end[i] - regs.beg[i]);
if (isTaint()) ss.setTaint(true);
arr.append(ss);
}
Expand Down Expand Up @@ -169,8 +169,9 @@ public IRubyObject inspect() {
*
*/
@JRubyMethod(name = "to_a")
@Override
public RubyArray to_a() {
return match_array(0);
return match_array(getRuntime(), 0);
}

@JRubyMethod(name = "values_at", required = 1, rest = true)
Expand All @@ -187,8 +188,8 @@ public IRubyObject select(ThreadContext context, Block block) {
*
*/
@JRubyMethod(name = "captures")
public IRubyObject captures() {
return match_array(1);
public IRubyObject captures(ThreadContext context) {
return match_array(context.getRuntime(), 1);
}

private int nameToBackrefNumber(RubyString str) {
Expand Down Expand Up @@ -325,15 +326,15 @@ public IRubyObject offset(IRubyObject index) {
*
*/
@JRubyMethod(name = "pre_match")
public IRubyObject pre_match() {
public IRubyObject pre_match(ThreadContext context) {
RubyString ss;

if (regs == null) {
if(begin == -1) return getRuntime().getNil();
ss = str.makeShared(0, begin);
if(begin == -1) return context.getRuntime().getNil();
ss = str.makeShared(context.getRuntime(), 0, begin);
} else {
if(regs.beg[0] == -1) return getRuntime().getNil();
ss = str.makeShared(0, regs.beg[0]);
if(regs.beg[0] == -1) return context.getRuntime().getNil();
ss = str.makeShared(context.getRuntime(), 0, regs.beg[0]);
}

if (isTaint()) ss.setTaint(true);
Expand All @@ -344,15 +345,15 @@ public IRubyObject pre_match() {
*
*/
@JRubyMethod(name = "post_match")
public IRubyObject post_match() {
public IRubyObject post_match(ThreadContext context) {
RubyString ss;

if (regs == null) {
if (begin == -1) return getRuntime().getNil();
ss = str.makeShared(end, str.getByteList().length() - end);
if (begin == -1) return context.getRuntime().getNil();
ss = str.makeShared(context.getRuntime(), end, str.getByteList().length() - end);
} else {
if (regs.beg[0] == -1) return getRuntime().getNil();
ss = str.makeShared(regs.end[0], str.getByteList().length() - regs.end[0]);
if (regs.beg[0] == -1) return context.getRuntime().getNil();
ss = str.makeShared(context.getRuntime(), regs.end[0], str.getByteList().length() - regs.end[0]);
}

if(isTaint()) ss.setTaint(true);
Expand Down
8 changes: 4 additions & 4 deletions src/org/jruby/RubyRegexp.java
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,9 @@ public static IRubyObject nth_match(int nth, IRubyObject match) {
end = m.regs.end[nth];
}

if(start == -1) return match.getRuntime().getNil();
if (start == -1) return match.getRuntime().getNil();

RubyString str = m.str.makeShared(start, end - start);
RubyString str = m.str.makeShared(match.getRuntime(), start, end - start);
str.infectBy(match);
return str;
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ public static IRubyObject match_pre(IRubyObject match) {

if(beg == -1) match.getRuntime().getNil();

RubyString str = m.str.makeShared(0, beg);
RubyString str = m.str.makeShared(match.getRuntime(), 0, beg);
str.infectBy(match);
return str;
}
Expand All @@ -1069,7 +1069,7 @@ public static IRubyObject match_post(IRubyObject match) {
end = m.regs.end[0];
}

RubyString str = m.str.makeShared(end, m.str.getByteList().realSize - end);
RubyString str = m.str.makeShared(match.getRuntime(), end, m.str.getByteList().realSize - end);
str.infectBy(match);
return str;
}
Expand Down
33 changes: 20 additions & 13 deletions src/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ final RubyString strDup(Ruby runtime, RubyClass clazz) {
return dup;
}

public final RubyString makeShared(int index, int len) {
public final RubyString makeShared(Ruby runtime, int index, int len) {
if (len == 0) {
RubyString s = newEmptyString(getRuntime(), getMetaClass());
RubyString s = newEmptyString(runtime, getMetaClass());
s.infectBy(this);
return s;
}

if (shareLevel == SHARE_LEVEL_NONE) shareLevel = SHARE_LEVEL_BUFFER;
RubyString shared = new RubyString(getRuntime(), getMetaClass(), value.makeShared(index, len));
RubyString shared = new RubyString(runtime, getMetaClass(), value.makeShared(index, len));
shared.shareLevel = SHARE_LEVEL_BUFFER;

shared.infectBy(this);
Expand Down Expand Up @@ -1393,12 +1393,12 @@ private IRubyObject gsubCommon(Regex regex, ThreadContext context, final boolean
if (regex.numberOfCaptures() == 0) {
begz = matcher.getBegin();
endz = matcher.getEnd();
val = objAsString(context, block.yield(context, substr(begz, endz - begz)));
val = objAsString(context, block.yield(context, substr(context.getRuntime(), begz, endz - begz)));
} else {
Region region = matcher.getRegion();
begz = region.beg[0];
endz = region.end[0];
val = objAsString(context, block.yield(context, substr(begz, endz - begz)));
val = objAsString(context, block.yield(context, substr(context.getRuntime(), begz, endz - begz)));
}
modifyCheck(bytes, size);
if (bang) {
Expand Down Expand Up @@ -1687,6 +1687,10 @@ private int strRindex(RubyString sub, int pos) {

/* rb_str_substr */
public IRubyObject substr(int beg, int len) {
return substr(getRuntime(), beg, len);
}

public IRubyObject substr(Ruby runtime, int beg, int len) {
int length = value.length();
if (len < 0 || beg > length) return getRuntime().getNil();

Expand All @@ -1696,8 +1700,10 @@ public IRubyObject substr(int beg, int len) {
}

int end = Math.min(length, beg + len);
return makeShared(beg, end - beg);
return makeShared(getRuntime(), beg, end - beg);
}



/* rb_str_replace */
public IRubyObject replace(int beg, int len, RubyString replaceWith) {
Expand Down Expand Up @@ -1736,7 +1742,7 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject arg1, IRubyObject
}
return context.getRuntime().getNil();
}
return substr(RubyNumeric.fix2int(arg1), RubyNumeric.fix2int(arg2));
return substr(context.getRuntime(), RubyNumeric.fix2int(arg1), RubyNumeric.fix2int(arg2));
}

/** rb_str_aref, rb_str_aref_m
Expand All @@ -1755,7 +1761,7 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject arg) {
} else if (arg instanceof RubyRange) {
long[] begLen = ((RubyRange) arg).begLen(value.length(), 0);
return begLen == null ? context.getRuntime().getNil() :
substr((int) begLen[0], (int) begLen[1]);
substr(context.getRuntime(), (int) begLen[0], (int) begLen[1]);
}
int idx = (int) arg.convertToInteger().getLongValue();

Expand Down Expand Up @@ -2201,7 +2207,7 @@ private RubyArray split(ThreadContext context, IRubyObject pat, boolean limit, i
result.append(newEmptyString(runtime, getMetaClass()));
break;
} else if (lastNull) {
result.append(substr(beg, regex.getEncoding().length(value.bytes[begin + beg])));
result.append(substr(runtime, beg, regex.getEncoding().length(value.bytes[begin + beg])));
beg = start - begin;
} else {
if (start == range) {
Expand Down Expand Up @@ -2273,7 +2279,8 @@ private RubyArray split(ThreadContext context, IRubyObject pat, boolean limit, i
}

private RubyArray awkSplit(boolean limit, int lim, int i) {
RubyArray result = getRuntime().newArray();
Ruby runtime = getRuntime();
RubyArray result = runtime.newArray();

byte[]bytes = value.bytes;
int p = value.begin;
Expand All @@ -2293,7 +2300,7 @@ private RubyArray awkSplit(boolean limit, int lim, int i) {
}
} else {
if (ASCII.isSpace(bytes[p] & 0xff)) {
result.append(makeShared(beg, end - beg));
result.append(makeShared(runtime, beg, end - beg));
skip = true;
beg = end + 1;
if (limit) i++;
Expand All @@ -2305,9 +2312,9 @@ private RubyArray awkSplit(boolean limit, int lim, int i) {

if (value.realSize > 0 && (limit || value.realSize > beg || lim < 0)) {
if (value.realSize == beg) {
result.append(newEmptyString(getRuntime(), getMetaClass()));
result.append(newEmptyString(runtime, getMetaClass()));
} else {
result.append(makeShared(beg, value.realSize - beg));
result.append(makeShared(runtime, beg, value.realSize - beg));
}
}
return result;
Expand Down
Loading

0 comments on commit 83cf236

Please sign in to comment.