Skip to content

Commit c31255c

Browse files
committed
[GR-22093] LocalTZA depends on time argument now.
PullRequest: js/1443
2 parents 7ac6e86 + 7b3e53b commit c31255c

File tree

5 files changed

+53
-232
lines changed

5 files changed

+53
-232
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/DatePrototypeBuiltins.java

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.oracle.truffle.api.dsl.Specialization;
4747
import com.oracle.truffle.api.interop.InteropLibrary;
4848
import com.oracle.truffle.api.object.DynamicObject;
49-
import com.oracle.truffle.api.profiles.BranchProfile;
5049
import com.oracle.truffle.api.profiles.ConditionProfile;
5150
import com.oracle.truffle.js.builtins.DatePrototypeBuiltinsFactory.JSDateGetDateNodeGen;
5251
import com.oracle.truffle.js.builtins.DatePrototypeBuiltinsFactory.JSDateGetDayNodeGen;
@@ -80,7 +79,6 @@
8079
import com.oracle.truffle.js.builtins.DatePrototypeBuiltinsFactory.JSDateToTimeStringNodeGen;
8180
import com.oracle.truffle.js.builtins.DatePrototypeBuiltinsFactory.JSDateValueOfNodeGen;
8281
import com.oracle.truffle.js.builtins.ObjectPrototypeBuiltins.ObjectOperation;
83-
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
8482
import com.oracle.truffle.js.nodes.access.IsJSObjectNode;
8583
import com.oracle.truffle.js.nodes.access.PropertyGetNode;
8684
import com.oracle.truffle.js.nodes.cast.JSToNumberNode;
@@ -93,7 +91,6 @@
9391
import com.oracle.truffle.js.runtime.Errors;
9492
import com.oracle.truffle.js.runtime.JSArguments;
9593
import com.oracle.truffle.js.runtime.JSContext;
96-
import com.oracle.truffle.js.runtime.JSRealm;
9794
import com.oracle.truffle.js.runtime.JSRuntime;
9895
import com.oracle.truffle.js.runtime.Symbol;
9996
import com.oracle.truffle.js.runtime.builtins.BuiltinEnum;
@@ -547,11 +544,9 @@ protected String doOperation(Object thisDate) {
547544
}
548545

549546
public abstract static class JSDateGetFullYearNode extends JSDateOperation {
550-
@Child protected LocalDayNode localDayNode;
551547

552548
public JSDateGetFullYearNode(JSContext context, JSBuiltin builtin, boolean isUTC) {
553549
super(context, builtin, isUTC);
554-
localDayNode = isUTC ? null : LocalDayNode.create(context);
555550
}
556551

557552
@Specialization
@@ -560,21 +555,15 @@ protected double doOperation(Object thisDate) {
560555
if (isNaN.profile(Double.isNaN(t))) {
561556
return Double.NaN;
562557
}
563-
if (isUTC) {
564-
return JSDate.yearFromTime((long) t);
565-
} else {
566-
int daysAfter1970 = localDayNode.execute((long) t);
567-
return JSDate.yearFromDays(daysAfter1970);
568-
}
558+
t = isUTC ? t : JSDate.localTime(t, getContext());
559+
return JSDate.yearFromTime((long) t);
569560
}
570561
}
571562

572563
public abstract static class JSDateGetYearNode extends JSDateOperation {
573-
@Child protected LocalDayNode localDayNode;
574564

575565
public JSDateGetYearNode(JSContext context, JSBuiltin builtin) {
576566
super(context, builtin, false);
577-
localDayNode = isUTC ? null : LocalDayNode.create(context);
578567
}
579568

580569
@Specialization
@@ -583,17 +572,15 @@ protected double doOperation(Object thisDate) {
583572
if (isNaN.profile(Double.isNaN(t))) {
584573
return Double.NaN;
585574
}
586-
int daysAfter1970 = localDayNode.execute((long) t);
587-
return JSDate.yearFromDays(daysAfter1970) - 1900d;
575+
t = JSDate.localTime(t, getContext());
576+
return JSDate.yearFromTime((long) t) - 1900d;
588577
}
589578
}
590579

591580
public abstract static class JSDateGetMonthNode extends JSDateOperation {
592-
@Child protected LocalDayNode localDayNode;
593581

594582
public JSDateGetMonthNode(JSContext context, JSBuiltin builtin, boolean isUTC) {
595583
super(context, builtin, isUTC);
596-
localDayNode = isUTC ? null : LocalDayNode.create(context);
597584
}
598585

599586
@Specialization
@@ -602,23 +589,15 @@ protected double doOperation(Object thisDate) {
602589
if (Double.isNaN(t)) {
603590
return Double.NaN;
604591
}
605-
if (isUTC) {
606-
return JSDate.monthFromTime(t);
607-
} else {
608-
int daysAfter1970 = localDayNode.execute((long) t);
609-
return JSDate.monthFromDays(daysAfter1970);
610-
}
592+
t = isUTC ? t : JSDate.localTime(t, getContext());
593+
return JSDate.monthFromTime(t);
611594
}
612595
}
613596

614597
public abstract static class JSDateGetDateNode extends JSDateOperation {
615-
private static final int DAYS_FROM_1970_TO_1901 = JSDate.dayFromYear(1901);
616-
private static final int DAYS_FROM_1970_TO_2100 = JSDate.dayFromYear(2100);
617-
@Child protected LocalDayNode localDayNode;
618598

619599
public JSDateGetDateNode(JSContext context, JSBuiltin builtin, boolean isUTC) {
620600
super(context, builtin, isUTC);
621-
localDayNode = isUTC ? null : LocalDayNode.create(context);
622601
}
623602

624603
@Specialization
@@ -627,26 +606,15 @@ protected double doOperation(Object thisDate) {
627606
if (isNaN.profile(Double.isNaN(t))) {
628607
return Double.NaN;
629608
}
630-
if (isUTC) {
631-
return JSDate.dateFromTime(t);
632-
} else {
633-
int daysAfter1970 = localDayNode.execute((long) t);
634-
if (DAYS_FROM_1970_TO_1901 <= daysAfter1970 && daysAfter1970 <= DAYS_FROM_1970_TO_2100) {
635-
// There are regular leap years between 1.1.1901 and 1.1.2100
636-
return JSDate.dateFromDaysRegularLeapYears(daysAfter1970);
637-
} else {
638-
return JSDate.dateFromDays(daysAfter1970);
639-
}
640-
}
609+
t = isUTC ? t : JSDate.localTime(t, getContext());
610+
return JSDate.dateFromTime(t);
641611
}
642612
}
643613

644614
public abstract static class JSDateGetDayNode extends JSDateOperation {
645-
@Child protected LocalDayNode localDayNode;
646615

647616
public JSDateGetDayNode(JSContext context, JSBuiltin builtin, boolean isUTC) {
648617
super(context, builtin, isUTC);
649-
localDayNode = isUTC ? null : LocalDayNode.create(context);
650618
}
651619

652620
@Specialization
@@ -655,14 +623,8 @@ protected double doOperation(Object thisDate) {
655623
if (isNaN.profile(Double.isNaN(t))) {
656624
return Double.NaN;
657625
}
658-
if (isUTC) {
659-
return JSDate.weekDay(t);
660-
} else {
661-
int daysAfter1970 = localDayNode.execute((long) t);
662-
int result = (daysAfter1970 + 4) % 7;
663-
return result >= 0 ? result : (result + 7);
664-
}
665-
626+
t = isUTC ? t : JSDate.localTime(t, getContext());
627+
return JSDate.weekDay(t);
666628
}
667629
}
668630

@@ -962,35 +924,4 @@ protected Object toPrimitive(Object obj, Object hint) {
962924
}
963925
}
964926

965-
public static final class LocalDayNode extends JavaScriptBaseNode {
966-
private final JSContext context;
967-
private final BranchProfile dstNeededProfile = BranchProfile.create();
968-
969-
private LocalDayNode(JSContext context) {
970-
this.context = context;
971-
}
972-
973-
public static LocalDayNode create(JSContext context) {
974-
return new LocalDayNode(context);
975-
}
976-
977-
public int execute(long t) {
978-
JSRealm realm = context.getRealm();
979-
long localNoDST = t + realm.getLocalTZA();
980-
long day = Math.floorDiv(localNoDST, JSDate.MS_PER_DAY);
981-
assert JSRuntime.longIsRepresentableAsInt(day);
982-
int iday = (int) day;
983-
long timeInDay = localNoDST - JSDate.MS_PER_DAY * day;
984-
if (timeInDay < JSDate.MS_PER_DAY - JSDate.MS_MAX_DST) {
985-
// DST offset cannot change the day
986-
return iday;
987-
} else {
988-
dstNeededProfile.enter();
989-
timeInDay += JSDate.daylightSavingTA(realm.getLocalTimeZoneId(), t);
990-
return (timeInDay < JSDate.MS_PER_DAY) ? iday : (iday + 1);
991-
}
992-
}
993-
994-
}
995-
996927
}

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/JSRealm.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@
152152
import com.oracle.truffle.js.runtime.objects.PropertyDescriptor;
153153
import com.oracle.truffle.js.runtime.objects.PropertyProxy;
154154
import com.oracle.truffle.js.runtime.objects.Undefined;
155-
import com.oracle.truffle.js.runtime.util.LocalTimeZoneHolder;
156155
import com.oracle.truffle.js.runtime.util.PrintWriterWrapper;
157156
import com.oracle.truffle.js.runtime.util.TRegexUtil;
158157

@@ -342,9 +341,9 @@ public class JSRealm {
342341
private String staticRegexResultOriginalInputString;
343342

344343
/**
345-
* Local time zone information. Initialized lazily.
344+
* Local time zone ID. Initialized lazily.
346345
*/
347-
@CompilationFinal private LocalTimeZoneHolder localTimeZoneHolder;
346+
@CompilationFinal private ZoneId localTimeZoneId;
348347

349348
public static final long NANOSECONDS_PER_MILLISECOND = 1000000;
350349
private SplittableRandom random;
@@ -1832,8 +1831,8 @@ public boolean patchContext(TruffleLanguage.Env newEnv) {
18321831
addArgumentsFromEnv(newEnv);
18331832

18341833
// Reflect any changes to the timezone option.
1835-
if (localTimeZoneHolder != null) {
1836-
localTimeZoneHolder = getTimeZoneFromEnv();
1834+
if (localTimeZoneId != null) {
1835+
localTimeZoneId = getTimeZoneFromEnv();
18371836
}
18381837
initTimeOffsetAndRandom();
18391838

@@ -2099,33 +2098,25 @@ public void setAgent(JSAgent newAgent) {
20992098
this.agent = newAgent;
21002099
}
21012100

2102-
private LocalTimeZoneHolder getLocalTimeZoneHolder() {
2103-
LocalTimeZoneHolder holder = localTimeZoneHolder;
2104-
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, holder == null)) {
2105-
if (CompilerDirectives.isPartialEvaluationConstant(holder)) {
2101+
public ZoneId getLocalTimeZoneId() {
2102+
ZoneId id = localTimeZoneId;
2103+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, id == null)) {
2104+
if (CompilerDirectives.isPartialEvaluationConstant(id)) {
21062105
CompilerDirectives.transferToInterpreterAndInvalidate();
21072106
}
2108-
holder = getTimeZoneFromEnv();
2109-
localTimeZoneHolder = holder;
2107+
id = getTimeZoneFromEnv();
2108+
localTimeZoneId = id;
21102109
}
2111-
return holder;
2110+
return id;
21122111
}
21132112

21142113
@TruffleBoundary
2115-
private LocalTimeZoneHolder getTimeZoneFromEnv() {
2114+
private ZoneId getTimeZoneFromEnv() {
21162115
OptionValues options = getEnv().getOptions();
21172116
if (JSContextOptions.TIME_ZONE.hasBeenSet(options)) {
2118-
return new LocalTimeZoneHolder(TimeZone.getTimeZone(JSContextOptions.TIME_ZONE.getValue(options)).toZoneId());
2117+
return TimeZone.getTimeZone(JSContextOptions.TIME_ZONE.getValue(options)).toZoneId();
21192118
}
2120-
return new LocalTimeZoneHolder(getEnv().getTimeZone());
2121-
}
2122-
2123-
public final ZoneId getLocalTimeZoneId() {
2124-
return getLocalTimeZoneHolder().localTimeZoneId;
2125-
}
2126-
2127-
public final long getLocalTZA() {
2128-
return getLocalTimeZoneHolder().localTZA;
2119+
return getEnv().getTimeZone();
21292120
}
21302121

21312122
private void initTimeOffsetAndRandom() {

0 commit comments

Comments
 (0)