Skip to content

Commit

Permalink
Showing 70 changed files with 2,966 additions and 1,121 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/util/StringSupport.java
Original file line number Diff line number Diff line change
@@ -319,7 +319,7 @@ public static long strLengthWithCodeRange(Encoding enc, byte[]bytes, int p, int
}
}

private static long strLengthWithCodeRangeAsciiCompatible(Encoding enc, byte[]bytes, int p, int end) {
public static long strLengthWithCodeRangeAsciiCompatible(Encoding enc, byte[]bytes, int p, int end) {
int cr = 0, c = 0;
while (p < end) {
if (Encoding.isAscii(bytes[p])) {
@@ -341,7 +341,7 @@ private static long strLengthWithCodeRangeAsciiCompatible(Encoding enc, byte[]by
return pack(c, cr == 0 ? CR_7BIT : cr);
}

private static long strLengthWithCodeRangeNonAsciiCompatible(Encoding enc, byte[]bytes, int p, int end) {
public static long strLengthWithCodeRangeNonAsciiCompatible(Encoding enc, byte[]bytes, int p, int end) {
int cr = 0, c = 0;
for (c = 0; p < end; c++) {
int cl = preciseLength(enc, bytes, p, end);
@@ -365,7 +365,7 @@ public static long strLengthWithCodeRange(ByteList bytes, Encoding enc) {
}

// arg cannot be negative
static long pack(int result, int arg) {
public static long pack(int result, int arg) {
return ((long)arg << 31) | result;
}

8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/util/io/EncodingUtils.java
Original file line number Diff line number Diff line change
@@ -1842,12 +1842,12 @@ public static Encoding getEncoding(ByteList str) {

// MRI: get_actual_encoding
public static Encoding getActualEncoding(Encoding enc, ByteList byteList) {
return getActualEncoding(enc, byteList.getUnsafeBytes(), byteList.begin(), byteList.begin() + byteList.realSize());
}

public static Encoding getActualEncoding(Encoding enc, byte[] bytes, int p, int end) {
if (enc.isDummy() && enc instanceof UnicodeEncoding) {
// handle dummy UTF-16 and UTF-32 by scanning for BOM, as in MRI
byte[] bytes = byteList.unsafeBytes();
int p = byteList.begin();
int end = p + byteList.getRealSize();

if (enc == UTF16Dummy && end - p >= 2) {
int c0 = bytes[p] & 0xff;
int c1 = bytes[p + 1] & 0xff;
23 changes: 22 additions & 1 deletion test/truffle/compiler/pe/core/string_pe.rb
Original file line number Diff line number Diff line change
@@ -12,4 +12,25 @@
example "Truffle::Primitive.create_simple_string.getbyte(0)", simple_string.getbyte(0)

example "'abc'.length", 3
tagged_example "'abc' == 'abc'", true # seems to fail sometimes
example "'こにちわ'.length", 4

example "'abc'.bytesize", 3
example "'こにちわ'.bytesize", 12

example "'abc' == 'abc'", true
example "x = 'abc'; x == x", true
example "x = 'abc'; x == x.dup", true
example "x = 'abc'; 'abc' == x.dup", true

example "'abc'.ascii_only?", true
example "'こにちわ'.ascii_only?", false

example "''.ascii?", false
example "'abc'.ascii?", true

example "'abc'.valid_encoding?", true
example "'こにちわ'.valid_encoding?", true

example "''.empty?", true
example "'abc'.empty?", false
example "'こにちわ'.empty?", false
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.format.runtime.exceptions.FormatException;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;
import org.jruby.util.Pack;
@@ -168,7 +169,7 @@ public Object read(VirtualFrame frame, byte[] source) {
final ByteList result = new ByteList(lElem, 0, index, encoding, false);
setSourcePosition(frame, encode.position());

return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), result, StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN), null);
}

}
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.format.runtime.MissingValue;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;
@@ -109,7 +110,7 @@ public Object read(VirtualFrame frame, byte[] source) {

setSourcePosition(frame, start + length);

return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), result, StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN), null);
}

}
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;
@@ -83,7 +84,7 @@ public Object read(VirtualFrame frame, byte[] source) {
final ByteList result = new ByteList(lElem, encoding, false);
setSourcePosition(frame, encode.position());

return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), result, StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN), null);
}

}
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;
import org.jruby.util.Pack;
@@ -84,7 +85,7 @@ public Object read(VirtualFrame frame, byte[] source) {
final ByteList result = new ByteList(lElem, encoding, false);
setSourcePosition(frame, encode.position());

return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), result, StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN), null);
}

}
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import org.jruby.truffle.format.nodes.PackNode;
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;
import org.jruby.util.Pack;
@@ -75,7 +76,7 @@ public Object read(VirtualFrame frame, byte[] source) {
final ByteList result = new ByteList(lElem, 0, index, encoding, false);
setSourcePosition(frame, encode.position());

return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), result, StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN), null);
}

}
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import org.jruby.truffle.format.nodes.SourceNode;
import org.jruby.truffle.format.runtime.exceptions.NoImplicitConversionException;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;
import org.jruby.util.Pack;
@@ -156,7 +157,7 @@ else if (encode.hasRemaining()) {

setSourcePosition(frame, encode.position());

return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), result, StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.ropeFromByteList(result, StringSupport.CR_UNKNOWN), null);
}

private static int safeGet(ByteBuffer encode) {
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ public ByteList toStringString(VirtualFrame frame, DynamicObject string) {
setTainted(frame);
}

return StringOperations.getByteList(string);
return StringOperations.getByteListReadOnly(string);
}

@Specialization(guards = "isRubyArray(array)")
@@ -116,7 +116,7 @@ public ByteList toString(VirtualFrame frame, DynamicObject array) {
setTainted(frame);
}

return StringOperations.getByteList((DynamicObject) value);
return StringOperations.getByteListReadOnly((DynamicObject) value);
}

CompilerDirectives.transferToInterpreter();
@@ -142,7 +142,7 @@ public ByteList toString(VirtualFrame frame, Object object) {
setTainted(frame);
}

return StringOperations.getByteList((DynamicObject) value);
return StringOperations.getByteListReadOnly((DynamicObject) value);
}

if (inspectOnConversionFailure) {
@@ -152,7 +152,7 @@ public ByteList toString(VirtualFrame frame, Object object) {
getEncapsulatingSourceSection(), new RubyNode[]{null}));
}

return StringOperations.getByteList(inspectNode.toS(frame, object));
return StringOperations.getByteListReadOnly(inspectNode.toS(frame, object));
}

CompilerDirectives.transferToInterpreter();
9 changes: 9 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/nodes/RubyNode.java
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.rope.Rope;
import org.jruby.truffle.runtime.sockets.NativeSockets;
import org.jruby.util.ByteList;

@@ -156,6 +157,10 @@ public DynamicObject getSymbol(ByteList name) {
return getContext().getSymbol(name);
}

public DynamicObject getSymbol(Rope name) {
return getContext().getSymbol(name);
}

/** Creates a String from the ByteList, with unknown CR */
protected DynamicObject createString(ByteList bytes) {
return StringOperations.createString(getContext(), bytes);
@@ -166,6 +171,10 @@ protected DynamicObject create7BitString(ByteList bytes) {
return StringOperations.create7BitString(getContext(), bytes);
}

protected DynamicObject createString(Rope rope) {
return StringOperations.createString(getContext(), rope);
}

protected POSIX posix() {
return getContext().getPosix();
}
Original file line number Diff line number Diff line change
@@ -12,33 +12,38 @@

import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.rope.Rope;
import org.jruby.util.ByteList;

public abstract class StringCachingGuards {

public static ByteList privatizeByteList(DynamicObject string) {
public static Rope privatizeRope(DynamicObject string) {
if (RubyGuards.isRubyString(string)) {
return StringOperations.getByteList(string).dup();
// TODO (nirvdrum 25-Jan-16) Should we flatten the rope to avoid caching a potentially deep rope tree?
return StringOperations.rope(string);
} else {
return null;
}
}

public static boolean byteListsEqual(DynamicObject string, ByteList byteList) {
public static boolean ropesEqual(DynamicObject string, Rope rope) {
if (RubyGuards.isRubyString(string)) {
final Rope stringRope = StringOperations.rope(string);

// equal below does not check encoding
if (StringOperations.getByteList(string).getEncoding() != byteList.getEncoding()) {
if (stringRope.getEncoding() != rope.getEncoding()) {
return false;
}
// TODO CS 8-Nov-15 this code goes off into the woods - need to break it apart and branch profile it
return StringOperations.getByteList(string).equal(byteList);

return stringRope.equals(rope);
} else {
return false;
}
}

public static int byteListLength(ByteList byteList) {
return byteList.length();
public static int ropeLength(Rope rope) {
return rope.byteLength();
}

}
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ protected DynamicObject toSymbolSymbol(DynamicObject symbol) {

@Specialization(guards = "isRubyString(string)")
protected DynamicObject toSymbolString(DynamicObject string) {
return getSymbol(StringOperations.getByteList(string));
return getSymbol(StringOperations.rope(string));
}

@Specialization
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ public InstanceEvalNode(RubyContext context, SourceSection sourceSection) {
@CompilerDirectives.TruffleBoundary
@Specialization(guards = "isRubyString(string)")
public Object instanceEval(Object receiver, DynamicObject string, NotProvided block) {
return getContext().instanceEval(StringOperations.getByteList(string), receiver, "(eval)", this);
return getContext().instanceEval(StringOperations.getByteListReadOnly(string), receiver, "(eval)", this);
}

@Specialization
Loading

0 comments on commit 983325b

Please sign in to comment.