Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7fcefac71b21
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 50b46ac0413b
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Aug 20, 2015

  1. Copy the full SHA
    317e15e View commit details
  2. Copy the full SHA
    50b46ac View commit details
28 changes: 12 additions & 16 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/TimeNodes.java
Original file line number Diff line number Diff line change
@@ -18,23 +18,14 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.objects.AllocateObjectNode;
import org.jruby.truffle.nodes.objects.AllocateObjectNodeGen;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.layouts.TimeLayoutImpl;

@CoreClass(name = "Time")
public abstract class TimeNodes {

private static final DateTime ZERO = new DateTime(0);

public static DateTime getDateTime(DynamicObject time) {
return Layouts.TIME.getDateTime(time);
}

public static DynamicObject createRubyTime(DynamicObject timeClass, DateTime dateTime, Object offset) {
return Layouts.TIME.createTime(Layouts.CLASS.getInstanceFactory(timeClass), dateTime, offset);
}

// We need it to copy the internal data for a call to Kernel#clone.
@CoreMethod(names = "initialize_copy", required = 1)
public abstract static class InitializeCopyNode extends CoreMethodArrayArgumentsNode {
@@ -45,7 +36,7 @@ public InitializeCopyNode(RubyContext context, SourceSection sourceSection) {

@Specialization(guards = "isRubyTime(from)")
public Object initializeCopy(DynamicObject self, DynamicObject from) {
Layouts.TIME.setDateTime(self, getDateTime(from));
Layouts.TIME.setDateTime(self, Layouts.TIME.getDateTime(from));
Layouts.TIME.setOffset(self, Layouts.TIME.getOffset(from));
return self;
}
@@ -63,8 +54,8 @@ public InternalGMTNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public boolean internalGMT(DynamicObject time) {
return Layouts.TIME.getOffset(time) == nil() &&
(getDateTime(time).getZone().equals(DateTimeZone.UTC) ||
getDateTime(time).getZone().getOffset(getDateTime(time).getMillis()) == 0);
(Layouts.TIME.getDateTime(time).getZone().equals(DateTimeZone.UTC) ||
Layouts.TIME.getDateTime(time).getZone().getOffset(Layouts.TIME.getDateTime(time).getMillis()) == 0);
}
}

@@ -83,7 +74,7 @@ public InternalSetGMTNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public boolean internalSetGMT(DynamicObject time, boolean isGMT) {
if (isGMT) {
Layouts.TIME.setDateTime(time, getDateTime(time).withZone(DateTimeZone.UTC));
Layouts.TIME.setDateTime(time, Layouts.TIME.getDateTime(time).withZone(DateTimeZone.UTC));
} else {
// Do nothing I guess - we can't change it to another zone, as what zone would that be?
}
@@ -127,13 +118,18 @@ public Object internalSetOffset(DynamicObject time, Object offset) {
@CoreMethod(names = "allocate", constructor = true)
public abstract static class AllocateNode extends CoreMethodArrayArgumentsNode {

private static final DateTime ZERO = new DateTime(0);

@Child private AllocateObjectNode allocateObjectNode;

public AllocateNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
allocateObjectNode = AllocateObjectNodeGen.create(context, sourceSection, null, null);
}

@Specialization
public DynamicObject allocate(DynamicObject rubyClass) {
return createRubyTime(rubyClass, ZERO, getContext().getCoreLibrary().getNilObject());
return allocateObjectNode.allocate(rubyClass, ZERO, getContext().getCoreLibrary().getNilObject());
}

}
Original file line number Diff line number Diff line change
@@ -19,12 +19,12 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.jruby.truffle.nodes.RubyGuards;
import org.jruby.truffle.nodes.core.TimeNodes;
import org.jruby.truffle.nodes.objects.AllocateObjectNode;
import org.jruby.truffle.nodes.objects.AllocateObjectNodeGen;
import org.jruby.truffle.nodes.time.ReadTimeZoneNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.layouts.TimeLayoutImpl;
import org.jruby.util.RubyDateFormatter;

/**
@@ -36,16 +36,18 @@ public abstract class TimePrimitiveNodes {
public static abstract class TimeSNowPrimitiveNode extends RubiniusPrimitiveNode {

@Child private ReadTimeZoneNode readTimeZoneNode;
@Child private AllocateObjectNode allocateObjectNode;

public TimeSNowPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
readTimeZoneNode = new ReadTimeZoneNode(context, sourceSection);
allocateObjectNode = AllocateObjectNodeGen.create(context, sourceSection, null, null);
}

@Specialization
public DynamicObject timeSNow(VirtualFrame frame, DynamicObject timeClass) {
// TODO CS 4-Mar-15 whenever we get time we have to convert lookup and time zone to a string and look it up - need to cache somehow...
return TimeNodes.createRubyTime(timeClass, now((DynamicObject) readTimeZoneNode.execute(frame)), nil());
return allocateObjectNode.allocate(timeClass, now((DynamicObject) readTimeZoneNode.execute(frame)), nil());
}

@TruffleBoundary
@@ -59,14 +61,16 @@ private DateTime now(DynamicObject timeZone) {
@RubiniusPrimitive(name = "time_s_dup", needsSelf = false)
public static abstract class TimeSDupPrimitiveNode extends RubiniusPrimitiveNode {

@Child private AllocateObjectNode allocateObjectNode;

public TimeSDupPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
allocateObjectNode = AllocateObjectNodeGen.create(context, sourceSection, null, null);
}

@Specialization
public DynamicObject timeSDup(DynamicObject other) {
final DynamicObject time = TimeNodes.createRubyTime(getContext().getCoreLibrary().getTimeClass(), TimeNodes.getDateTime(other), Layouts.TIME.getOffset(other));
return time;
return allocateObjectNode.allocate(getContext().getCoreLibrary().getTimeClass(), Layouts.TIME.getDateTime(other), Layouts.TIME.getOffset(other));
}

}
@@ -75,24 +79,26 @@ public DynamicObject timeSDup(DynamicObject other) {
public static abstract class TimeSSpecificPrimitiveNode extends RubiniusPrimitiveNode {

@Child private ReadTimeZoneNode readTimeZoneNode;
@Child private AllocateObjectNode allocateObjectNode;

public TimeSSpecificPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
readTimeZoneNode = new ReadTimeZoneNode(context, sourceSection);
allocateObjectNode = AllocateObjectNodeGen.create(context, sourceSection, null, null);
}

@Specialization(guards = { "isUTC", "isNil(offset)" })
public DynamicObject timeSSpecificUTC(long seconds, int nanoseconds, boolean isUTC, Object offset) {
// TODO(CS): overflow checks needed?
final long milliseconds = getMillis(seconds, nanoseconds);
return TimeNodes.createRubyTime(getContext().getCoreLibrary().getTimeClass(), time(milliseconds), nil());
return allocateObjectNode.allocate(getContext().getCoreLibrary().getTimeClass(), time(milliseconds), nil());
}

@Specialization(guards = { "!isUTC", "isNil(offset)" })
public DynamicObject timeSSpecific(VirtualFrame frame, long seconds, int nanoseconds, boolean isUTC, Object offset) {
// TODO(CS): overflow checks needed?
final long milliseconds = getMillis(seconds, nanoseconds);
return TimeNodes.createRubyTime(getContext().getCoreLibrary().getTimeClass(), localtime(milliseconds, (DynamicObject) readTimeZoneNode.execute(frame)), offset);
return allocateObjectNode.allocate(getContext().getCoreLibrary().getTimeClass(), localtime(milliseconds, (DynamicObject) readTimeZoneNode.execute(frame)), offset);
}

private long getMillis(long seconds, int nanoseconds) {
@@ -127,7 +133,7 @@ public TimeSecondsPrimitiveNode(RubyContext context, SourceSection sourceSection

@Specialization
public long timeSeconds(DynamicObject time) {
return TimeNodes.getDateTime(time).getMillis() / 1_000;
return Layouts.TIME.getDateTime(time).getMillis() / 1_000;
}

}
@@ -141,7 +147,7 @@ public TimeUSecondsPrimitiveNode(RubyContext context, SourceSection sourceSectio

@Specialization
public long timeUSeconds(DynamicObject time) {
return TimeNodes.getDateTime(time).getMillisOfSecond() * 1_000L;
return Layouts.TIME.getDateTime(time).getMillisOfSecond() * 1_000L;
}

}
@@ -159,7 +165,7 @@ public TimeDecomposePrimitiveNode(RubyContext context, SourceSection sourceSecti
@Specialization
public DynamicObject timeDecompose(VirtualFrame frame, DynamicObject time) {
CompilerDirectives.transferToInterpreter();
final DateTime dateTime = TimeNodes.getDateTime(time);
final DateTime dateTime = Layouts.TIME.getDateTime(time);
final int sec = dateTime.getSecondOfMinute();
final int min = dateTime.getMinuteOfHour();
final int hour = dateTime.getHourOfDay();
@@ -203,7 +209,7 @@ public TimeStrftimePrimitiveNode(RubyContext context, SourceSection sourceSectio
public DynamicObject timeStrftime(DynamicObject time, DynamicObject format) {
final RubyDateFormatter rdf = getContext().getRuntime().getCurrentContext().getRubyDateFormatter();
// TODO CS 15-Feb-15 ok to just pass nanoseconds as 0?
return createString(rdf.formatToByteList(rdf.compilePattern(Layouts.STRING.getByteList(format), false), TimeNodes.getDateTime(time), 0, null));
return createString(rdf.formatToByteList(rdf.compilePattern(Layouts.STRING.getByteList(format), false), Layouts.TIME.getDateTime(time), 0, null));
}

}
@@ -212,10 +218,12 @@ public DynamicObject timeStrftime(DynamicObject time, DynamicObject format) {
public static abstract class TimeSFromArrayPrimitiveNode extends RubiniusPrimitiveNode {

@Child ReadTimeZoneNode readTimeZoneNode;
@Child private AllocateObjectNode allocateObjectNode;

public TimeSFromArrayPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
readTimeZoneNode = new ReadTimeZoneNode(context, sourceSection);
allocateObjectNode = AllocateObjectNodeGen.create(context, sourceSection, null, null);
}

@Specialization
@@ -261,7 +269,7 @@ private DynamicObject buildTime(VirtualFrame frame, DynamicObject timeClass, int

if (isdst == -1) {
final DateTime dateTime = new DateTime(year, month, mday, hour, min, sec, nsec / 1_000_000, zone);
return TimeNodes.createRubyTime(timeClass, dateTime, utcoffset);
return allocateObjectNode.allocate(timeClass, dateTime, utcoffset);
} else {
throw new UnsupportedOperationException(String.format("%s %s %s %s", isdst, fromutc, utcoffset, utcoffset.getClass()));
}
@@ -288,7 +296,7 @@ public TimeNSecondsPrimitiveNode(RubyContext context, SourceSection sourceSectio

@Specialization
public long timeNSeconds(DynamicObject time) {
return TimeNodes.getDateTime(time).getMillisOfSecond() * 1_000_000L;
return Layouts.TIME.getDateTime(time).getMillisOfSecond() * 1_000_000L;
}

}
@@ -303,7 +311,7 @@ public TimeSetNSecondsPrimitiveNode(RubyContext context, SourceSection sourceSec
@TruffleBoundary
@Specialization
public long timeSetNSeconds(DynamicObject time, int nanoseconds) {
Layouts.TIME.setDateTime(time, TimeNodes.getDateTime(time).withMillisOfSecond(nanoseconds / 1_000_000));
Layouts.TIME.setDateTime(time, Layouts.TIME.getDateTime(time).withMillisOfSecond(nanoseconds / 1_000_000));
return nanoseconds;
}

@@ -336,7 +344,7 @@ public Object timeUTCOffset(DynamicObject time) {
if (offset != nil()) {
return offset;
} else {
return TimeNodes.getDateTime(time).getZone().getOffset(TimeNodes.getDateTime(time).getMillis()) / 1_000;
return Layouts.TIME.getDateTime(time).getZone().getOffset(Layouts.TIME.getDateTime(time).getMillis()) / 1_000;
}
}

Original file line number Diff line number Diff line change
@@ -409,7 +409,7 @@ public void run(PropertyModel property, boolean last) {
}

stream.println(" assert factory != null;");
stream.println(" CompilerAsserts.compilationConstant(factory);");
stream.println(" CompilerAsserts.partialEvaluationConstant(factory);");
stream.printf(" assert creates%s(factory);\n", layout.getName());

for (PropertyModel property : layout.getAllNonShapeProperties()) {
@@ -490,7 +490,7 @@ public void run(PropertyModel property, boolean last) {
}

stream.println(" assert factory != null;");
stream.println(" CompilerAsserts.compilationConstant(factory);");
stream.println(" CompilerAsserts.partialEvaluationConstant(factory);");
stream.printf(" assert creates%s(factory);\n", layout.getName());

for (PropertyModel property : layout.getAllNonShapeProperties()) {