Skip to content

Commit

Permalink
[Truffle] Refactor to remove EncodingMap/List constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Sep 18, 2016
1 parent 711a7ca commit ce80eeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@ public DynamicObject encodingGetObjectEncodingNil(DynamicObject object) {

}

@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);
}

}

@Primitive(name = "encoding_replicate")
public static abstract class EncodingReplicateNode extends PrimitiveArrayArgumentsNode {

Expand Down
25 changes: 11 additions & 14 deletions truffle/src/main/ruby/core/encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def self.create(source, target)
end

class << self
def build_encoding_map
def 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 @@ -75,12 +75,9 @@ def build_encoding_map
}
map
end
private :build_encoding_map
end

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

@default_external = undefined
@default_internal = undefined
Expand Down Expand Up @@ -544,12 +541,12 @@ def self.get_converters(path)

def self.aliases
aliases = {}
EncodingMap.each do |n, r|
Encoding.encoding_map.each do |n, r|
index = r.last
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 All @@ -562,17 +559,17 @@ def self.set_alias_index(name, obj)
when Encoding
source_name = obj.name
when nil
EncodingMap[key][1] = nil
Encoding.encoding_map[key][1] = nil
return
else
source_name = StringValue(obj)
end

entry = EncodingMap[source_name.upcase.to_sym]
entry = Encoding.encoding_map[source_name.upcase.to_sym]
raise ArgumentError, "unknown encoding name - #{source_name}" unless entry
index = entry.last

EncodingMap[key][1] = index
Encoding.encoding_map[key][1] = index
end
private_class_method :set_alias_index

Expand Down Expand Up @@ -611,9 +608,9 @@ def self.find(name)
end

def self.name_list
EncodingMap.map do |n, r|
Encoding.encoding_map.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 All @@ -622,9 +619,9 @@ def inspect
end

def names
entry = EncodingMap[name.upcase.to_sym]
entry = Encoding.encoding_map[name.upcase.to_sym]
names = [name]
EncodingMap.each do |k, r|
Encoding.encoding_map.each do |k, r|
aname = r.first
names << aname if aname and r.last == entry.last
end
Expand Down
4 changes: 2 additions & 2 deletions truffle/src/main/ruby/core/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ def self.try_convert_to_encoding(obj)

key = str.upcase.to_sym

pair = Encoding::EncodingMap[key]
pair = Encoding.encoding_map[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 ce80eeb

Please sign in to comment.