Skip to content

Commit

Permalink
[Truffle] Support String coercion in String#encode.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Feb 6, 2015
1 parent c08e59b commit 226e682
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/encode_tags.txt
@@ -1,7 +1,6 @@
fails:String#encode when passed no options transcodes to Encoding.default_internal when set
fails:String#encode when passed no options transcodes a 7-bit String despite no generic converting being available
fails:String#encode when passed no options raises an Encoding::ConverterNotFoundError when no conversion is possible
fails:String#encode when passed to encoding calls #to_str to convert the object to an Encoding
fails:String#encode when passed to encoding transcodes a 7-bit String despite no generic converting being available
fails:String#encode when passed to encoding raises an Encoding::ConverterNotFoundError when no conversion is possible
fails:String#encode when passed to encoding raises an Encoding::ConverterNotFoundError for an invalid encoding
Expand Down
Expand Up @@ -796,6 +796,8 @@ public boolean empty(RubyString string) {
@CoreMethod(names = "encode", required = 1)
public abstract static class EncodeNode extends CoreMethodNode {

@Child private ToStrNode toStrNode;

public EncodeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
Expand Down Expand Up @@ -825,6 +827,18 @@ public RubyString encode(RubyString string, RubyEncoding encoding) {

return getContext().toTruffle(jrubyTranscoded);
}

@Specialization(guards = { "!isRubyString(arguments[1])", "!isRubyEncoding(arguments[1])" })
public RubyString encode(VirtualFrame frame, RubyString string, Object encoding) {
notDesignedForCompilation();

if (toStrNode == null) {
CompilerDirectives.transferToInterpreter();
toStrNode = insert(ToStrNodeFactory.create(getContext(), getSourceSection(), null));
}

return encode(string, toStrNode.executeRubyString(frame, encoding));
}
}

@CoreMethod(names = "encoding")
Expand Down

0 comments on commit 226e682

Please sign in to comment.