Skip to content

Commit

Permalink
[Truffle] Removed Array#max since it should be defined in Enumerable.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Jan 12, 2015
1 parent 24dcf50 commit cd998aa
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 125 deletions.
119 changes: 0 additions & 119 deletions core/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
Expand Up @@ -2443,125 +2443,6 @@ public RubyArray mapInPlaceObject(VirtualFrame frame, RubyArray array, RubyProc

// TODO: move into Enumerable?

@CoreMethod(names = "max")
public abstract static class MaxNode extends ArrayCoreMethodNode {

@Child private CallDispatchHeadNode eachNode;
private final MaxBlock maxBlock;

public MaxNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
eachNode = DispatchHeadNodeFactory.createMethodCall(context);
maxBlock = context.getCoreLibrary().getArrayMaxBlock();
}

public MaxNode(MaxNode prev) {
super(prev);
eachNode = prev.eachNode;
maxBlock = prev.maxBlock;
}

@Specialization
public Object max(VirtualFrame frame, RubyArray array) {
// TODO: can we just write to the frame instead of having this indirect object?

final Memo<Object> maximum = new Memo<>();

final VirtualFrame maximumClosureFrame = Truffle.getRuntime().createVirtualFrame(RubyArguments.pack(maxBlock, null, array, null, new Object[]{}), maxBlock.getFrameDescriptor());
maximumClosureFrame.setObject(maxBlock.getFrameSlot(), maximum);

final RubyProc block = new RubyProc(getContext().getCoreLibrary().getProcClass(), RubyProc.Type.PROC,
maxBlock.getSharedMethodInfo(), maxBlock.getCallTarget(), maxBlock.getCallTarget(),
maxBlock.getCallTarget(), maximumClosureFrame.materialize(), null, null, array, null);

eachNode.call(frame, array, "each", block);

if (maximum.get() == null) {
return getContext().getCoreLibrary().getNilObject();
} else {
return maximum.get();
}
}

}

public abstract static class MaxBlockNode extends CoreMethodNode {

@Child private CallDispatchHeadNode compareNode;

public MaxBlockNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
compareNode = DispatchHeadNodeFactory.createMethodCall(context);
}

public MaxBlockNode(MaxBlockNode prev) {
super(prev);
compareNode = prev.compareNode;
}

@Specialization
public RubyNilClass max(VirtualFrame frame, Object maximumObject, Object value) {
final Memo<Object> maximum = (Memo<Object>) maximumObject;

// TODO(CS): cast

final Object current = maximum.get();

if (current == null || (int) compareNode.call(frame, value, "<=>", null, current) < 0) {
maximum.set(value);
}

return getContext().getCoreLibrary().getNilObject();
}

}

public static class MaxBlock implements MethodLike {

private final FrameDescriptor frameDescriptor;
private final FrameSlot frameSlot;
private final SharedMethodInfo sharedMethodInfo;
private final CallTarget callTarget;

public MaxBlock(RubyContext context) {
final SourceSection sourceSection = new CoreSourceSection("Array", "max");

frameDescriptor = new FrameDescriptor();
frameSlot = frameDescriptor.addFrameSlot("maximum_memo");

sharedMethodInfo = new SharedMethodInfo(sourceSection, null, "max", false, null, false);

callTarget = Truffle.getRuntime().createCallTarget(new RubyRootNode(
context, sourceSection, null, sharedMethodInfo,
ArrayNodesFactory.MaxBlockNodeFactory.create(context, sourceSection, new RubyNode[]{
ReadLevelVariableNodeFactory.create(context, sourceSection, frameSlot, 1),
new ReadPreArgumentNode(context, sourceSection, 0, MissingArgumentBehaviour.RUNTIME_ERROR)
})));
}

public FrameDescriptor getFrameDescriptor() {
return frameDescriptor;
}

public FrameSlot getFrameSlot() {
return frameSlot;
}

@Override
public SharedMethodInfo getSharedMethodInfo() {
return sharedMethodInfo;
}

@Override
public RubyModule getDeclaringModule() {
throw new UnsupportedOperationException();
}

public CallTarget getCallTarget() {
return callTarget;
}
}

@CoreMethod(names = "min")
public abstract static class MinNode extends ArrayCoreMethodNode {

Expand Down
Expand Up @@ -122,7 +122,6 @@ public class CoreLibrary {
@CompilerDirectives.CompilationFinal private RubyHash envHash;

private ArrayNodes.MinBlock arrayMinBlock;
private ArrayNodes.MaxBlock arrayMaxBlock;

public CoreLibrary(RubyContext context) {
this.context = context;
Expand Down Expand Up @@ -343,7 +342,6 @@ public void initialize() {
initializeEncodingConstants();

arrayMinBlock = new ArrayNodes.MinBlock(context);
arrayMaxBlock = new ArrayNodes.MaxBlock(context);

argv = new RubyArray(arrayClass);
objectClass.setConstant(null, "ARGV", argv);
Expand Down Expand Up @@ -905,10 +903,6 @@ public ArrayNodes.MinBlock getArrayMinBlock() {
return arrayMinBlock;
}

public ArrayNodes.MaxBlock getArrayMaxBlock() {
return arrayMaxBlock;
}

public RubyClass getNumericClass() {
return numericClass;
}
Expand Down

0 comments on commit cd998aa

Please sign in to comment.