Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast-path conversions between Temporal types #1484

Merged
merged 19 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a72a2ea
Fix duplicate variable in ToTemporalMonthDay
ptomato Apr 12, 2021
7badec0
Fix typo in spec text
ptomato Apr 8, 2021
fb6c7ea
Fast-path conversion of a Temporal object to Temporal.Calendar
ptomato Apr 8, 2021
fbccf7d
SQUASH - tests for previous commit
ptomato Apr 8, 2021
2eeb35b
Fast-path conversion of Temporal.ZonedDateTime to Temporal.Instant
ptomato Apr 9, 2021
76600a4
SQUASH - tests for previous commit
ptomato Apr 9, 2021
7d642fb
Fast-path conversion of Temporal.PlainDateTime to Temporal.PlainDate
ptomato Apr 9, 2021
3d41e55
SQUASH - tests for previous commit
ptomato Apr 9, 2021
0e1e9c4
Fast-path conversion of Temporal.PlainDate to Temporal.PlainDateTime
ptomato Apr 10, 2021
79082d7
SQUASH - tests for previous commit
ptomato Apr 10, 2021
e1784d2
Fast-path conversion of Temporal.PlainDateTime to Temporal.PlainTime
ptomato Apr 10, 2021
19cd26f
Fast-path conversion of Temporal.ZonedDateTime to Temporal.TimeZone
ptomato Apr 12, 2021
b752adc
Fast-path conversion of Temporal.PlainDate to relativeTo object
ptomato Apr 12, 2021
b8ddefc
Fast-path accesses to `calendar` property
ptomato Apr 13, 2021
5cc8c2d
Fast-path conversion of Temporal.ZonedDateTime to PlainDate/Time/Date…
ptomato Apr 13, 2021
983e314
Short-circuit checks for `calendar` and `timeZone` in with()
ptomato Apr 13, 2021
88ebc41
Make types consistent that Calendar methods are be called with
ptomato Apr 13, 2021
44525f5
Fix faulty assertion of IsValidTimeZoneName
ptomato Apr 13, 2021
12dab2e
Add internal create operations to avoid observable effects
ptomato Apr 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,71 +88,73 @@ export class Calendar {
}
year(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].year(date);
}
month(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (ES.IsTemporalMonthDay(date)) throw new TypeError('use monthCode on PlainMonthDay instead');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].month(date);
}
monthCode(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date) && !ES.IsTemporalMonthDay(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].monthCode(date);
}
day(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalMonthDay(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].day(date);
}
era(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].era(date);
}
eraYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].eraYear(date);
}
dayOfWeek(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].dayOfWeek(date);
}
dayOfYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].dayOfYear(date);
}
weekOfYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].weekOfYear(date);
}
daysInWeek(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].daysInWeek(date);
}
daysInMonth(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].daysInMonth(date);
}
daysInYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].daysInYear(date);
}
monthsInYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date);
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].monthsInYear(date);
}
inLeapYear(date) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
if (!ES.IsTemporalYearMonth(date)) date = ES.ToTemporalDate(date);
return impl[GetSlot(this, CALENDAR_ID)].inLeapYear(date);
}
toString() {
Expand All @@ -163,7 +165,7 @@ export class Calendar {
return ES.ToString(this);
}
static from(item) {
return ES.CalendarFrom(item);
return ES.ToTemporalCalendar(item);
}
}

Expand All @@ -177,17 +179,15 @@ impl['iso8601'] = {
fields = resolveNonLunisolarMonth(fields);
let { year, month, day } = fields;
({ year, month, day } = ES.RegulateISODate(year, month, day, overflow));
const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%');
return new TemporalPlainDate(year, month, day, calendar);
return ES.CreateTemporalDate(year, month, day, calendar);
},
yearMonthFromFields(fields, options, calendar) {
const overflow = ES.ToTemporalOverflow(options);
fields = ES.PrepareTemporalFields(fields, [['month', undefined], ['monthCode', undefined], ['year']]);
fields = resolveNonLunisolarMonth(fields);
let { year, month } = fields;
({ year, month } = ES.RegulateISOYearMonth(year, month, overflow));
const TemporalPlainYearMonth = GetIntrinsic('%Temporal.PlainYearMonth%');
return new TemporalPlainYearMonth(year, month, calendar, /* referenceISODay */ 1);
return ES.CreateTemporalYearMonth(year, month, calendar, /* referenceISODay = */ 1);
},
monthDayFromFields(fields, options, calendar) {
const overflow = ES.ToTemporalOverflow(options);
Expand All @@ -205,8 +205,7 @@ impl['iso8601'] = {
fields = resolveNonLunisolarMonth(fields);
let { month, day, year } = fields;
({ month, day } = ES.RegulateISODate(useYear ? year : referenceISOYear, month, day, overflow));
const TemporalPlainMonthDay = GetIntrinsic('%Temporal.PlainMonthDay%');
return new TemporalPlainMonthDay(month, day, calendar, referenceISOYear);
return ES.CreateTemporalMonthDay(month, day, calendar, referenceISOYear);
},
fields(fields) {
return fields;
Expand All @@ -226,8 +225,7 @@ impl['iso8601'] = {
let month = GetSlot(date, ISO_MONTH);
let day = GetSlot(date, ISO_DAY);
({ year, month, day } = ES.AddISODate(year, month, day, years, months, weeks, days, overflow));
const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%');
return new TemporalPlainDate(year, month, day, calendar);
return ES.CreateTemporalDate(year, month, day, calendar);
},
dateUntil(one, two, largestUnit) {
return ES.DifferenceISODate(
Expand Down Expand Up @@ -1780,8 +1778,7 @@ const nonIsoGeneralImpl = {
['year', undefined]
]);
const { year, month, day } = this.helper.calendarToIsoDate(fields, overflow, cache);
const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%');
const result = new TemporalPlainDate(year, month, day, calendar);
const result = ES.CreateTemporalDate(year, month, day, calendar);
cache.setObject(result);
return result;
},
Expand All @@ -1797,8 +1794,7 @@ const nonIsoGeneralImpl = {
['year', undefined]
]);
const { year, month, day } = this.helper.calendarToIsoDate({ ...fields, day: 1 }, overflow, cache);
const TemporalPlainYearMonth = GetIntrinsic('%Temporal.PlainYearMonth%');
const result = new TemporalPlainYearMonth(year, month, calendar, /* referenceISODay = */ day);
const result = ES.CreateTemporalYearMonth(year, month, calendar, /* referenceISODay = */ day);
cache.setObject(result);
return result;
},
Expand All @@ -1819,8 +1815,7 @@ const nonIsoGeneralImpl = {
]);
const { year, month, day } = this.helper.monthDayFromFields(fields, overflow, cache);
// `year` is a reference year where this month/day exists in this calendar
const TemporalPlainMonthDay = GetIntrinsic('%Temporal.PlainMonthDay%');
const result = new TemporalPlainMonthDay(month, day, calendar, /* referenceISOYear */ year);
const result = ES.CreateTemporalMonthDay(month, day, calendar, /* referenceISOYear = */ year);
cache.setObject(result);
return result;
},
Expand Down Expand Up @@ -1855,8 +1850,7 @@ const nonIsoGeneralImpl = {
const added = this.helper.addCalendar(calendarDate, { years, months, weeks, days }, overflow, cache);
const isoAdded = this.helper.calendarToIsoDate(added, 'constrain', cache);
const { year, month, day } = isoAdded;
const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%');
const newTemporalObject = new TemporalPlainDate(year, month, day, calendar);
const newTemporalObject = ES.CreateTemporalDate(year, month, day, calendar);
// The new object's cache starts with the cache of the old object
const newCache = new OneObjectCache(cache);
newCache.setObject(newTemporalObject);
Expand Down
Loading