Navigation Menu

Skip to content

Commit

Permalink
[Truffle] Handle String coercion in Encoding.find.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Feb 6, 2015
1 parent a005832 commit 0a7f712
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/encoding/find_tags.txt
@@ -1,6 +1,5 @@
fails:Encoding.find returns the corresponding Encoding object if given a valid alias name
fails:Encoding.find returns the passed Encoding object
fails:Encoding.find accepts any object as encoding name, if it responds to #to_str
fails:Encoding.find raises an ArgumentError if the given encoding does not exist
fails:Encoding.find supports the 'locale' encoding alias
fails:Encoding.find returns default external encoding for the 'external' encoding alias
Expand Down
Expand Up @@ -10,6 +10,8 @@
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.CreateCast;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;
Expand All @@ -20,6 +22,8 @@
import org.jcodings.util.CaseInsensitiveBytesHash;
import org.jcodings.util.Hash;
import org.jruby.runtime.encoding.EncodingService;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.coerce.ToStrNodeFactory;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyEncoding;
Expand Down Expand Up @@ -224,7 +228,8 @@ public RubyNilClass defaultExternal(RubyNilClass encoding) {
}

@CoreMethod(names = "find", onSingleton = true, required = 1)
public abstract static class FindNode extends CoreMethodNode {
@NodeChild(value = "name")
public abstract static class FindNode extends RubyNode {

public FindNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand All @@ -234,6 +239,10 @@ public FindNode(FindNode prev) {
super(prev);
}

@CreateCast("name") public RubyNode coerceNameToString(RubyNode name) {
return ToStrNodeFactory.create(getContext(), getSourceSection(), name);
}

@Specialization
public RubyEncoding find(RubyString name) {
notDesignedForCompilation();
Expand Down

2 comments on commit 0a7f712

@chrisseaton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's simpler than a Rubinius fallback.

@nirvdrum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another benefit of this approach is it cuts down on the number of specializations, which cuts down on the amount of generated code.

Please sign in to comment.