Skip to content

Commit

Permalink
Use slots in all compare() methods
Browse files Browse the repository at this point in the history
When calendars are added, getters will return the calendar fields, but
we want to use the internal ISO 8601-valued slots. Dates are before,
after, or equal to other dates regardless of which calendar system is
used.
  • Loading branch information
ptomato committed May 19, 2020
1 parent bbed1a5 commit 63d36e9
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 20 deletions.
8 changes: 5 additions & 3 deletions polyfill/lib/date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ export class Date {
}
static compare(one, two) {
if (!ES.IsTemporalDate(one) || !ES.IsTemporalDate(two)) throw new TypeError('invalid Date object');
if (one.year !== two.year) return ES.ComparisonResult(one.year - two.year);
if (one.month !== two.month) return ES.ComparisonResult(one.month - two.month);
if (one.day !== two.day) return ES.ComparisonResult(one.day - two.day);
for (const slot of [YEAR, MONTH, DAY]) {
const val1 = GetSlot(one, slot);
const val2 = GetSlot(two, slot);
if (val1 !== val2) return ES.ComparisonResult(val1 - val2);
}
return ES.ComparisonResult(0);
}
}
Expand Down
14 changes: 5 additions & 9 deletions polyfill/lib/datetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,11 @@ export class DateTime {
}
static compare(one, two) {
if (!ES.IsTemporalDateTime(one) || !ES.IsTemporalDateTime(two)) throw new TypeError('invalid DateTime object');
if (one.year !== two.year) return ES.ComparisonResult(one.year - two.year);
if (one.month !== two.month) return ES.ComparisonResult(one.month - two.month);
if (one.day !== two.day) return ES.ComparisonResult(one.day - two.day);
if (one.hour !== two.hour) return ES.ComparisonResult(one.hour - two.hour);
if (one.minute !== two.minute) return ES.ComparisonResult(one.minute - two.minute);
if (one.second !== two.second) return ES.ComparisonResult(one.second - two.second);
if (one.millisecond !== two.millisecond) return ES.ComparisonResult(one.millisecond - two.millisecond);
if (one.microsecond !== two.microsecond) return ES.ComparisonResult(one.microsecond - two.microsecond);
if (one.nanosecond !== two.nanosecond) return ES.ComparisonResult(one.nanosecond - two.nanosecond);
for (const slot of [YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND]) {
const val1 = GetSlot(one, slot);
const val2 = GetSlot(two, slot);
if (val1 !== val2) return ES.ComparisonResult(val1 - val2);
}
return ES.ComparisonResult(0);
}
}
Expand Down
11 changes: 5 additions & 6 deletions polyfill/lib/time.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,11 @@ export class Time {
}
static compare(one, two) {
if (!ES.IsTemporalTime(one) || !ES.IsTemporalTime(two)) throw new TypeError('invalid Time object');
if (one.hour !== two.hour) return ES.ComparisonResult(one.hour - two.hour);
if (one.minute !== two.minute) return ES.ComparisonResult(one.minute - two.minute);
if (one.second !== two.second) return ES.ComparisonResult(one.second - two.second);
if (one.millisecond !== two.millisecond) return ES.ComparisonResult(one.millisecond - two.millisecond);
if (one.microsecond !== two.microsecond) return ES.ComparisonResult(one.microsecond - two.microsecond);
if (one.nanosecond !== two.nanosecond) return ES.ComparisonResult(one.nanosecond - two.nanosecond);
for (const slot of [HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND]) {
const val1 = GetSlot(one, slot);
const val2 = GetSlot(two, slot);
if (val1 !== val2) return ES.ComparisonResult(val1 - val2);
}
return ES.ComparisonResult(0);
}
}
Expand Down
7 changes: 5 additions & 2 deletions polyfill/lib/yearmonth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,11 @@ export class YearMonth {
}
static compare(one, two) {
if (!ES.IsTemporalYearMonth(one) || !ES.IsTemporalYearMonth(two)) throw new TypeError('invalid YearMonth object');
if (one.year !== two.year) return ES.ComparisonResult(one.year - two.year);
if (one.month !== two.month) return ES.ComparisonResult(one.month - two.month);
for (const slot of [YEAR, MONTH]) {
const val1 = GetSlot(one, slot);
const val2 = GetSlot(two, slot);
if (val1 !== val2) return ES.ComparisonResult(val1 - val2);
}
return ES.ComparisonResult(0);
}
}
Expand Down
24 changes: 24 additions & 0 deletions polyfill/test/Date/constructor/compare/use-internal-slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-comparetemporaldate
---*/

function CustomError() {}

class AvoidGettersDate extends Temporal.Date {
get year() {
throw new CustomError();
}
get month() {
throw new CustomError();
}
get day() {
throw new CustomError();
}
}

const one = new AvoidGettersDate(2000, 5, 2);
const two = new AvoidGettersDate(2006, 3, 25);
assert.sameValue(Temporal.Date.compare(one, two), -1);
42 changes: 42 additions & 0 deletions polyfill/test/DateTime/constructor/compare/use-internal-slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-comparetemporaldatetime
---*/

function CustomError() {}

class AvoidGettersDateTime extends Temporal.DateTime {
get year() {
throw new CustomError();
}
get month() {
throw new CustomError();
}
get day() {
throw new CustomError();
}
get hour() {
throw new CustomError();
}
get minute() {
throw new CustomError();
}
get second() {
throw new CustomError();
}
get millisecond() {
throw new CustomError();
}
get microsecond() {
throw new CustomError();
}
get nanosecond() {
throw new CustomError();
}
}

const one = new AvoidGettersDateTime(2000, 5, 2, 12, 34, 56, 987, 654, 321);
const two = new AvoidGettersDateTime(2006, 3, 25, 6, 54, 32, 123, 456, 789);
assert.sameValue(Temporal.DateTime.compare(one, two), -1);
33 changes: 33 additions & 0 deletions polyfill/test/Time/constructor/compare/use-internal-slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-comparetemporaltime
---*/

function CustomError() {}

class AvoidGettersTime extends Temporal.Time {
get hour() {
throw new CustomError();
}
get minute() {
throw new CustomError();
}
get second() {
throw new CustomError();
}
get millisecond() {
throw new CustomError();
}
get microsecond() {
throw new CustomError();
}
get nanosecond() {
throw new CustomError();
}
}

const one = new AvoidGettersTime(12, 34, 56, 987, 654, 321);
const two = new AvoidGettersTime(6, 54, 32, 123, 456, 789);
assert.sameValue(Temporal.Time.compare(one, two), 1);
21 changes: 21 additions & 0 deletions polyfill/test/YearMonth/constructor/compare/use-internal-slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal-comparetemporalyearmonth
---*/

function CustomError() {}

class AvoidGettersYearMonth extends Temporal.YearMonth {
get year() {
throw new CustomError();
}
get month() {
throw new CustomError();
}
}

const one = new AvoidGettersYearMonth(2000, 5);
const two = new AvoidGettersYearMonth(2006, 3);
assert.sameValue(Temporal.YearMonth.compare(one, two), -1);

0 comments on commit 63d36e9

Please sign in to comment.