Skip to content

Commit

Permalink
Remove TimeZoneToString abstract operation
Browse files Browse the repository at this point in the history
Also remove %Temporal.TimeZone.prototype.toString% intrinsic. Similarly to
the previous commit, these are not needed because toString() is a required
method of the TimeZone protocol, so there should always be a toString()
there to call.

See: #1294
  • Loading branch information
ptomato committed Jan 20, 2021
1 parent 873c8bd commit 6ac8f53
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 57 deletions.
15 changes: 4 additions & 11 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,13 +1509,13 @@ export const ES = ObjectAssign({}, ES2020, {
return ES.TimeZoneFrom(identifier);
},
TimeZoneCompare: (one, two) => {
const tz1 = ES.TimeZoneToString(one);
const tz2 = ES.TimeZoneToString(two);
const tz1 = ES.ToString(one);
const tz2 = ES.ToString(two);
return tz1 < tz2 ? -1 : tz1 > tz2 ? 1 : 0;
},
TimeZoneEquals: (one, two) => {
const tz1 = ES.TimeZoneToString(one);
const tz2 = ES.TimeZoneToString(two);
const tz1 = ES.ToString(one);
const tz2 = ES.ToString(two);
return tz1 === tz2;
},
TemporalDateTimeToDate: (dateTime) => {
Expand Down Expand Up @@ -1578,13 +1578,6 @@ export const ES = ObjectAssign({}, ES2020, {
}
return ES.Call(getInstantFor, timeZone, [dateTime, { disambiguation }]);
},
TimeZoneToString: (timeZone) => {
let toString = timeZone.toString;
if (toString === undefined) {
toString = GetIntrinsic('%Temporal.TimeZone.prototype.toString%');
}
return ES.ToString(ES.Call(toString, timeZone));
},
ISOYearString: (year) => {
let yearString;
if (year < 1000 || year > 9999) {
Expand Down
6 changes: 3 additions & 3 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function formatRange(a, b) {
}
const { instant: aa, formatter: aformatter, timeZone: atz } = extractOverrides(a, this);
const { instant: bb, formatter: bformatter, timeZone: btz } = extractOverrides(b, this);
if (atz && btz && ES.TimeZoneToString(atz) !== ES.TimeZoneToString(btz)) {
if (atz && btz && !ES.TimeZoneEquals(atz, btz)) {
throw new RangeError('cannot format range between different time zones');
}
if (aa && bb && aformatter && bformatter && aformatter === bformatter) {
Expand All @@ -131,7 +131,7 @@ function formatRangeToParts(a, b) {
}
const { instant: aa, formatter: aformatter, timeZone: atz } = extractOverrides(a, this);
const { instant: bb, formatter: bformatter, timeZone: btz } = extractOverrides(b, this);
if (atz && btz && ES.TimeZoneToString(atz) !== ES.TimeZoneToString(btz)) {
if (atz && btz && !ES.TimeZoneEquals(atz, btz)) {
throw new RangeError('cannot format range between different time zones');
}
if (aa && bb && aformatter && bformatter && aformatter === bformatter) {
Expand Down Expand Up @@ -369,7 +369,7 @@ function extractOverrides(temporalObj, main) {
}

let timeZone = GetSlot(temporalObj, TIME_ZONE);
const objTimeZone = ES.TimeZoneToString(timeZone);
const objTimeZone = ES.ToString(timeZone);
if (main[TZ_GIVEN] && main[TZ_GIVEN] !== objTimeZone) {
throw new RangeError(`timeZone option ${main[TZ_GIVEN]} doesn't match actual time zone ${objTimeZone}`);
}
Expand Down
5 changes: 2 additions & 3 deletions polyfill/lib/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class TimeZone {
}
}
get id() {
return ES.TimeZoneToString(this);
return ES.ToString(this);
}
getOffsetNanosecondsFor(instant) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
Expand Down Expand Up @@ -220,7 +220,7 @@ export class TimeZone {
return String(GetSlot(this, TIMEZONE_ID));
}
toJSON() {
return ES.TimeZoneToString(this);
return ES.ToString(this);
}
static from(item) {
if (ES.Type(item) === 'Object') {
Expand All @@ -241,4 +241,3 @@ DefineIntrinsic('Temporal.TimeZone.prototype.getPlainDateTimeFor', TimeZone.prot
DefineIntrinsic('Temporal.TimeZone.prototype.getInstantFor', TimeZone.prototype.getInstantFor);
DefineIntrinsic('Temporal.TimeZone.prototype.getOffsetNanosecondsFor', TimeZone.prototype.getOffsetNanosecondsFor);
DefineIntrinsic('Temporal.TimeZone.prototype.getOffsetStringFor', TimeZone.prototype.getOffsetStringFor);
DefineIntrinsic('Temporal.TimeZone.prototype.toString', TimeZone.prototype.toString);
2 changes: 1 addition & 1 deletion polyfill/lib/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ function zonedDateTimeToString(
);
let result = `${year}-${month}-${day}T${hour}:${minute}${seconds}`;
if (showOffset !== 'never') result += ES.GetOffsetStringFor(tz, instant);
if (showTimeZone !== 'never') result += `[${ES.TimeZoneToString(tz)}]`;
if (showTimeZone !== 'never') result += `[${tz}]`;
const calendarID = ES.ToString(GetSlot(zdt, CALENDAR));
result += ES.FormatCalendarAnnotation(calendarID, showCalendar);
return result;
Expand Down
4 changes: 1 addition & 3 deletions polyfill/test/TimeZone/prototype/id/no-toString.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ Object.defineProperty(timeZone, "toString", {
});

const descriptor = Object.getOwnPropertyDescriptor(Temporal.TimeZone.prototype, "id");
const result = descriptor.get.call(timeZone);
assert.sameValue(result, "UTC");

assert.throws(TypeError, () => descriptor.get.call(timeZone));
assert.compareArray(actual, expected);
16 changes: 12 additions & 4 deletions polyfill/test/TimeZone/prototype/id/plain-custom-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"get timeZone[@@toPrimitive]",
"get timeZone.toString",
"call timeZone.toString",
];
Expand All @@ -19,17 +20,24 @@ const timeZone = new Proxy({
},
}, {
has(target, property) {
actual.push(`has timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('has timeZone[@@toPrimitive]');
} else {
actual.push(`has timeZone.${property}`);
}
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('get timeZone[@@toPrimitive]');
} else {
actual.push(`get timeZone.${property}`);
}
return target[property];
},
});

const descriptor = Object.getOwnPropertyDescriptor(Temporal.TimeZone.prototype, "id");
const result = descriptor.get.call(timeZone);
assert.sameValue(result, "time zone");

assert.compareArray(actual, expected);
assert.sameValue(result, "time zone");
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = ['get timeZone.toString'];
const expected = [
'get timeZone[@@toPrimitive]',
'get timeZone.toString',
'get timeZone.valueOf',
];

const timeZone = new Proxy(
{
toString: undefined
},
{
has(target, property) {
actual.push(`has timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('has timeZone[@@toPrimitive]');
} else {
actual.push(`has timeZone.${property}`);
}
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('get timeZone[@@toPrimitive]');
} else {
actual.push(`get timeZone.${property}`);
}
return target[property];
}
}
Expand Down
18 changes: 15 additions & 3 deletions polyfill/test/TimeZone/prototype/toJSON/tostring-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = ['get timeZone.toString', 'call timeZone.toString'];
const expected = [
'get timeZone[@@toPrimitive]',
'get timeZone.toString',
'call timeZone.toString',
];

const timeZone = new Proxy(
{
Expand All @@ -18,11 +22,19 @@ const timeZone = new Proxy(
},
{
has(target, property) {
actual.push(`has timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('has timeZone[@@toPrimitive]');
} else {
actual.push(`has timeZone.${property}`);
}
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('get timeZone[@@toPrimitive]');
} else {
actual.push(`get timeZone.${property}`);
}
return target[property];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = ['get timeZone.toString'];
const expected = [
'get timeZone[@@toPrimitive]',
'get timeZone.toString',
'get timeZone.valueOf',
];

const timeZone = new Proxy(
{
toString: undefined
},
{
has(target, property) {
actual.push(`has timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('has timeZone[@@toPrimitive]');
} else {
actual.push(`has timeZone.${property}`);
}
return property in target;
},
get(target, property) {
actual.push(`get timeZone.${property}`);
if (property === Symbol.toPrimitive) {
actual.push('get timeZone[@@toPrimitive]');
} else {
actual.push(`get timeZone.${property}`);
}
return target[property];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ esid: sec-temporal.timezone.prototype.tojson
const tz = Temporal.TimeZone.from('UTC');
tz.toString = undefined;

const result = tz.toJSON();
assert.sameValue(result, 'UTC');
assert.throws(TypeError, () => tz.toJSON());
2 changes: 1 addition & 1 deletion spec/intl.html
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ <h1>PartitionDateTimePattern ( _dateTimeFormat_, _x_ )</h1>
1. <ins>Let _calendar_ be ? ToString(_x_.[[Calendar]]).</ins>
1. <ins>If _calendar_ is not *"iso8601"* and not equal to _dateTimeFormat_.[[Calendar]], then</ins>
1. <ins>Throw a *RangeError* exception.</ins>
1. <ins>Let _timeZone_ be ? TimeZoneToString(_x_.[[TimeZone]]).</ins>
1. <ins>Let _timeZone_ be ? ToString(_x_.[[TimeZone]]).</ins>
1. <ins>If _dateTimeFormat_.[[TimeZone]] is not equal to DefaultTimeZone(), and _timeZone_ is not equal to _dateTimeFormat_.[[TimeZone]], then</ins>
1. <ins>Throw a *RangeError* exception.</ins>
1. <ins>Let _instant_ be ? CreateTemporalInstant(_x_.[[Nanoseconds]]).</ins>
Expand Down
24 changes: 6 additions & 18 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ <h1>get Temporal.TimeZone.prototype.id</h1>
</p>
<emu-alg>
1. Let _timeZone_ be the *this* value.
1. Return ? TimeZoneToString(_timeZone_).
1. Return ? ToString(_timeZone_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -297,9 +297,6 @@ <h1>Temporal.TimeZone.prototype.toString ( )</h1>
1. Perform ? RequireInternalSlot(_timeZone_, [[InitializedTemporalTimeZone]]).
1. Return _timeZone_.[[Identifier]].
</emu-alg>
<p>
This function is the <dfn>%Temporal.TimeZone.prototype.toString%</dfn> intrinsic object.
</p>
</emu-clause>

<emu-clause id="sec-temporal.timezone.prototype.tojson">
Expand All @@ -309,7 +306,7 @@ <h1>Temporal.TimeZone.prototype.toJSON ( )</h1>
</p>
<emu-alg>
1. Let _timeZone_ be the *this* value.
1. Return ? TimeZoneToString(_timeZone_).
1. Return ? ToString(_timeZone_).
</emu-alg>
</emu-clause>
</emu-clause>
Expand Down Expand Up @@ -593,24 +590,15 @@ <h1>GetPossibleInstantsFor ( _timeZone_, _dateTime_ )</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-timezonetostring" aoid="TimeZoneToString">
<h1>TimeZoneToString ( _timeZone_ )</h1>
<emu-alg>
1. Let _toString_ be ? Get(_timeZone_, *"toString"*).
1. If _toString_ is *undefined*, set _toString_ to %Temporal.TimeZone.prototype.toString%.
1. Return be ? ToString(? Call(_toString_, _timeZone_, « »)).
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-comparetimezone" aoid="CompareTimeZone">
<h1>CompareTimeZone ( _one_, _two_ )</h1>
<p>
The abstract operation CompareTimeZone lexicographically compares the
results of calling `toString()` on its arguments.
</p>
<emu-alg>
1. Let _calendarOne_ be ? TimeZoneToString(_one_).
1. Let _calendarTwo_ be ? TimeZoneToString(_two_).
1. Let _timeZoneOne_ be ? ToString(_one_).
1. Let _timeZoneTwo_ be ? ToString(_two_).
1. Let _r_ be the result of performing Abstract Relational Comparison _timeZoneOne_ &lt; _timeZoneTwo_.
1. If _r_ is *true*, return −1.
1. Let _r_ be the result of performing Abstract Relational Comparison _timeZoneTwo_ &lt; _timeZoneOne_.
Expand All @@ -626,8 +614,8 @@ <h1>TimeZoneEquals ( _one_, _two_ )</h1>
calling `toString()` on its arguments are equal.
</p>
<emu-alg>
1. Let _timeZoneOne_ be ? TimeZoneToString(_one_).
1. Let _timeZoneTwo_ be ? TimeZoneToString(_two_).
1. Let _timeZoneOne_ be ? ToString(_one_).
1. Let _timeZoneTwo_ be ? ToString(_two_).
1. If _timeZoneOne_ is _timeZoneTwo_, return *true*.
1. Return *false*.
</emu-alg>
Expand Down
5 changes: 3 additions & 2 deletions spec/zoneddatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1343,10 +1343,11 @@ <h1>TemporalZonedDateTimeToString ( _zonedDateTime_, _precision_, _showCalendar_
1. If _showTimeZone_ is *"never"*, then
1. Let _timeZoneString_ be the empty String.
1. Else,
1. Let _timeZoneString_ be ? TimeZoneToString(_timeZone_).
1. Let _timeZoneID_ be ? ToString(_timeZone_).
1. Let _timeZoneString_ be the string-concatenation of the code unit 0x005B (LEFT SQUARE BRACKET), _timeZoneID_, and the code unit 0x005D (RIGHT SQUARE BRACKET).
1. Let _calendarID_ be ? ToString(_zonedDateTime_.[[Calendar]]).
1. Let _calendarString_ be ? FormatCalendarAnnotation(_calendarID_, _showCalendar_).
1. Return the string-concatenation of _dateTimeString_, _offsetString_, the code unit 0x005B (LEFT SQUARE BRACKET), _timeZoneString_, the code unit 0x005D (RIGHT SQUARE BRACKET), and _calendarString_.
1. Return the string-concatenation of _dateTimeString_, _offsetString_, _timeZoneString_, and _calendarString_.
</emu-alg>
</emu-clause>

Expand Down

0 comments on commit 6ac8f53

Please sign in to comment.