Skip to content

Commit

Permalink
Polyfill: Replace 'new PlainX' with 'CreateTemporalX' in internal code
Browse files Browse the repository at this point in the history
The constructors of the Plain types call the
ObjectImplementsTemporalCalendarProtocol AO which performs observable
HasProperty operations. The spec does not say to call the constructors
here, so those operations should not be performed. Instead, match the spec
by calling CreateTemporalDate and CreateTemporalDateTime to create
instances when necessary.
  • Loading branch information
ptomato committed Aug 25, 2023
1 parent b1c699b commit 678fdd1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
20 changes: 14 additions & 6 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,8 +1451,18 @@ export function InterpretISODateTimeOffset(
offsetOpt,
matchMinute
) {
const DateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const dt = new DateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond);
const dt = CreateTemporalDateTime(
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
'iso8601'
);

if (offsetBehaviour === 'wall' || offsetOpt === 'ignore') {
// Simple case: ISO string without a TZ offset (or caller wants to ignore
Expand Down Expand Up @@ -2368,7 +2378,6 @@ export function DisambiguatePossibleInstants(possibleInstants, timeZone, dateTim
switch (disambiguation) {
case 'earlier': {
const calendar = GetSlot(dateTime, CALENDAR);
const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const earlier = AddDateTime(
year,
month,
Expand All @@ -2392,7 +2401,7 @@ export function DisambiguatePossibleInstants(possibleInstants, timeZone, dateTim
-nanoseconds,
undefined
);
const earlierPlainDateTime = new PlainDateTime(
const earlierPlainDateTime = CreateTemporalDateTime(
earlier.year,
earlier.month,
earlier.day,
Expand All @@ -2410,7 +2419,6 @@ export function DisambiguatePossibleInstants(possibleInstants, timeZone, dateTim
// fall through because 'compatible' means 'later' for "spring forward" transitions
case 'later': {
const calendar = GetSlot(dateTime, CALENDAR);
const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const later = AddDateTime(
year,
month,
Expand All @@ -2434,7 +2442,7 @@ export function DisambiguatePossibleInstants(possibleInstants, timeZone, dateTim
nanoseconds,
undefined
);
const laterPlainDateTime = new PlainDateTime(
const laterPlainDateTime = CreateTemporalDateTime(
later.year,
later.month,
later.day,
Expand Down
22 changes: 15 additions & 7 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ES from './ecmascript.mjs';
import { GetIntrinsic } from './intrinsicclass.mjs';
import {
GetSlot,
ISO_YEAR,
Expand Down Expand Up @@ -346,16 +345,25 @@ function sameTemporalType(x, y) {
}

function extractOverrides(temporalObj, main) {
const DateTime = GetIntrinsic('%Temporal.PlainDateTime%');

if (ES.IsTemporalTime(temporalObj)) {
const hour = GetSlot(temporalObj, ISO_HOUR);
const minute = GetSlot(temporalObj, ISO_MINUTE);
const second = GetSlot(temporalObj, ISO_SECOND);
const millisecond = GetSlot(temporalObj, ISO_MILLISECOND);
const microsecond = GetSlot(temporalObj, ISO_MICROSECOND);
const nanosecond = GetSlot(temporalObj, ISO_NANOSECOND);
const datetime = new DateTime(1970, 1, 1, hour, minute, second, millisecond, microsecond, nanosecond, 'iso8601');
const datetime = ES.CreateTemporalDateTime(
1970,
1,
1,
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
'iso8601'
);
return {
instant: ES.GetInstantFor(main[TZ_CANONICAL], datetime, 'compatible'),
formatter: getPropLazy(main, TIME)
Expand All @@ -372,7 +380,7 @@ function extractOverrides(temporalObj, main) {
`cannot format PlainYearMonth with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`
);
}
const datetime = new DateTime(isoYear, isoMonth, referenceISODay, 12, 0, 0, 0, 0, 0, calendar);
const datetime = ES.CreateTemporalDateTime(isoYear, isoMonth, referenceISODay, 12, 0, 0, 0, 0, 0, calendar);
return {
instant: ES.GetInstantFor(main[TZ_CANONICAL], datetime, 'compatible'),
formatter: getPropLazy(main, YM)
Expand All @@ -389,7 +397,7 @@ function extractOverrides(temporalObj, main) {
`cannot format PlainMonthDay with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`
);
}
const datetime = new DateTime(referenceISOYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, calendar);
const datetime = ES.CreateTemporalDateTime(referenceISOYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, calendar);
return {
instant: ES.GetInstantFor(main[TZ_CANONICAL], datetime, 'compatible'),
formatter: getPropLazy(main, MD)
Expand All @@ -404,7 +412,7 @@ function extractOverrides(temporalObj, main) {
if (calendar !== 'iso8601' && calendar !== main[CAL_ID]) {
throw new RangeError(`cannot format PlainDate with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`);
}
const datetime = new DateTime(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, main[CAL_ID]);
const datetime = ES.CreateTemporalDateTime(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, main[CAL_ID]);
return {
instant: ES.GetInstantFor(main[TZ_CANONICAL], datetime, 'compatible'),
formatter: getPropLazy(main, DATE)
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/plaindate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class PlainDate {
withCalendar(calendar) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
calendar = ES.ToTemporalCalendarSlotValue(calendar);
return new PlainDate(GetSlot(this, ISO_YEAR), GetSlot(this, ISO_MONTH), GetSlot(this, ISO_DAY), calendar);
return ES.CreateTemporalDate(GetSlot(this, ISO_YEAR), GetSlot(this, ISO_MONTH), GetSlot(this, ISO_DAY), calendar);
}
add(temporalDurationLike, options = undefined) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/plaindatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class PlainDateTime {
withCalendar(calendar) {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
calendar = ES.ToTemporalCalendarSlotValue(calendar);
return new PlainDateTime(
return ES.CreateTemporalDateTime(
GetSlot(this, ISO_YEAR),
GetSlot(this, ISO_MONTH),
GetSlot(this, ISO_DAY),
Expand Down
5 changes: 2 additions & 3 deletions polyfill/lib/plaintime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as ES from './ecmascript.mjs';
import { DateTimeFormat } from './intl.mjs';
import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass.mjs';
import { MakeIntrinsicClass } from './intrinsicclass.mjs';

import {
ISO_YEAR,
Expand Down Expand Up @@ -279,8 +279,7 @@ export class PlainTime {
const microsecond = GetSlot(this, ISO_MICROSECOND);
const nanosecond = GetSlot(this, ISO_NANOSECOND);

const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const dt = new PlainDateTime(
const dt = ES.CreateTemporalDateTime(
year,
month,
day,
Expand Down
39 changes: 28 additions & 11 deletions polyfill/lib/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,23 @@ export class ZonedDateTime {
get hoursInDay() {
if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver');
const dt = dateTime(this);
const DateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const year = GetSlot(dt, ISO_YEAR);
const month = GetSlot(dt, ISO_MONTH);
const day = GetSlot(dt, ISO_DAY);
const today = new DateTime(year, month, day, 0, 0, 0, 0, 0, 0);
const today = ES.CreateTemporalDateTime(year, month, day, 0, 0, 0, 0, 0, 0, 'iso8601');
const tomorrowFields = ES.AddISODate(year, month, day, 0, 0, 0, 1, 'reject');
const tomorrow = new DateTime(tomorrowFields.year, tomorrowFields.month, tomorrowFields.day, 0, 0, 0, 0, 0, 0);
const tomorrow = ES.CreateTemporalDateTime(
tomorrowFields.year,
tomorrowFields.month,
tomorrowFields.day,
0,
0,
0,
0,
0,
0,
'iso8601'
);
const timeZone = GetSlot(this, TIME_ZONE);
const todayNs = GetSlot(ES.GetInstantFor(timeZone, today, 'compatible'), EPOCHNANOSECONDS);
const tomorrowNs = GetSlot(ES.GetInstantFor(timeZone, tomorrow, 'compatible'), EPOCHNANOSECONDS);
Expand Down Expand Up @@ -253,8 +263,7 @@ export class ZonedDateTime {

calendar = ES.ConsolidateCalendars(GetSlot(this, CALENDAR), calendar);
const timeZone = GetSlot(this, TIME_ZONE);
const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const dt = new PlainDateTime(
const dt = ES.CreateTemporalDateTime(
year,
month,
day,
Expand Down Expand Up @@ -288,8 +297,7 @@ export class ZonedDateTime {
const nanosecond = GetSlot(temporalTime, ISO_NANOSECOND);

const timeZone = GetSlot(this, TIME_ZONE);
const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const dt = new PlainDateTime(
const dt = ES.CreateTemporalDateTime(
year,
month,
day,
Expand Down Expand Up @@ -370,9 +378,19 @@ export class ZonedDateTime {
let microsecond = GetSlot(dt, ISO_MICROSECOND);
let nanosecond = GetSlot(dt, ISO_NANOSECOND);

const DateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const calendar = GetSlot(this, CALENDAR);
const dtStart = new DateTime(GetSlot(dt, ISO_YEAR), GetSlot(dt, ISO_MONTH), GetSlot(dt, ISO_DAY), 0, 0, 0, 0, 0, 0);
const dtStart = ES.CreateTemporalDateTime(
GetSlot(dt, ISO_YEAR),
GetSlot(dt, ISO_MONTH),
GetSlot(dt, ISO_DAY),
0,
0,
0,
0,
0,
0,
'iso8601'
);
const instantStart = ES.GetInstantFor(timeZone, dtStart, 'compatible');
const endNs = ES.AddZonedDateTime(instantStart, timeZone, calendar, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0);
const dayLengthNs = endNs.subtract(GetSlot(instantStart, EPOCHNANOSECONDS));
Expand Down Expand Up @@ -514,9 +532,8 @@ export class ZonedDateTime {
startOfDay() {
if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver');
const dt = dateTime(this);
const DateTime = GetIntrinsic('%Temporal.PlainDateTime%');
const calendar = GetSlot(this, CALENDAR);
const dtStart = new DateTime(
const dtStart = ES.CreateTemporalDateTime(
GetSlot(dt, ISO_YEAR),
GetSlot(dt, ISO_MONTH),
GetSlot(dt, ISO_DAY),
Expand Down

0 comments on commit 678fdd1

Please sign in to comment.