Skip to content

Commit 49b4402

Browse files
committed
[Truffle] Implement CodeRangeable for RubyString via an adapter.
1 parent 59622c2 commit 49b4402

File tree

5 files changed

+115
-55
lines changed

5 files changed

+115
-55
lines changed

truffle/src/main/java/org/jruby/truffle/nodes/core/EncodingNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public CompatibleQueryNode(RubyContext context, SourceSection sourceSection) {
5555
@TruffleBoundary
5656
@Specialization
5757
public Object isCompatible(RubyString first, RubyString second) {
58-
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(first, second);
58+
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(first.getCodeRangeable(), second.getCodeRangeable());
5959

6060
if (compatibleEncoding != null) {
6161
return RubyEncoding.getEncoding(compatibleEncoding);
@@ -139,7 +139,7 @@ public Object isCompatible(RubySymbol first, RubyRegexp second) {
139139
@TruffleBoundary
140140
@Specialization
141141
public Object isCompatible(RubyString first, RubySymbol second) {
142-
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(first, second);
142+
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(first.getCodeRangeable(), second);
143143

144144
if (compatibleEncoding != null) {
145145
return RubyEncoding.getEncoding(compatibleEncoding);

truffle/src/main/java/org/jruby/truffle/nodes/core/StringGuards.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class StringGuards {
1919

2020
public static boolean isSingleByteOptimizable(RubyString string) {
21-
return StringSupport.isSingleByteOptimizable(string, string.getByteList().getEncoding());
21+
return StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), string.getByteList().getEncoding());
2222
}
2323

2424
public static boolean isAsciiCompatible(RubyString string) {
@@ -34,7 +34,7 @@ public static boolean isSingleByte(RubyString string) {
3434
}
3535

3636
public static boolean isValidOr7BitEncoding(RubyString string) {
37-
return string.isCodeRangeValid() || CodeRangeSupport.isCodeRangeAsciiOnly(string);
37+
return string.isCodeRangeValid() || CodeRangeSupport.isCodeRangeAsciiOnly(string.getCodeRangeable());
3838
}
3939

4040
public static boolean isFixedWidthEncoding(RubyString string) {

truffle/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.oracle.truffle.api.dsl.*;
3030
import com.oracle.truffle.api.frame.VirtualFrame;
3131
import com.oracle.truffle.api.nodes.Node;
32+
import com.oracle.truffle.api.object.DynamicObject;
3233
import com.oracle.truffle.api.object.DynamicObjectFactory;
3334
import com.oracle.truffle.api.object.Shape;
3435
import com.oracle.truffle.api.source.SourceSection;
@@ -42,6 +43,7 @@
4243
import org.joni.Matcher;
4344
import org.joni.Option;
4445
import org.jruby.Ruby;
46+
import org.jruby.runtime.Helpers;
4547
import org.jruby.truffle.nodes.RubyNode;
4648
import org.jruby.truffle.nodes.cast.CmpIntNode;
4749
import org.jruby.truffle.nodes.cast.CmpIntNodeGen;
@@ -97,19 +99,19 @@ public static void set(RubyString string, ByteList bytes) {
9799
public static void forceEncoding(RubyString string, Encoding encoding) {
98100
string.modify();
99101
string.clearCodeRange();
100-
StringSupport.associateEncoding(string, encoding);
102+
StringSupport.associateEncoding(string.getCodeRangeable(), encoding);
101103
string.clearCodeRange();
102104
}
103105

104106
public static int length(RubyString string) {
105107
if (CompilerDirectives.injectBranchProbability(
106108
CompilerDirectives.FASTPATH_PROBABILITY,
107-
StringSupport.isSingleByteOptimizable(string, string.getByteList().getEncoding()))) {
109+
StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), string.getByteList().getEncoding()))) {
108110

109111
return string.getByteList().getRealSize();
110112

111113
} else {
112-
return StringSupport.strLengthFromRubyString(string);
114+
return StringSupport.strLengthFromRubyString(string.getCodeRangeable());
113115
}
114116
}
115117

@@ -127,7 +129,7 @@ public static int clampExclusiveIndex(RubyString string, int index) {
127129

128130
@TruffleBoundary
129131
public static Encoding checkEncoding(RubyString string, CodeRangeable other, Node node) {
130-
final Encoding encoding = StringSupport.areCompatible(string, other);
132+
final Encoding encoding = StringSupport.areCompatible(string.getCodeRangeable(), other);
131133

132134
if (encoding == null) {
133135
throw new RaiseException(
@@ -142,7 +144,7 @@ public static Encoding checkEncoding(RubyString string, CodeRangeable other, Nod
142144
}
143145

144146
public static boolean singleByteOptimizable(RubyString string) {
145-
return StringSupport.isSingleByteOptimizable(string, EncodingUtils.STR_ENC_GET(string));
147+
return StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), EncodingUtils.STR_ENC_GET(string.getCodeRangeable()));
146148
}
147149

148150
public static RubyString createEmptyString(RubyClass stringClass) {
@@ -188,7 +190,7 @@ public AddNode(RubyContext context, SourceSection sourceSection) {
188190

189191
@Specialization
190192
public RubyString add(RubyString string, RubyString other) {
191-
final Encoding enc = checkEncoding(string, other, this);
193+
final Encoding enc = checkEncoding(string, other.getCodeRangeable(), this);
192194
final RubyString ret = createString(StringSupport.addByteLists(string.getByteList(), other.getByteList()));
193195

194196
if (taintResultNode == null) {
@@ -311,7 +313,7 @@ public int compare(RubyString a, RubyString b) {
311313

312314
final int ret = a.getByteList().cmp(b.getByteList());
313315

314-
if ((ret == 0) && !StringSupport.areComparable(a, b)) {
316+
if ((ret == 0) && !StringSupport.areComparable(a.getCodeRangeable(), b.getCodeRangeable())) {
315317
return a.getByteList().getEncoding().getIndex() > b.getByteList().getEncoding().getIndex() ? 1 : -1;
316318
}
317319

@@ -427,7 +429,7 @@ public RubyString concat(RubyString string, RubyString other) {
427429
final int[] ptr_cr_ret = { codeRange };
428430

429431
try {
430-
EncodingUtils.encCrStrBufCat(getContext().getRuntime(), string, other.getByteList(), other.getByteList().getEncoding(), codeRange, ptr_cr_ret);
432+
EncodingUtils.encCrStrBufCat(getContext().getRuntime(), string.getCodeRangeable(), other.getByteList(), other.getByteList().getEncoding(), codeRange, ptr_cr_ret);
431433
} catch (org.jruby.exceptions.RaiseException e) {
432434
if (e.getException().getMetaClass() == getContext().getRuntime().getEncodingCompatibilityError()) {
433435
CompilerDirectives.transferToInterpreter();
@@ -753,7 +755,7 @@ public CaseCmpNode(RubyContext context, SourceSection sourceSection) {
753755
public Object caseCmpSingleByte(RubyString string, RubyString other) {
754756
// Taken from org.jruby.RubyString#casecmp19.
755757

756-
if (StringSupport.areCompatible(string, other) == null) {
758+
if (StringSupport.areCompatible(string.getCodeRangeable(), other.getCodeRangeable()) == null) {
757759
return nil();
758760
}
759761

@@ -764,7 +766,7 @@ public Object caseCmpSingleByte(RubyString string, RubyString other) {
764766
public Object caseCmp(RubyString string, RubyString other) {
765767
// Taken from org.jruby.RubyString#casecmp19 and
766768

767-
final Encoding encoding = StringSupport.areCompatible(string, other);
769+
final Encoding encoding = StringSupport.areCompatible(string.getCodeRangeable(), other.getCodeRangeable());
768770

769771
if (encoding == null) {
770772
return nil();
@@ -779,8 +781,8 @@ private int multiByteCasecmp(Encoding enc, ByteList value, ByteList otherValue)
779781
}
780782

781783
public static boolean bothSingleByteOptimizable(RubyString string, RubyString other) {
782-
final boolean stringSingleByteOptimizable = StringSupport.isSingleByteOptimizable(string, string.getByteList().getEncoding());
783-
final boolean otherSingleByteOptimizable = StringSupport.isSingleByteOptimizable(other, other.getByteList().getEncoding());
784+
final boolean stringSingleByteOptimizable = StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), string.getByteList().getEncoding());
785+
final boolean otherSingleByteOptimizable = StringSupport.isSingleByteOptimizable(other.getCodeRangeable(), other.getByteList().getEncoding());
784786

785787
return stringSingleByteOptimizable && otherSingleByteOptimizable;
786788
}
@@ -815,7 +817,7 @@ public Object chopBang(VirtualFrame frame, RubyString string) {
815817

816818
@TruffleBoundary
817819
private int choppedLength(RubyString string) {
818-
return StringSupport.choppedLength19(string, getContext().getRuntime());
820+
return StringSupport.choppedLength19(string.getCodeRangeable(), getContext().getRuntime());
819821
}
820822
}
821823

@@ -859,7 +861,7 @@ private int countSlow(RubyString string, RubyString[] otherStrings) {
859861
for (int i = 1; i < otherStrings.length; i++) {
860862
otherStr = otherStrings[i];
861863

862-
enc = checkEncoding(string, otherStr, this);
864+
enc = checkEncoding(string, otherStr.getCodeRangeable(), this);
863865
tables = StringSupport.trSetupTable(otherStr.getByteList(), getContext().getRuntime(), table, tables, false, enc);
864866
}
865867

@@ -893,7 +895,7 @@ public Object crypt(RubyString string, RubyString salt) {
893895
final RubyString otherStr = StringNodes.createString(getContext().getCoreLibrary().getStringClass(), otherBL);
894896

895897
otherStr.modify();
896-
StringSupport.associateEncoding(otherStr, ascii8bit);
898+
StringSupport.associateEncoding(otherStr.getCodeRangeable(), ascii8bit);
897899

898900
if (otherBL.length() < 2) {
899901
CompilerDirectives.transferToInterpreter();
@@ -919,7 +921,7 @@ public Object crypt(RubyString string, RubyString salt) {
919921
}
920922

921923
final RubyString result = StringNodes.createString(getContext().getCoreLibrary().getStringClass(), new ByteList(cryptedString, 0, cryptedString.length - 1));
922-
StringSupport.associateEncoding(result, ascii8bit);
924+
StringSupport.associateEncoding(result.getCodeRangeable(), ascii8bit);
923925

924926
return result;
925927
}
@@ -973,19 +975,19 @@ public Object deleteBang(VirtualFrame frame, RubyString string, Object... args)
973975
@TruffleBoundary
974976
private Object deleteBangSlow(RubyString string, RubyString... otherStrings) {
975977
RubyString otherString = otherStrings[0];
976-
Encoding enc = checkEncoding(string, otherString, this);
978+
Encoding enc = checkEncoding(string, otherString.getCodeRangeable(), this);
977979

978980
boolean[] squeeze = new boolean[StringSupport.TRANS_SIZE + 1];
979981
StringSupport.TrTables tables = StringSupport.trSetupTable(otherString.getByteList(),
980982
getContext().getRuntime(),
981983
squeeze, null, true, enc);
982984

983985
for (int i = 1; i < otherStrings.length; i++) {
984-
enc = checkEncoding(string, otherStrings[i], this);
986+
enc = checkEncoding(string, otherStrings[i].getCodeRangeable(), this);
985987
tables = StringSupport.trSetupTable(otherStrings[i].getByteList(), getContext().getRuntime(), squeeze, tables, false, enc);
986988
}
987989

988-
if (StringSupport.delete_bangCommon19(string, getContext().getRuntime(), squeeze, tables, enc) == null) {
990+
if (StringSupport.delete_bangCommon19(string.getCodeRangeable(), getContext().getRuntime(), squeeze, tables, enc) == null) {
989991
return nil();
990992
}
991993

@@ -1396,7 +1398,7 @@ public Object lstripBang(RubyString string) {
13961398
return nil();
13971399
}
13981400

1399-
final Encoding enc = EncodingUtils.STR_ENC_GET(string);
1401+
final Encoding enc = EncodingUtils.STR_ENC_GET(string.getCodeRangeable());
14001402
final int s = string.getByteList().getBegin();
14011403
final int end = s + string.getByteList().getRealSize();
14021404
final byte[]bytes = string.getByteList().getUnsafeBytes();
@@ -1557,7 +1559,7 @@ public Object rstripBang(RubyString string) {
15571559
return nil();
15581560
}
15591561

1560-
final Encoding enc = EncodingUtils.STR_ENC_GET(string);
1562+
final Encoding enc = EncodingUtils.STR_ENC_GET(string.getCodeRangeable());
15611563
final byte[] bytes = string.getByteList().getUnsafeBytes();
15621564
final int start = string.getByteList().getBegin();
15631565
final int end = start + string.getByteList().getRealSize();
@@ -1621,7 +1623,7 @@ public RubyBasicObject swapcaseSingleByte(RubyString string) {
16211623
final int end = s + value.getRealSize();
16221624
final byte[]bytes = value.getUnsafeBytes();
16231625

1624-
if (singleByteOptimizableProfile.profile(StringSupport.isSingleByteOptimizable(string, enc))) {
1626+
if (singleByteOptimizableProfile.profile(StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), enc))) {
16251627
if (StringSupport.singleByteSwapcase(bytes, s, end)) {
16261628
return string;
16271629
}
@@ -1835,7 +1837,7 @@ public int sizeSingleByte(RubyString string) {
18351837

18361838
@Specialization(guards = "!isSingleByteOptimizable(string)")
18371839
public int size(RubyString string) {
1838-
return StringSupport.strLengthFromRubyString(string);
1840+
return StringSupport.strLengthFromRubyString(string.getCodeRangeable());
18391841
}
18401842
}
18411843

@@ -1896,15 +1898,15 @@ public Object squeezeBang(VirtualFrame frame, RubyString string, Object... args)
18961898
}
18971899

18981900
RubyString otherStr = otherStrings[0];
1899-
Encoding enc = checkEncoding(string, otherStr, this);
1901+
Encoding enc = checkEncoding(string, otherStr.getCodeRangeable(), this);
19001902
final boolean squeeze[] = new boolean[StringSupport.TRANS_SIZE + 1];
19011903
StringSupport.TrTables tables = StringSupport.trSetupTable(otherStr.getByteList(), getContext().getRuntime(), squeeze, null, true, enc);
19021904

19031905
boolean singlebyte = singleByteOptimizable(string) && singleByteOptimizable(otherStr);
19041906

19051907
for (int i = 1; i < otherStrings.length; i++) {
19061908
otherStr = otherStrings[i];
1907-
enc = string.checkEncoding(otherStr);
1909+
enc = string.checkEncoding(otherStr.getCodeRangeable());
19081910
singlebyte = singlebyte && singleByteOptimizable(otherStr);
19091911
tables = StringSupport.trSetupTable(otherStr.getByteList(), getContext().getRuntime(), squeeze, tables, false, enc);
19101912
}
@@ -2470,18 +2472,18 @@ public static int checkIndexForRef(RubyString string, int index, RubyNode node)
24702472

24712473
@TruffleBoundary
24722474
public static void replaceInternal(RubyString string, int start, int length, RubyString replacement) {
2473-
StringSupport.replaceInternal19(start, length, string, replacement);
2475+
StringSupport.replaceInternal19(start, length, string.getCodeRangeable(), replacement.getCodeRangeable());
24742476
}
24752477

24762478
@TruffleBoundary
24772479
private static Object trTransHelper(RubyContext context, RubyString self, RubyString fromStr, RubyString toStr, boolean sFlag) {
2478-
final CodeRangeable ret = StringSupport.trTransHelper(context.getRuntime(), self, fromStr, toStr, sFlag);
2480+
final CodeRangeable ret = StringSupport.trTransHelper(context.getRuntime(), self.getCodeRangeable(), fromStr.getCodeRangeable(), toStr.getCodeRangeable(), sFlag);
24792481

24802482
if (ret == null) {
24812483
return context.getCoreLibrary().getNilObject();
24822484
}
24832485

2484-
return ret;
2486+
return self;
24852487
}
24862488
}
24872489

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/StringPrimitiveNodes.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public RubyArray stringAwkSplit(RubyString string, int lim) {
138138
boolean skip = true;
139139

140140
int e = 0, b = 0;
141-
final boolean singlebyte = StringSupport.isSingleByteOptimizable(string, enc);
141+
final boolean singlebyte = StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), enc);
142142
while (p < end) {
143143
final int c;
144144
if (singlebyte) {
@@ -445,7 +445,7 @@ public boolean stringEqual(RubyString string, RubyString other) {
445445
final ByteList b = other.getByteList();
446446

447447
if (incompatibleEncodingProfile.profile((a.getEncoding() != b.getEncoding()) &&
448-
(org.jruby.RubyEncoding.areCompatible(string, other) == null))) {
448+
(org.jruby.RubyEncoding.areCompatible(string.getCodeRangeable(), other.getCodeRangeable()) == null))) {
449449
return false;
450450
}
451451

@@ -610,8 +610,8 @@ public StringIndexPrimitiveNode(RubyContext context, SourceSection sourceSection
610610

611611
@Specialization
612612
public Object stringIndex(RubyString string, RubyString pattern, int start) {
613-
final int index = StringSupport.index(string,
614-
pattern,
613+
final int index = StringSupport.index(string.getCodeRangeable(),
614+
pattern.getCodeRangeable(),
615615
start, string.getByteList().getEncoding());
616616

617617
if (index == -1) {
@@ -720,7 +720,7 @@ public Object stringCharacterIndex(RubyString string, RubyString pattern, int of
720720
final byte[] stringBytes = string.getByteList().getUnsafeBytes();
721721
final byte[] patternBytes = pattern.getByteList().getUnsafeBytes();
722722

723-
if (StringSupport.isSingleByteOptimizable(string, string.getByteList().getEncoding())) {
723+
if (StringSupport.isSingleByteOptimizable(string.getCodeRangeable(), string.getByteList().getEncoding())) {
724724
for(s = p += offset, ss = pp; p < e; s = ++p) {
725725
if (stringBytes[p] != patternBytes[pp]) continue;
726726

@@ -854,7 +854,7 @@ public Object stringByteIndex(RubyString string, RubyString pattern, int offset)
854854
return nil();
855855
}
856856

857-
final Encoding encoding = StringNodes.checkEncoding(string, pattern, this);
857+
final Encoding encoding = StringNodes.checkEncoding(string, pattern.getCodeRangeable(), this);
858858
int p = string.getByteList().getBegin();
859859
final int e = p + string.getByteList().getRealSize();
860860
int pp = pattern.getByteList().getBegin();
@@ -1204,12 +1204,12 @@ public Object stringSubstring(RubyString string, int beg, int len) {
12041204
}
12051205
return makeSubstring(string, p - s, e - p);
12061206
} else {
1207-
beg += StringSupport.strLengthFromRubyString(string, enc);
1207+
beg += StringSupport.strLengthFromRubyString(string.getCodeRangeable(), enc);
12081208
if (beg < 0) {
12091209
return nil();
12101210
}
12111211
}
1212-
} else if (beg > 0 && beg > StringSupport.strLengthFromRubyString(string, enc)) {
1212+
} else if (beg > 0 && beg > StringSupport.strLengthFromRubyString(string.getCodeRangeable(), enc)) {
12131213
return nil();
12141214
}
12151215
if (len == 0) {

0 commit comments

Comments
 (0)