Skip to content

Commit

Permalink
Editorial: Simplify ToTemporalTimeRecord
Browse files Browse the repository at this point in the history
ToTemporalTimeRecord can be simplified quite a lot and doesn't need to
call the more complicated PrepareTemporalFields. This change is
unobservable, but it's preparation for refactoring PrepareTemporalFields
in #2925.
  • Loading branch information
ptomato committed Aug 21, 2024
1 parent 54f79f6 commit b7e008d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
20 changes: 9 additions & 11 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const NumberMaxSafeInteger = Number.MAX_SAFE_INTEGER;
const ObjectCreate = Object.create;
const ObjectDefineProperty = Object.defineProperty;
const ObjectEntries = Object.entries;
const ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
const SetPrototypeHas = Set.prototype.has;
const StringCtor = String;
const StringFromCharCode = String.fromCharCode;
Expand Down Expand Up @@ -1136,8 +1135,7 @@ export function PrepareTemporalFields(
fields,
requiredFields,
extraFieldDescriptors = [],
duplicateBehaviour = 'throw',
{ emptySourceErrorMessage = 'no supported properties found' } = {}
duplicateBehaviour = 'throw'
) {
const result = ObjectCreate(null);
let any = false;
Expand Down Expand Up @@ -1186,7 +1184,7 @@ export function PrepareTemporalFields(
previousProperty = property;
}
if (requiredFields === 'partial' && !any) {
throw new TypeError(emptySourceErrorMessage);
throw new TypeError('no supported properties found');
}
return result;
}
Expand Down Expand Up @@ -1232,19 +1230,19 @@ export function PrepareCalendarFields(calendarRec, bag, calendarFieldNames, nonC

export function ToTemporalTimeRecord(bag, completeness = 'complete') {
const fields = ['hour', 'microsecond', 'millisecond', 'minute', 'nanosecond', 'second'];
const partial = PrepareTemporalFields(bag, fields, 'partial', undefined, undefined, {
emptySourceErrorMessage: 'invalid time-like'
});
const result = {};
let any = false;
const result = ObjectCreate(null);
for (let index = 0; index < fields.length; index++) {
const field = fields[index];
const valueDesc = ObjectGetOwnPropertyDescriptor(partial, field);
if (valueDesc !== undefined) {
result[field] = valueDesc.value;
const value = bag[field];
if (value !== undefined) {
result[field] = ToIntegerWithTruncation(value);
any = true;
} else if (completeness === 'complete') {
result[field] = 0;
}
}
if (!any) throw new TypeError('invalid time-like');
return result;
}

Expand Down
53 changes: 27 additions & 26 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -816,35 +816,36 @@ <h1>
</dl>
<emu-alg>
1. If _completeness_ is not present, set _completeness_ to ~complete~.
1. Let _partial_ be ? PrepareTemporalFields(_temporalTimeLike_, « *"hour"*, *"microsecond"*, *"millisecond"*, *"minute"*, *"nanosecond"*, *"second"* », ~partial~).
1. If _completeness_ is ~complete~, then
1. Let _result_ be a new TemporalTimeLike Record with each field set to 0.
1. Else,
1. Let _result_ be a new TemporalTimeLike Record with each field set to *undefined*.
1. Let _hourDesc_ be OrdinaryGetOwnProperty(_partial_, *"hour"*).
1. If _hourDesc_ is not *undefined*, then
1. Assert: _hourDesc_ is a data Property Descriptor.
1. Set _result_.[[Hour]] to ℝ(_hourDesc_.[[Value]]).
1. Let _minuteDesc_ be OrdinaryGetOwnProperty(_partial_, *"minute"*).
1. If _minuteDesc_ is not *undefined*, then
1. Assert: _minuteDesc_ is a data Property Descriptor.
1. Set _result_.[[Minute]] to ℝ(_minuteDesc_.[[Value]]).
1. Let _secondDesc_ be OrdinaryGetOwnProperty(_partial_, *"second"*).
1. If _secondDesc_ is not *undefined*, then
1. Assert: _secondDesc_ is a data Property Descriptor.
1. Set _result_.[[Second]] to ℝ(_secondDesc_.[[Value]]).
1. Let _millisecondDesc_ be OrdinaryGetOwnProperty(_partial_, *"millisecond"*).
1. If _millisecondDesc_ is not *undefined*, then
1. Assert: _millisecondDesc_ is a data Property Descriptor.
1. Set _result_.[[Millisecond]] to ℝ(_millisecondDesc_.[[Value]]).
1. Let _microsecondDesc_ be OrdinaryGetOwnProperty(_partial_, *"microsecond"*).
1. If _microsecondDesc_ is not *undefined*, then
1. Assert: _microsecondDesc_ is a data Property Descriptor.
1. Set _result_.[[Microsecond]] to ℝ(_microsecondDesc_.[[Value]]).
1. Let _nanosecondDesc_ be OrdinaryGetOwnProperty(_partial_, *"nanosecond"*).
1. If _nanosecondDesc_ is not *undefined*, then
1. Assert: _nanosecondDesc_ is a data Property Descriptor.
1. Set _result_.[[Nanosecond]] to ℝ(_nanosecondDesc_.[[Value]]).
1. Let _result_ be a new TemporalTimeLike Record with each field set to ~unset~.
1. Let _any_ be *false*.
1. Let _hour_ be ? Get(_temporalTimeLike_, *"hour"*).
1. If _hour_ is not *undefined*, then
1. Set _result_.[[Hour]] to ? ToIntegerWithTruncation(_hour_).
1. Set _any_ to *true*.
1. Let _microsecond_ be ? Get(_temporalTimeLike_, *"microsecond"*).
1. If _microsecond_ is not *undefined*, then
1. Set _result_.[[Microsecond]] to ? ToIntegerWithTruncation(_microsecond_).
1. Set _any_ to *true*.
1. Let _millisecond_ be ? Get(_temporalTimeLike_, *"millisecond"*).
1. If _millisecond_ is not *undefined*, then
1. Set _result_.[[Millisecond]] to ? ToIntegerWithTruncation(_millisecond_).
1. Set _any_ to *true*.
1. Let _minute_ be ? Get(_temporalTimeLike_, *"minute"*).
1. If _minute_ is not *undefined*, then
1. Set _result_.[[Minute]] to ? ToIntegerWithTruncation(_minute_).
1. Set _any_ to *true*.
1. Let _nanosecond_ be ? Get(_temporalTimeLike_, *"nanosecond"*).
1. If _nanosecond_ is not *undefined*, then
1. Set _result_.[[Nanosecond]] to ? ToIntegerWithTruncation(_nanosecond_).
1. Set _any_ to *true*.
1. Let _second_ be ? Get(_temporalTimeLike_, *"second"*).
1. If _second_ is not *undefined*, then
1. Set _result_.[[Second]] to ? ToIntegerWithTruncation(_second_).
1. Set _any_ to *true*.
1. If _any_ is *false*, throw a *TypeError* exception.
1. Return _result_.
</emu-alg>

Expand Down

0 comments on commit b7e008d

Please sign in to comment.