Skip to content

Commit

Permalink
[Truffle] Reuse SingletonClassNode in Kernel#singleton_class.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Dec 29, 2014
1 parent b542641 commit 19985d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
42 changes: 8 additions & 34 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.jruby.truffle.nodes.dispatch.PredicateDispatchHeadNode;
import org.jruby.truffle.nodes.globals.WrapInThreadLocalNode;
import org.jruby.truffle.nodes.literal.*;
import org.jruby.truffle.nodes.objects.SingletonClassNode;
import org.jruby.truffle.nodes.objects.SingletonClassNodeFactory;
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
import org.jruby.truffle.nodes.objectstorage.WriteHeadObjectFieldNode;
import org.jruby.truffle.nodes.yield.*;
Expand All @@ -39,8 +41,6 @@
import org.jruby.truffle.runtime.backtrace.MRIBacktraceFormatter;
import org.jruby.truffle.runtime.control.*;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyHash;
import org.jruby.truffle.runtime.hash.KeyValue;
import org.jruby.truffle.runtime.hash.HashOperations;
import org.jruby.truffle.runtime.methods.RubyMethod;
Expand Down Expand Up @@ -1772,47 +1772,21 @@ public RubyProc setTraceFunc(RubyProc traceFunc) {
@CoreMethod(names = "singleton_class")
public abstract static class SingletonClassMethodNode extends CoreMethodNode {

@Child protected SingletonClassNode singletonClassNode;

public SingletonClassMethodNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
singletonClassNode = SingletonClassNodeFactory.create(context, sourceSection, null);
}

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

@Specialization(guards = "isTrue")
public RubyClass singletonClassTrue(boolean self) {
return getContext().getCoreLibrary().getTrueClass();
}

@Specialization(guards = "!isTrue")
public RubyClass singletonClassFalse(boolean self) {
return getContext().getCoreLibrary().getFalseClass();
singletonClassNode = prev.singletonClassNode;
}

@Specialization
public RubyClass singletonClass(int self) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().typeErrorCantDefineSingleton(this));
}

@Specialization
public RubyClass singletonClass(long self) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().typeErrorCantDefineSingleton(this));
}

@Specialization
public RubyClass singletonClass(double self) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().typeErrorCantDefineSingleton(this));
}

@Specialization
public RubyClass singletonClass(RubyBasicObject self) {
notDesignedForCompilation();

return self.getSingletonClass(this);
public RubyClass singletonClass(Object self) {
return singletonClassNode.executeSingletonClass(self);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public SingletonClassNode(SingletonClassNode prev) {
super(prev);
}

public abstract RubyClass executeSingletonClass(Object value);

@Specialization(guards = "isTrue")
protected RubyClass singletonClassTrue(boolean value) {
return getContext().getCoreLibrary().getTrueClass();
Expand Down

0 comments on commit 19985d2

Please sign in to comment.