Skip to content

Commit 290085d

Browse files
committed
[GR-19220] Add Truffle::Ropes.convert_to_native (#2345)
PullRequest: truffleruby/2643
2 parents f313eb0 + fef2003 commit 290085d

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/main/java/org/truffleruby/core/rope/RopeNodes.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.truffleruby.core.rope.RopeNodesFactory.SetByteNodeGen;
3636
import org.truffleruby.core.string.StringAttributes;
3737
import org.truffleruby.core.string.StringSupport;
38-
import org.truffleruby.core.string.StringUtils;
3938
import org.truffleruby.language.NotProvided;
4039
import org.truffleruby.language.RubyBaseNode;
4140
import org.truffleruby.language.RubyContextNode;
@@ -745,7 +744,7 @@ protected Object debugPrintLeafRope(LeafRope rope, int currentLevel, boolean pri
745744
// Converting a rope to a java.lang.String may populate the byte[], so we need to query for the array status beforehand.
746745
final boolean bytesAreNull = rope.getRawBytes() == null;
747746

748-
System.err.println(StringUtils.format(
747+
System.err.println(String.format(
749748
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
750749
printString ? RopeOperations.escape(rope) : "<skipped>",
751750
rope.getClass().getSimpleName(),
@@ -766,7 +765,7 @@ protected Object debugPrintSubstringRope(SubstringRope rope, int currentLevel, b
766765
// Converting a rope to a java.lang.String may populate the byte[], so we need to query for the array status beforehand.
767766
final boolean bytesAreNull = rope.getRawBytes() == null;
768767

769-
System.err.println(StringUtils.format(
768+
System.err.println(String.format(
770769
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; O: %d; E: %s)",
771770
printString ? RopeOperations.escape(rope) : "<skipped>",
772771
rope.getClass().getSimpleName(),
@@ -793,7 +792,7 @@ protected Object debugPrintConcatRopeBytes(ConcatRope rope, int currentLevel, bo
793792
final boolean bytesAreNull = rope.getRawBytes() == null;
794793

795794
if (state.isFlattened()) {
796-
System.err.println(StringUtils.format(
795+
System.err.println(String.format(
797796
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
798797
printString ? RopeOperations.escape(rope) : "<skipped>",
799798
rope.getClass().getSimpleName(),
@@ -803,7 +802,7 @@ protected Object debugPrintConcatRopeBytes(ConcatRope rope, int currentLevel, bo
803802
rope.getCodeRange(),
804803
rope.getEncoding()));
805804
} else {
806-
System.err.println(StringUtils.format(
805+
System.err.println(String.format(
807806
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
808807
printString ? RopeOperations.escape(rope) : "<skipped>",
809808
rope.getClass().getSimpleName(),
@@ -828,7 +827,7 @@ protected Object debugPrintRepeatingRope(RepeatingRope rope, int currentLevel, b
828827
// Converting a rope to a java.lang.String may populate the byte[], so we need to query for the array status beforehand.
829828
final boolean bytesAreNull = rope.getRawBytes() == null;
830829

831-
System.err.println(StringUtils.format(
830+
System.err.println(String.format(
832831
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; T: %d; D: %d; E: %s)",
833832
printString ? RopeOperations.escape(rope) : "<skipped>",
834833
rope.getClass().getSimpleName(),
@@ -852,7 +851,7 @@ protected Object debugPrintLazyInt(LazyIntRope rope, int currentLevel, boolean p
852851
// Converting a rope to a java.lang.String may populate the byte[], so we need to query for the array status beforehand.
853852
final boolean bytesAreNull = rope.getRawBytes() == null;
854853

855-
System.err.println(StringUtils.format(
854+
System.err.println(String.format(
856855
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; V: %d, D: %d; E: %s)",
857856
printString ? RopeOperations.escape(rope) : "<skipped>",
858857
rope.getClass().getSimpleName(),
@@ -866,6 +865,25 @@ protected Object debugPrintLazyInt(LazyIntRope rope, int currentLevel, boolean p
866865
return nil;
867866
}
868867

868+
@TruffleBoundary
869+
@Specialization
870+
protected Object debugPrintNative(NativeRope rope, int currentLevel, boolean printString) {
871+
printPreamble(currentLevel);
872+
873+
System.err.println(String.format(
874+
"%s (%s; BL: %d; CL: %d; CR: %s; P: 0x%x, S: %d; E: %s)",
875+
printString ? RopeOperations.escape(rope) : "<skipped>",
876+
rope.getClass().getSimpleName(),
877+
rope.byteLength(),
878+
rope.characterLength(),
879+
rope.getCodeRange(),
880+
rope.getNativePointer().getAddress(),
881+
rope.getNativePointer().getSize(),
882+
rope.getEncoding()));
883+
884+
return nil;
885+
}
886+
869887
private void printPreamble(int level) {
870888
if (level > 0) {
871889
for (int i = 0; i < level; i++) {

src/main/java/org/truffleruby/core/rope/TruffleRopesNodes.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.truffleruby.builtins.CoreMethod;
1515
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1616
import org.truffleruby.builtins.CoreModule;
17+
import org.truffleruby.cext.CExtNodes;
1718
import org.truffleruby.core.rope.ConcatRope.ConcatState;
1819
import org.truffleruby.core.string.RubyString;
1920
import org.truffleruby.core.string.StringNodes;
@@ -79,6 +80,8 @@ protected Object debugPrint(Object string, boolean printString,
7980
System.err.println("LD = Left Depth (ConcatRope only)");
8081
System.err.println("RD = Right Depth (ConcatRope only)");
8182
System.err.println("E = Encoding");
83+
System.err.println("P = Native Pointer (NativeRope only)");
84+
System.err.println("S = Native Size (NativeRope only)");
8285

8386
return debugPrintRopeNode.executeDebugPrint(strings.getRope(string), 0, printString);
8487
}
@@ -164,6 +167,19 @@ protected RubyString flattenRope(Object string,
164167

165168
}
166169

170+
@CoreMethod(names = "convert_to_native", onSingleton = true, required = 1)
171+
public abstract static class NativeRopeNode extends CoreMethodArrayArgumentsNode {
172+
173+
@Specialization(guards = "strings.isRubyString(string)")
174+
protected Object nativeRope(Object string,
175+
@Cached CExtNodes.StringToNativeNode toNativeNode,
176+
@CachedLibrary(limit = "2") RubyStringLibrary strings) {
177+
toNativeNode.executeToNative(string);
178+
return string;
179+
}
180+
181+
}
182+
167183
/* Truffle.create_simple_string creates a string 'test' without any part of the string escaping. Useful for testing
168184
* compilation of String because most other ways to construct a string can currently escape. */
169185

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,7 +4936,6 @@ protected Object splicePrepend(
49364936
@Cached RopeNodes.SubstringNode prependSubstringNode,
49374937
@Cached RopeNodes.ConcatNode prependConcatNode,
49384938
@CachedLibrary(limit = "2") RubyStringLibrary libOther) {
4939-
49404939
final Encoding encoding = rubyEncoding.encoding;
49414940
final Rope original = string.rope;
49424941
final Rope left = libOther.getRope(other);
@@ -4972,7 +4971,6 @@ protected RubyString splice(
49724971
@Cached RopeNodes.ConcatNode leftConcatNode,
49734972
@Cached RopeNodes.ConcatNode rightConcatNode,
49744973
@CachedLibrary(limit = "2") RubyStringLibrary libOther) {
4975-
49764974
final Encoding encoding = rubyEncoding.encoding;
49774975
final Rope source = string.rope;
49784976
final Rope insert = libOther.getRope(other);

0 commit comments

Comments
 (0)