Skip to content

Commit

Permalink
[Truffle] Refactoring Array#zip.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Mar 25, 2015
1 parent eb8f62e commit 8cb2b3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
18 changes: 7 additions & 11 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Expand Up @@ -4587,29 +4587,25 @@ public RubyArray zipObjectObject(RubyArray array, Object[] others) {

@Specialization(guards = {"!isOtherSingleObjectArray"})
public Object zipObjectObjectNotSingleObject(VirtualFrame frame, RubyArray array, Object[] others) {
RubyBasicObject proc = RubyArguments.getBlock(frame.getArguments());
if (proc == null) {
proc = nil();
}
return ruby(frame, "zip_internal(nil, *others)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
return zipRuby(frame, others);
}

@Specialization(guards = {"!isOtherSingleIntegerFixnumArray"})
public Object zipObjectObjectNotSingleInteger(VirtualFrame frame, RubyArray array, Object[] others) {
RubyBasicObject proc = RubyArguments.getBlock(frame.getArguments());
if (proc == null) {
proc = nil();
}
return ruby(frame, "zip_internal(block, *others)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
return zipRuby(frame, others);
}

@Specialization(guards = {"!isObject"})
public Object zipObjectObjectNotObject(VirtualFrame frame, RubyArray array, Object[] others) {
return zipRuby(frame, others);
}

private Object zipRuby(VirtualFrame frame, Object[] others) {
RubyBasicObject proc = RubyArguments.getBlock(frame.getArguments());
if (proc == null) {
proc = nil();
}
return ruby(frame, "zip_internal(block, *others)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
return ruby(frame, "zip_internal(*others, &block)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
}

}
Expand Down
7 changes: 3 additions & 4 deletions truffle/src/main/ruby/core/rubinius/common/array.rb
Expand Up @@ -91,8 +91,7 @@ def first(n = undefined)
Array.new self[0, n]
end

# MODIFIED to handle blocks from java
def zip_internal(block, *others)
def zip_internal(*others)
out = Array.new(size) { [] }
others = others.map do |ary|
if ary.respond_to?(:to_ary)
Expand All @@ -110,8 +109,8 @@ def zip_internal(block, *others)
others.each { |ary| slot << ary.at(i) }
end

unless block.nil?
out.each { |ary| block.call(ary) }
if block_given?
out.each { |ary| yield ary }
return nil
end

Expand Down

0 comments on commit 8cb2b3c

Please sign in to comment.