Skip to content

Commit

Permalink
Normative: Validate required methods of TimeZone protocol
Browse files Browse the repository at this point in the history
Checking whether an object implements the TimeZone protocol is now done by
means of HasProperty operations for each of the required methods unless
the object already has the TimeZone brand.

Discussion:
#2104 (comment)

See: #2104
  • Loading branch information
ptomato committed Apr 10, 2023
1 parent f431988 commit 9852b94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 6 additions & 2 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1956,11 +1956,15 @@ export const ES = ObjectAssign({}, ES2022, {
return result;
},

ObjectImplementsTemporalTimeZoneProtocol: (object) => {
if (ES.IsTemporalTimeZone(object)) return true;
return 'getOffsetNanosecondsFor' in object && 'getPossibleInstantsFor' in object && 'id' in object;
},
ToTemporalTimeZoneSlotValue: (temporalTimeZoneLike) => {
if (ES.Type(temporalTimeZoneLike) === 'Object') {
if (ES.IsTemporalZonedDateTime(temporalTimeZoneLike)) return GetSlot(temporalTimeZoneLike, TIME_ZONE);
if (ES.IsTemporalCalendar(temporalTimeZoneLike)) {
throw new RangeError('Expected a time zone object but received a Temporal.Calendar');
if (!ES.ObjectImplementsTemporalTimeZoneProtocol(temporalTimeZoneLike)) {
throw new TypeError('expected a Temporal.TimeZone or object implementing the Temporal.TimeZone protocol');
}
return temporalTimeZoneLike;
}
Expand Down
24 changes: 23 additions & 1 deletion spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,28 @@ <h1>FormatISOTimeZoneOffsetString ( _offsetNanoseconds_ )</h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-objectimplementstemporaltimezoneprotocol" type="abstract operation">
<h1>
ObjectImplementsTemporalTimeZoneProtocol (
_object_: an Object,
): either a normal completion containing a Boolean, or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>
It determines whether the given _object_ is a Temporal.TimeZone instance or implements the required methods from the Temporal.TimeZone protocol.
For ordinary objects, and some exotic objects, this operation is infallible and will always return a normal completion.
However, if _object_ is a Proxy or has one in its prototype chain, user code may be called.
</dd>
</dl>
<emu-alg>
1. If _object_ has an [[InitializedTemporalTimeZone]] internal slot, return *true*.
1. For each property key _key_ of « *"getOffsetNanosecondsFor"*, *"getPossibleInstantsFor"*, *"id"* », do
1. If ? HasProperty(_object_, _key_) is *false*, return *false*.
1. Return *true*.
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-totemporaltimezoneslotvalue" type="abstract operation">
<h1>
ToTemporalTimeZoneSlotValue (
Expand All @@ -560,7 +582,7 @@ <h1>
1. If Type(_temporalTimeZoneLike_) is Object, then
1. If _temporalTimeZoneLike_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Return _temporalTimeZoneLike_.[[TimeZone]].
1. If _temporalTimeZoneLike_ has an [[InitializedTemporalCalendar]] internal slot, throw a *RangeError* exception.
1. If ? ObjectImplementsTemporalTimeZoneProtocol(_temporalTimeZoneLike_) is *false*, throw a *TypeError* exception.
1. Return _temporalTimeZoneLike_.
1. Let _identifier_ be ? ToString(_temporalTimeZoneLike_).
1. Let _parseResult_ be ? ParseTemporalTimeZoneString(_identifier_).
Expand Down

0 comments on commit 9852b94

Please sign in to comment.