Skip to content

Commit

Permalink
Showing 2 changed files with 30 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
import org.jruby.truffle.builtins.NonStandard;
import org.jruby.truffle.builtins.Primitive;
import org.jruby.truffle.builtins.PrimitiveArrayArgumentsNode;
import org.jruby.truffle.builtins.YieldingCoreMethodNode;
import org.jruby.truffle.core.rope.Rope;
import org.jruby.truffle.core.rope.RopeConstants;
import org.jruby.truffle.core.rope.RopeNodes;
@@ -99,43 +100,25 @@ private int rubiniusToJRubyFlags(int flags) {
}

@NonStandard
@CoreMethod(names = "transcoding_map", onSingleton = true)
public abstract static class TranscodingMapNode extends CoreMethodArrayArgumentsNode {

@Child private CallDispatchHeadNode upcaseNode;
@Child private CallDispatchHeadNode toSymNode;
@Child private CallDispatchHeadNode newLookupTableNode;
@Child private CallDispatchHeadNode lookupTableWriteNode;
@Child private CallDispatchHeadNode newTranscodingNode;

public TranscodingMapNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
upcaseNode = DispatchHeadNodeFactory.createMethodCall(context);
toSymNode = DispatchHeadNodeFactory.createMethodCall(context);
newLookupTableNode = DispatchHeadNodeFactory.createMethodCall(context);
lookupTableWriteNode = DispatchHeadNodeFactory.createMethodCall(context);
newTranscodingNode = DispatchHeadNodeFactory.createMethodCall(context);
}
@CoreMethod(names = "each_transcoder", onSingleton = true, needsBlock = true)
public abstract static class EachTranscoderNode extends YieldingCoreMethodNode {

@Specialization
public Object transcodingMap(VirtualFrame frame) {
final Object ret = newLookupTableNode.call(frame, coreLibrary().getLookupTableClass(), "new");

public Object transcodingMap(VirtualFrame frame, DynamicObject block) {
for (Map.Entry<String, Map<String, Transcoder>> sourceEntry : TranscodingManager.allTranscoders.entrySet()) {
final DynamicObject source = getContext().getSymbolTable().getSymbol(sourceEntry.getKey());
final Object destinations = newLookupTableNode.call(frame, coreLibrary().getLookupTableClass(), "new");
final int size = sourceEntry.getValue().size();
final Object[] destinations = new Object[size];

int i = 0;
for (Map.Entry<String, Transcoder> destinationEntry : sourceEntry.getValue().entrySet()) {
final DynamicObject destination = getContext().getSymbolTable().getSymbol(destinationEntry.getKey());
final Object lookupTableValue = newTranscodingNode.call(frame, coreLibrary().getTranscodingClass(), "create", source, destination);

lookupTableWriteNode.call(frame, destinations, "[]=", destination, lookupTableValue);
destinations[i++] = getContext().getSymbolTable().getSymbol(destinationEntry.getKey());
}

lookupTableWriteNode.call(frame, ret, "[]=", source, destinations);
yield(frame, block, source, createArray(destinations, size));
}

return ret;
return nil();
}
}

32 changes: 20 additions & 12 deletions truffle/src/main/ruby/core/encoding.rb
Original file line number Diff line number Diff line change
@@ -37,20 +37,15 @@ class EncodingError < StandardError

class Encoding
class Transcoding
attr_accessor :source
attr_accessor :target
attr_reader :source, :target

def inspect
"#<#{super} #{source} to #{target}"
def initialize(source, target)
@source = source
@target = target
end

def self.create(source, target)
ret = new

ret.source = source
ret.target = target

ret
def inspect
"#<#{super} #{source} to #{target}"
end
end

@@ -76,9 +71,22 @@ def build_encoding_map
map
end
private :build_encoding_map

def build_transcoding_map
map = Rubinius::LookupTable.new
Encoding::Converter.each_transcoder { |source, destinations|

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum Dec 15, 2016

Contributor

I think we use do...end for multiline blocks basically everywhere else.

This comment has been minimized.

Copy link
@eregon

eregon Dec 15, 2016

Author Member

I don't really mind either of them, although do...end doesn't work well if something is called after the block (not in this case). Should I change it?

This comment has been minimized.

Copy link
@pitr-ch

pitr-ch Dec 15, 2016

Member

do...end is well established convention for multiline blocks, please change

h = {}
destinations.each { |dest|
h[dest] = Transcoding.new(source, dest)
}
map[source] = h
}
map
end
private :build_transcoding_map
end

TranscodingMap = Encoding::Converter.transcoding_map
TranscodingMap = build_transcoding_map
EncodingMap = build_encoding_map

@default_external = Truffle::Encoding.get_default_encoding('external')

0 comments on commit e26dfb6

Please sign in to comment.