Skip to content

Commit 226e682

Browse files
committed
[Truffle] Support String coercion in String#encode.
1 parent c08e59b commit 226e682

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

spec/truffle/tags/core/string/encode_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
fails:String#encode when passed no options transcodes to Encoding.default_internal when set
22
fails:String#encode when passed no options transcodes a 7-bit String despite no generic converting being available
33
fails:String#encode when passed no options raises an Encoding::ConverterNotFoundError when no conversion is possible
4-
fails:String#encode when passed to encoding calls #to_str to convert the object to an Encoding
54
fails:String#encode when passed to encoding transcodes a 7-bit String despite no generic converting being available
65
fails:String#encode when passed to encoding raises an Encoding::ConverterNotFoundError when no conversion is possible
76
fails:String#encode when passed to encoding raises an Encoding::ConverterNotFoundError for an invalid encoding

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ public boolean empty(RubyString string) {
796796
@CoreMethod(names = "encode", required = 1)
797797
public abstract static class EncodeNode extends CoreMethodNode {
798798

799+
@Child private ToStrNode toStrNode;
800+
799801
public EncodeNode(RubyContext context, SourceSection sourceSection) {
800802
super(context, sourceSection);
801803
}
@@ -825,6 +827,18 @@ public RubyString encode(RubyString string, RubyEncoding encoding) {
825827

826828
return getContext().toTruffle(jrubyTranscoded);
827829
}
830+
831+
@Specialization(guards = { "!isRubyString(arguments[1])", "!isRubyEncoding(arguments[1])" })
832+
public RubyString encode(VirtualFrame frame, RubyString string, Object encoding) {
833+
notDesignedForCompilation();
834+
835+
if (toStrNode == null) {
836+
CompilerDirectives.transferToInterpreter();
837+
toStrNode = insert(ToStrNodeFactory.create(getContext(), getSourceSection(), null));
838+
}
839+
840+
return encode(string, toStrNode.executeRubyString(frame, encoding));
841+
}
828842
}
829843

830844
@CoreMethod(names = "encoding")

0 commit comments

Comments
 (0)