Skip to content

Commit

Permalink
[Truffle] Update Encoding to use primitive and snippet nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Sep 19, 2016
1 parent a279485 commit 2e0d7ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Expand Up @@ -48,6 +48,7 @@
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.SnippetNode;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.util.ByteList;

Expand Down Expand Up @@ -563,8 +564,7 @@ public DynamicObject encodingGetObjectEncodingNil(DynamicObject object) {
public static abstract class EncodingReplicateNode extends PrimitiveArrayArgumentsNode {

@Specialization(guards = "isRubyString(name)")
@TruffleBoundary
public DynamicObject encodingReplicate(DynamicObject self, DynamicObject name) {
public DynamicObject encodingReplicate(VirtualFrame frame, DynamicObject self, DynamicObject name, @Cached("new()") SnippetNode snippetNode) {
final String nameString = StringOperations.getString(name);
final DynamicObject existing = getContext().getEncodingManager().getRubyEncoding(nameString);
if (existing != null) {
Expand All @@ -574,7 +574,19 @@ public DynamicObject encodingReplicate(DynamicObject self, DynamicObject name) {
EncodingDB.replicate(nameString, new String(base.getName()));
final Entry entry = EncodingDB.getEncodings().get(nameString.getBytes());
getContext().getEncodingManager().defineEncoding(entry, nameString.getBytes(), 0, nameString.getBytes().length);
return getContext().getEncodingManager().getRubyEncoding(nameString.toLowerCase(Locale.ENGLISH));
final DynamicObject newEncoding = getContext().getEncodingManager().getRubyEncoding(nameString.toLowerCase(Locale.ENGLISH));
snippetNode.execute(frame, "Encoding::EncodingMap[enc.name.upcase.to_sym] = [nil, index]", "enc", newEncoding, "index", entry.getIndex());
return newEncoding;
}

}

@Primitive(name = "encoding_get_object_encoding_by_index", needsSelf = false)
public static abstract class EncodingGetObjectEncodingByIndexNode extends PrimitiveArrayArgumentsNode {

@Specialization
public DynamicObject encodingGetObjectEncodingByIndex(int index) {
return getContext().getEncodingManager().getRubyEncoding(index);
}

}
Expand Down
7 changes: 3 additions & 4 deletions truffle/src/main/ruby/core/encoding.rb
Expand Up @@ -57,7 +57,7 @@ def self.create(source, target)
class << self
def build_encoding_map
map = Rubinius::LookupTable.new
EncodingList.each_with_index { |encoding, index|
Encoding.list.each_with_index { |encoding, index|
key = encoding.name.upcase.to_sym
map[key] = [nil, index]
}
Expand All @@ -79,7 +79,6 @@ def build_encoding_map
end

TranscodingMap = Encoding::Converter.transcoding_map
EncodingList = Encoding.list.freeze
EncodingMap = build_encoding_map

@default_external = undefined
Expand Down Expand Up @@ -549,7 +548,7 @@ def self.aliases
next unless index

aname = r.first
aliases[aname] = EncodingList[index].name if aname
aliases[aname] = Truffle.invoke_primitive(:encoding_get_object_encoding_by_index, index).name if aname
end

aliases
Expand Down Expand Up @@ -613,7 +612,7 @@ def self.find(name)
def self.name_list
EncodingMap.map do |n, r|
index = r.last
r.first or (index and EncodingList[index].name)
r.first or (index and Truffle.invoke_primitive(:encoding_get_object_encoding_by_index, index).name)
end
end

Expand Down
2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core/type.rb
Expand Up @@ -462,7 +462,7 @@ def self.try_convert_to_encoding(obj)
pair = Encoding::EncodingMap[key]
if pair
index = pair.last
return index && Encoding::EncodingList[index]
return index && Truffle.invoke_primitive(:encoding_get_object_encoding_by_index, index)
end

return undefined
Expand Down

0 comments on commit 2e0d7ea

Please sign in to comment.