Skip to content

Commit

Permalink
Merge branch 'truffle-allocate'
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Apr 19, 2015
2 parents 787bdf0 + c31f20b commit 77a5ca9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public RubyBasicObject allocate(RubyClass rubyClass) {
@CoreMethod(names = "new", needsBlock = true, argumentsAsArray = true)
public abstract static class NewNode extends CoreMethodNode {

@Child private AllocateNode allocateNode;
@Child private CallDispatchHeadNode allocateNode;
@Child private CallDispatchHeadNode initialize;

public NewNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
allocateNode = ClassNodesFactory.AllocateNodeFactory.create(context, sourceSection, new RubyNode[]{null});
allocateNode = DispatchHeadNodeFactory.createMethodCallOnSelf(context);
initialize = DispatchHeadNodeFactory.createMethodCallOnSelf(context);
}

Expand All @@ -74,17 +74,17 @@ public NewNode(NewNode prev) {
public abstract RubyBasicObject executeNew(VirtualFrame frame, RubyClass rubyClass, Object[] args, Object block);

@Specialization
public RubyBasicObject newInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, UndefinedPlaceholder block) {
public Object newInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, UndefinedPlaceholder block) {
return doNewInstance(frame, rubyClass, args, null);
}

@Specialization
public RubyBasicObject newInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, RubyProc block) {
public Object newInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, RubyProc block) {
return doNewInstance(frame, rubyClass, args, block);
}

private RubyBasicObject doNewInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, RubyProc block) {
final RubyBasicObject instance = allocateNode.executeAllocate(frame, rubyClass);
private Object doNewInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, RubyProc block) {
final Object instance = allocateNode.call(frame, rubyClass, "allocate", null);
initialize.call(frame, instance, "initialize", block, args);
return instance;

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Apr 19, 2015

Author Contributor

We were directly calling the Class#allocate node, not doing a call to whatever #allocate happens to be. This means we now have an extra call on object allocation that we should have had all along. Just hoping that the inliner always sees through it.

@eregon @nirvdrum

This comment has been minimized.

Copy link
@eregon

eregon Apr 19, 2015

Member

This is not how it is implemented in MRI at least. They look an an allocator function attached to the class like us and do a static call. Let's discuss it on Monday.

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Apr 19, 2015

Author Contributor

It looks like this is something non-standard that Rubinius does - and relies upon:

class Foo

  def self.allocate
    puts 'Foo#allocate'
    super
  end

end

Foo.new
$ ~/.rbenv/versions/rbx-2.5.2/bin/ruby test.rb 
Foo#allocate
$ ~/.rbenv/versions/2.2.2/bin/ruby test.rb 
$ ~/.rbenv/versions/jruby-1.7.19/bin/ruby test.rb 

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum Apr 19, 2015

Contributor

I thought with String I was seeing that the Rubinius allocator was being called. I had to remove it from string.rb for things to work properly. Maybe it was only being called on dup or clone?

This comment has been minimized.

Copy link
@eregon

eregon Apr 20, 2015

Member

I suppose that's their way to redefine the allocator in Ruby, sound fair enough.

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ public abstract class DirPrimitiveNodes {
private static final HiddenKey contentsKey = new HiddenKey("contents");
private static final HiddenKey positionKey = new HiddenKey("position");

@RubiniusPrimitive(name = "dir_allocate")
public static abstract class DirAllocatePrimitiveNode extends RubiniusPrimitiveNode {

public DirAllocatePrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public DirAllocatePrimitiveNode(DirAllocatePrimitiveNode prev) {
super(prev);
}

@Specialization
public RubyBasicObject allocate(RubyClass dirClass) {
return new RubyBasicObject(dirClass);
}

}

@RubiniusPrimitive(name = "dir_open")
public static abstract class DirOpenPrimitiveNode extends RubiniusPrimitiveNode {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.UndefinedPlaceholder;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyEncoding;
import org.jruby.truffle.runtime.core.RubyEncodingConverter;
import org.jruby.truffle.runtime.core.RubyException;
import org.jruby.truffle.runtime.core.RubyHash;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.core.RubySymbol;
import org.jruby.truffle.runtime.core.*;
import org.jruby.util.ByteList;
import org.jruby.util.io.EncodingUtils;

Expand All @@ -53,8 +46,8 @@ public EncodingConverterAllocateNode(EncodingConverterAllocateNode prev) {
}

@Specialization
public Object encodingConverterAllocate(RubyEncoding fromEncoding, RubyEncoding toEncoding, RubyHash options) {
return new RubyEncodingConverter(getContext().getCoreLibrary().getEncodingConverterClass(), null);
public Object encodingConverterAllocate(RubyClass encodingConverterClass, UndefinedPlaceholder undefined1, UndefinedPlaceholder undefined2) {
return new RubyEncodingConverter(encodingConverterClass, null);
}

}
Expand Down

0 comments on commit 77a5ca9

Please sign in to comment.