Skip to content

Commit

Permalink
[Truffle] Remove boxed versions of primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Nov 29, 2014
1 parent 922e631 commit a90e5d0
Show file tree
Hide file tree
Showing 51 changed files with 740 additions and 1,149 deletions.
10 changes: 4 additions & 6 deletions core/src/main/java/org/jruby/truffle/nodes/RubyCallNode.java
Expand Up @@ -184,23 +184,21 @@ public Object isDefined(VirtualFrame frame) {
return getContext().getCoreLibrary().getNilObject();
}

final RubyBasicObject receiverBasicObject = context.getCoreLibrary().box(receiverObject);

// TODO(CS): this lookup should be cached

final RubyMethod method = ModuleOperations.lookupMethod(receiverBasicObject.getMetaClass(), methodName);
final RubyMethod method = ModuleOperations.lookupMethod(context.getCoreLibrary().getMetaClass(receiverObject), methodName);

final RubyBasicObject self = context.getCoreLibrary().box(RubyArguments.getSelf(frame.getArguments()));
final Object self = RubyArguments.getSelf(frame.getArguments());

if (method == null) {
final Object r = respondToMissing.call(frame, receiverBasicObject, "respond_to_missing?", null, context.makeString(methodName));
final Object r = respondToMissing.call(frame, receiverObject, "respond_to_missing?", null, context.makeString(methodName));

if (r != Dispatch.MISSING && !respondToMissingCast.executeBoolean(frame, r)) {
return getContext().getCoreLibrary().getNilObject();
}
} else if (method.isUndefined()) {
return getContext().getCoreLibrary().getNilObject();
} else if (!ignoreVisibility && !method.isVisibleTo(this, self.getMetaClass())) {
} else if (!ignoreVisibility && !method.isVisibleTo(this, context.getCoreLibrary().getMetaClass(self))) {
return getContext().getCoreLibrary().getNilObject();
}

Expand Down
51 changes: 50 additions & 1 deletion core/src/main/java/org/jruby/truffle/nodes/RubyNode.java
Expand Up @@ -218,7 +218,56 @@ public static void notDesignedForCompilation() {
CompilerAsserts.neverPartOfCompilation();
}

public static boolean isNil(RubyObject o) {
public static boolean isNil(Object o) {
return o instanceof RubyNilClass;
}

public static boolean isTrue(boolean b) {
return b;
}

public static boolean isModule(RubyBasicObject o) {
return o instanceof RubyModule;
}

public static boolean isArray(Object o) {
return o instanceof RubyArray;
}

public static boolean isFixnum(Object o) {
return o instanceof Integer || o instanceof Long;
}

public static boolean isBignum(Object o) {
return o instanceof BigInteger;
}

public static boolean isFloat(Object o) {
return o instanceof Double;
}

public static boolean isFirstFixnum(Object o) {
return o instanceof Integer || o instanceof Long;
}

public static boolean isFirstBignum(Object o) {
return o instanceof BigInteger;
}

public static boolean isFirstFloat(Object o) {
return o instanceof Double;
}

public static boolean isSecondFixnum(Object a, Object b) {
return b instanceof Integer || b instanceof Long;
}

public static boolean isSecondBignum(Object a, Object b) {
return b instanceof BigInteger;
}

public static boolean isSecondFloat(Object a, Object b) {
return b instanceof Double;
}

}
36 changes: 0 additions & 36 deletions core/src/main/java/org/jruby/truffle/nodes/RubyTypes.java
Expand Up @@ -42,15 +42,11 @@
RubyRange.LongFixnumRange.class, //
RubyRange.ObjectRange.class, //
RubyArray.class, //
RubyBignum.class, //
RubyBinding.class, //
RubyClass.class, //
RubyException.class, //
RubyFiber.class, //
RubyFile.class, //
RubyFixnum.IntegerFixnum.class, //
RubyFixnum.LongFixnum.class, //
RubyFloat.class, //
RubyHash.class, //
RubyMatchData.class, //
RubyModule.class, //
Expand All @@ -63,8 +59,6 @@
RubySymbol.class, //
RubyThread.class, //
RubyTime.class, //
RubyTrueClass.class, //
RubyFalseClass.class, //
RubiniusChannel.class, //
RubiniusByteArray.class, //
RubyEncodingConverter.class, //
Expand All @@ -74,34 +68,4 @@

public class RubyTypes {

@ImplicitCast
public boolean unboxBoolean(RubyTrueClass value) {
return true;
}

@ImplicitCast
public boolean unboxBoolean(RubyFalseClass value) {
return false;
}

@ImplicitCast
public int unboxIntegerFixnum(RubyFixnum.IntegerFixnum value) {
return value.getValue();
}

@ImplicitCast
public long unboxLongFixnum(RubyFixnum.LongFixnum value) {
return value.getValue();
}

@ImplicitCast
public BigInteger unboxBignum(RubyBignum value) {
return value.getValue();
}

@ImplicitCast
public double unboxFloat(RubyFloat value) {
return value.getValue();
}

}
36 changes: 32 additions & 4 deletions core/src/main/java/org/jruby/truffle/nodes/cast/ArrayCastNode.java
Expand Up @@ -19,8 +19,11 @@
import org.jruby.truffle.runtime.*;
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.RubyNilClass;

import java.math.BigInteger;

@NodeChild("child")
public abstract class ArrayCastNode extends RubyNode {

Expand All @@ -39,17 +42,42 @@ public ArrayCastNode(ArrayCastNode prev) {
protected abstract RubyNode getChild();

@Specialization
public RubyArray doArray(RubyArray array) {
public RubyNilClass cast(boolean value) {
return getContext().getCoreLibrary().getNilObject();
}

@Specialization
public RubyNilClass cast(int value) {
return getContext().getCoreLibrary().getNilObject();
}

@Specialization
public RubyNilClass cast(long value) {
return getContext().getCoreLibrary().getNilObject();
}

@Specialization
public RubyNilClass cast(double value) {
return getContext().getCoreLibrary().getNilObject();
}

@Specialization
public RubyNilClass cast(BigInteger value) {
return getContext().getCoreLibrary().getNilObject();
}

@Specialization
public RubyArray cast(RubyArray array) {
return array;
}

@Specialization
public RubyNilClass doNil(RubyNilClass nil) {
public RubyNilClass cast(RubyNilClass nil) {
return nil;
}

@Specialization
public Object doObject(VirtualFrame frame, Object object) {
@Specialization(guards = {"!isNil", "!isArray"})
public Object cast(VirtualFrame frame, RubyBasicObject object) {
notDesignedForCompilation();

final Object result = toArrayNode.call(frame, object, "to_ary", null, new Object[]{});
Expand Down
Expand Up @@ -39,11 +39,6 @@ public boolean doNil(@SuppressWarnings("unused") RubyNilClass nil) {
return false;
}

@Specialization
public boolean doFalse(@SuppressWarnings("unused") RubyFalseClass falseObject) {
return false;
}

@Specialization
public boolean doBoolean(boolean value) {
return value;
Expand Down Expand Up @@ -75,8 +70,7 @@ public boolean doBasicObject(RubyBasicObject object) {
}

protected boolean neitherNilNorFalse(RubyBasicObject object) {
return object != getContext().getCoreLibrary().getNilObject() &&
object != getContext().getCoreLibrary().getFalseObject();
return object != getContext().getCoreLibrary().getNilObject();
}

@Override
Expand Down
81 changes: 0 additions & 81 deletions core/src/main/java/org/jruby/truffle/nodes/cast/BoxingNode.java

This file was deleted.

49 changes: 25 additions & 24 deletions core/src/main/java/org/jruby/truffle/nodes/cast/SplatCastNode.java
Expand Up @@ -18,6 +18,7 @@
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyNilClass;

/**
* Splat as used to cast a value to an array if it isn't already, as in {@code *value}.
Expand Down Expand Up @@ -50,43 +51,43 @@ public SplatCastNode(SplatCastNode prev) {
protected abstract RubyNode getChild();

@Specialization
public RubyArray doArray(RubyArray array) {
return array;
public RubyArray splat(RubyNilClass nil) {
switch (nilBehavior) {
case EMPTY_ARRAY:
return new RubyArray(getContext().getCoreLibrary().getArrayClass());

case ARRAY_WITH_NIL:
return RubyArray.fromObject(getContext().getCoreLibrary().getArrayClass(), getContext().getCoreLibrary().getNilObject());

default: {
CompilerAsserts.neverPartOfCompilation();
throw new UnsupportedOperationException();
}
}
}

@Specialization
public RubyArray doObject(VirtualFrame frame, Object object) {
notDesignedForCompilation();

if (object == getContext().getCoreLibrary().getNilObject()) {
switch (nilBehavior) {
case EMPTY_ARRAY:
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
public RubyArray splat(RubyArray array) {
return array;
}

case ARRAY_WITH_NIL:
return RubyArray.fromObject(getContext().getCoreLibrary().getArrayClass(), getContext().getCoreLibrary().getNilObject());
@Specialization(guards = {"!isNil", "!isArray"})
public RubyArray splat(VirtualFrame frame, Object object) {
notDesignedForCompilation();

default: {
CompilerAsserts.neverPartOfCompilation();
throw new UnsupportedOperationException();
}
}
} else if (object instanceof RubyArray) {
return (RubyArray) object;
} else {
if (toA.doesRespondTo(frame, "to_a", object)) {
final Object array = toA.call(frame, object, "to_a", null);

if (array instanceof RubyArray) {
return (RubyArray) array;
}

// TODO(CS): surely this is an error? to_a returned something that was not an array

return RubyArray.fromObject(getContext().getCoreLibrary().getArrayClass(), object);
} else {
return RubyArray.fromObject(getContext().getCoreLibrary().getArrayClass(), object);
}
}

@Override
public void executeVoid(VirtualFrame frame) {
getChild().executeVoid(frame);
}

}

0 comments on commit a90e5d0

Please sign in to comment.