Skip to content

Commit

Permalink
Showing 10 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
import org.jruby.truffle.core.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.RubiniusOnly;
import org.jruby.truffle.core.rope.RopeOperations;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.dispatch.CallDispatchHeadNode;
@@ -116,7 +117,7 @@ private int rubiniusToJRubyFlags(int flags) {

private IRubyObject toJRuby(Object object) {
if (RubyGuards.isRubyString(object)) {
return getContext().getJRubyRuntime().newString(StringOperations.rope((DynamicObject) object).toByteListCopy());
return getContext().getJRubyRuntime().newString(RopeOperations.toByteListCopy(StringOperations.rope((DynamicObject) object)));
} else if (RubyGuards.isRubyEncoding(object)) {
return getContext().getJRubyRuntime().getEncodingService().rubyEncodingFromObject(getContext().getJRubyRuntime().newString(Layouts.ENCODING.getName((DynamicObject) object)));
} else {
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ public static Object matchCommon(RubyContext context, RopeNodes.MakeSubstringNod

final Rope regexpSourceRope = Layouts.REGEXP.getSource(regexp);
final Encoding enc = checkEncoding(regexp, sourceRope, true);
final ByteList preprocessed = RegexpSupport.preprocess(context.getJRubyRuntime(), regexpSourceRope.getUnsafeByteList(), enc, new Encoding[] { null }, RegexpSupport.ErrorMode.RAISE);
final ByteList preprocessed = RegexpSupport.preprocess(context.getJRubyRuntime(), RopeOperations.getByteListReadOnly(regexpSourceRope), enc, new Encoding[] { null }, RegexpSupport.ErrorMode.RAISE);

final Regex r = new Regex(preprocessed.getUnsafeBytes(), preprocessed.getBegin(), preprocessed.getBegin() + preprocessed.getRealSize(), Layouts.REGEXP.getOptions(regexp).toJoniOptions(), checkEncoding(regexp, sourceRope, true));
final Matcher matcher = r.matcher(sourceRope.getBytes(), sourceRope.begin(), sourceRope.begin() + sourceRope.realSize());
@@ -274,7 +274,7 @@ public static Regex compile(Node currentNode, RubyContext context, Rope bytes, R
}
*/

final ByteList byteList = bytes.getUnsafeByteList();
final ByteList byteList = RopeOperations.getByteListReadOnly(bytes);
Encoding enc = bytes.getEncoding();
Encoding[] fixedEnc = new Encoding[]{null};
ByteList unescaped = RegexpSupport.preprocess(context.getJRubyRuntime(), byteList, enc, fixedEnc, RegexpSupport.ErrorMode.RAISE);
@@ -545,7 +545,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public DynamicObject toS(DynamicObject regexp) {
return createString(((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp(getContext().getJRubyRuntime(), Layouts.REGEXP.getSource(regexp).getUnsafeByteList(), Layouts.REGEXP.getRegex(regexp).getOptions()).to_s()).getByteList());
return createString(((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp(getContext().getJRubyRuntime(), RopeOperations.getByteListReadOnly(Layouts.REGEXP.getSource(regexp)), Layouts.REGEXP.getRegex(regexp).getOptions()).to_s()).getByteList());
}

}
4 changes: 1 addition & 3 deletions truffle/src/main/java/org/jruby/truffle/core/rope/Rope.java
Original file line number Diff line number Diff line change
@@ -50,16 +50,14 @@ public boolean isEmpty() {
return byteLength == 0;
}

public final ByteList getUnsafeByteList() {
final ByteList getUnsafeByteList() {
if (unsafeByteList == null) {
unsafeByteList = new ByteList(getBytes(), getEncoding(), false);
}

return unsafeByteList;
}

public final ByteList toByteListCopy() { return new ByteList(getBytes(), getEncoding(), true); }

protected abstract byte getByteSlow(int index);

public final byte[] getRawBytes() {
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import org.jcodings.specific.UTF8Encoding;
import org.jruby.Ruby;
import org.jruby.RubyEncoding;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;
import org.jruby.util.io.EncodingUtils;

@@ -429,4 +430,13 @@ public static int cmp(Rope string, Rope other) {
}
return size == other.realSize() ? 0 : size == len ? -1 : 1;
}

public static ByteList getByteListReadOnly(Rope rope) {
return rope.getUnsafeByteList();
}

public static ByteList toByteListCopy(Rope rope) {
return new ByteList(rope.getBytes(), rope.getEncoding(), true);
}

}
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
import org.jruby.truffle.core.rope.RopeConstants;
import org.jruby.truffle.core.rope.RopeNodes;
import org.jruby.truffle.core.rope.RopeNodesFactory;
import org.jruby.truffle.core.rope.RopeOperations;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.NotProvided;
import org.jruby.truffle.language.RubyGuards;
@@ -95,7 +96,7 @@ private Object primitiveConvertHelper(DynamicObject encodingConverter, DynamicOb
final boolean nonNullSource = source != nil();
Rope sourceRope = nonNullSource ? rope(source) : RopeConstants.EMPTY_UTF8_ROPE;
final Rope targetRope = rope(target);
final ByteList outBytes = targetRope.toByteListCopy();
final ByteList outBytes = RopeOperations.toByteListCopy(targetRope);

final Ptr inPtr = new Ptr();
final Ptr outPtr = new Ptr();
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import org.jruby.truffle.core.regexp.RegexpNodes;
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.rope.RopeNodes;
import org.jruby.truffle.core.rope.RopeOperations;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.util.ByteList;
@@ -142,7 +143,7 @@ public Object searchRegion(DynamicObject regexp, DynamicObject string, int start
final Rope stringRope = StringOperations.rope(string);
final Rope regexpSourceRope = Layouts.REGEXP.getSource(regexp);
final Encoding enc = RegexpNodes.checkEncoding(regexp, stringRope, true);
ByteList preprocessed = RegexpSupport.preprocess(getContext().getJRubyRuntime(), regexpSourceRope.getUnsafeByteList(), enc, new Encoding[]{null}, RegexpSupport.ErrorMode.RAISE);
ByteList preprocessed = RegexpSupport.preprocess(getContext().getJRubyRuntime(), RopeOperations.getByteListReadOnly(regexpSourceRope), enc, new Encoding[]{null}, RegexpSupport.ErrorMode.RAISE);
Rope preprocessedRope = RegexpNodes.shimModifiers(StringOperations.ropeFromByteList(preprocessed));
final Regex r = new Regex(preprocessedRope.getBytes(), preprocessedRope.getBegin(), preprocessedRope.getBegin() + preprocessedRope.getRealSize(), Layouts.REGEXP.getRegex(regexp).getOptions(), RegexpNodes.checkEncoding(regexp, stringRope, true));
final Matcher matcher = r.matcher(stringRope.getBytes(), stringRope.begin(), stringRope.begin() + stringRope.realSize());
Original file line number Diff line number Diff line change
@@ -1247,7 +1247,7 @@ public DynamicObject stringCopyFrom(DynamicObject string, DynamicObject other, i
if(negativeStartOffsetProfile.profile(src < 0)) src = 0;
if(sizeTooLargeInReplacementProfile.profile(cnt > osz - src)) cnt = osz - src;

final ByteList stringBytes = Layouts.STRING.getRope(string).toByteListCopy();
final ByteList stringBytes = RopeOperations.toByteListCopy(Layouts.STRING.getRope(string));
int sz = stringBytes.unsafeBytes().length - stringBytes.begin();
if(negativeDestinationOffsetProfile.profile(dst < 0)) dst = 0;
if(sizeTooLargeInStringProfile.profile(cnt > sz - dst)) cnt = sz - dst;
Original file line number Diff line number Diff line change
@@ -1935,7 +1935,7 @@ public Object squeezeBangZeroArgs(DynamicObject string, Object[] args,
// Taken from org.jruby.RubyString#squeeze_bang19.

final Rope rope = rope(string);
final ByteList buffer = rope.toByteListCopy();
final ByteList buffer = RopeOperations.toByteListCopy(rope);

final boolean squeeze[] = new boolean[StringSupport.TRANS_SIZE];
for (int i = 0; i < StringSupport.TRANS_SIZE; i++) squeeze[i] = true;
@@ -1981,13 +1981,13 @@ private Object performSqueezeBang(DynamicObject string, DynamicObject[] otherStr
@Cached("createBinaryProfile()") ConditionProfile singleByteOptimizableProfile) {

final Rope rope = rope(string);
final ByteList buffer = rope.toByteListCopy();
final ByteList buffer = RopeOperations.toByteListCopy(rope);

DynamicObject otherStr = otherStrings[0];
Rope otherRope = rope(otherStr);
Encoding enc = StringOperations.checkEncoding(getContext(), string, otherStr, this);
final boolean squeeze[] = new boolean[StringSupport.TRANS_SIZE + 1];
StringSupport.TrTables tables = StringSupport.trSetupTable(otherRope.getUnsafeByteList(), getContext().getJRubyRuntime(), squeeze, null, true, enc);
StringSupport.TrTables tables = StringSupport.trSetupTable(RopeOperations.getByteListReadOnly(otherRope), getContext().getJRubyRuntime(), squeeze, null, true, enc);

boolean singlebyte = rope.isSingleByteOptimizable() && otherRope.isSingleByteOptimizable();

@@ -1996,7 +1996,7 @@ private Object performSqueezeBang(DynamicObject string, DynamicObject[] otherStr
otherRope = rope(otherStr);
enc = StringOperations.checkEncoding(getContext(), string, otherStr, this);
singlebyte = singlebyte && otherRope.isSingleByteOptimizable();
tables = StringSupport.trSetupTable(otherRope.getUnsafeByteList(), getContext().getJRubyRuntime(), squeeze, tables, false, enc);
tables = StringSupport.trSetupTable(RopeOperations.getByteListReadOnly(otherRope), getContext().getJRubyRuntime(), squeeze, tables, false, enc);
}

if (singleByteOptimizableProfile.profile(singlebyte)) {
@@ -2495,7 +2495,7 @@ public DynamicObject upcase(DynamicObject string) {
return nil();
}

final ByteList bytes = rope.toByteListCopy();
final ByteList bytes = RopeOperations.toByteListCopy(rope);

try {
final boolean modified = multiByteUpcase(encoding, bytes.unsafeBytes(), bytes.begin(), bytes.realSize());
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ public static String getString(RubyContext context, DynamicObject string) {

public static StringCodeRangeableWrapper getCodeRangeableReadWrite(final DynamicObject string) {
return new StringCodeRangeableWrapper(string) {
private final ByteList byteList = StringOperations.rope(string).toByteListCopy();
private final ByteList byteList = RopeOperations.toByteListCopy(StringOperations.rope(string));
int codeRange = StringOperations.getCodeRange(string).toInt();

@Override
@@ -226,7 +226,7 @@ public static Rope createRope(String s, Encoding encoding) {
}

public static ByteList getByteListReadOnly(DynamicObject object) {
return Layouts.STRING.getRope(object).getUnsafeByteList();
return RopeOperations.getByteListReadOnly(rope(object));
}

public static Rope ropeFromByteList(ByteList byteList) {
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import org.jcodings.Encoding;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.core.rope.CodeRange;
import org.jruby.truffle.core.rope.RopeOperations;
import org.jruby.util.ByteList;
import org.jruby.util.CodeRangeable;

@@ -82,7 +83,7 @@ public Encoding checkEncoding(CodeRangeable other) {

@Override
public ByteList getByteList() {
return Layouts.SYMBOL.getRope(symbol).getUnsafeByteList();
return RopeOperations.getByteListReadOnly(Layouts.SYMBOL.getRope(symbol));
}

}

0 comments on commit 75c651f

Please sign in to comment.