1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:37:35 +00:00

LibJS: Move common code into RejectObjectWithCalendarOrTimeZone

This is an editorial change in the Temporal spec.

See: 5654fe0
This commit is contained in:
Linus Groh 2021-11-04 16:16:35 +01:00
parent bb60629d3b
commit d129e1ff29
5 changed files with 51 additions and 51 deletions

View file

@ -224,7 +224,7 @@
M(TemporalInvalidTimeZoneName, "Invalid time zone name") \
M(TemporalInvalidUnitRange, "Invalid unit range, {} is larger than {}") \
M(TemporalMissingOptionsObject, "Required options object is missing or undefined") \
M(TemporalPlainTimeWithArgumentMustNotHave, "Argument must not have a defined {} property") \
M(TemporalObjectMustNotHave, "Object must not have a defined {} property") \
M(TemporalPropertyMustBeFinite, "Property must not be Infinity") \
M(TemporalPropertyMustBePositiveInteger, "Property must be a positive integer") \
M(ThisHasNotBeenInitialized, "|this| has not been initialized") \

View file

@ -623,8 +623,8 @@ Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit)
return 1000;
}
// 13.26 RejectTemporalCalendarType ( object ), https://tc39.es/proposal-temporal/#sec-temporal-rejecttemporalcalendartype
ThrowCompletionOr<void> reject_temporal_calendar_type(GlobalObject& global_object, Object& object)
// 13.26 RejectObjectWithCalendarOrTimeZone ( object ), https://tc39.es/proposal-temporal/#sec-temporal-rejectobjectwithcalendarortimezone
ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(GlobalObject& global_object, Object& object)
{
auto& vm = global_object.vm();
@ -633,7 +633,25 @@ ThrowCompletionOr<void> reject_temporal_calendar_type(GlobalObject& global_objec
// 2. If object has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
if (is<PlainDate>(object) || is<PlainDateTime>(object) || is<PlainMonthDay>(object) || is<PlainTime>(object) || is<PlainYearMonth>(object) || is<ZonedDateTime>(object)) {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalPlainTimeWithArgumentMustNotHave, "calendar or timeZone");
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalObjectMustNotHave, "calendar or timeZone");
}
// 3. Let calendarProperty be ? Get(object, "calendar").
auto calendar_property = TRY(object.get(vm.names.calendar));
// 4. If calendarProperty is not undefined, then
if (!calendar_property.is_undefined()) {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalObjectMustNotHave, "calendar");
}
// 5. Let timeZoneProperty be ? Get(object, "timeZone").
auto time_zone_property = TRY(object.get(vm.names.timeZone));
// 6. If timeZoneProperty is not undefined, then
if (!time_zone_property.is_undefined()) {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalObjectMustNotHave, "timeZone");
}
return {};

View file

@ -102,7 +102,7 @@ ThrowCompletionOr<void> validate_temporal_unit_range(GlobalObject&, StringView l
String larger_of_two_temporal_units(StringView, StringView);
ThrowCompletionOr<Object*> merge_largest_unit_option(GlobalObject&, Object& options, String largest_unit);
Optional<u16> maximum_temporal_duration_rounding_increment(StringView unit);
ThrowCompletionOr<void> reject_temporal_calendar_type(GlobalObject&, Object&);
ThrowCompletionOr<void> reject_object_with_calendar_or_time_zone(GlobalObject&, Object&);
String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);
double constrain_to_range(double x, double minimum, double maximum);
i64 round_number_to_increment(double, u64 increment, StringView rounding_mode);

View file

@ -195,76 +195,58 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::with)
auto& temporal_time_like = temporal_time_like_argument.as_object();
// 4. Perform ? RejectTemporalCalendarType(temporalTimeLike).
TRY(reject_temporal_calendar_type(global_object, temporal_time_like));
// 4. Perform ? RejectObjectWithCalendarOrTimeZone(temporalTimeLike).
TRY(reject_object_with_calendar_or_time_zone(global_object, temporal_time_like));
// 5. Let calendarProperty be ? Get(temporalTimeLike, "calendar").
auto calendar_property = TRY(temporal_time_like.get(vm.names.calendar));
// 6. If calendarProperty is not undefined, then
if (!calendar_property.is_undefined()) {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalPlainTimeWithArgumentMustNotHave, "calendar");
}
// 7. Let timeZoneProperty be ? Get(temporalTimeLike, "timeZone").
auto time_zone_property = TRY(temporal_time_like.get(vm.names.timeZone));
// 8. If timeZoneProperty is not undefined, then
if (!time_zone_property.is_undefined()) {
// a. Throw a TypeError exception.
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalPlainTimeWithArgumentMustNotHave, "timeZone");
}
// 9. Let partialTime be ? ToPartialTime(temporalTimeLike).
// 5. Let partialTime be ? ToPartialTime(temporalTimeLike).
auto partial_time = TRY(to_partial_time(global_object, temporal_time_like));
// 10. Set options to ? GetOptionsObject(options).
// 6. Set options to ? GetOptionsObject(options).
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
// 11. Let overflow be ? ToTemporalOverflow(options).
// 7. Let overflow be ? ToTemporalOverflow(options).
auto overflow = TRY(to_temporal_overflow(global_object, *options));
// 12. If partialTime.[[Hour]] is not undefined, then
// 8. If partialTime.[[Hour]] is not undefined, then
// a. Let hour be partialTime.[[Hour]].
// 13. Else,
// 9. Else,
// a. Let hour be temporalTime.[[ISOHour]].
auto hour = partial_time.hour.value_or(temporal_time->iso_hour());
// 14. If partialTime.[[Minute]] is not undefined, then
// 10. If partialTime.[[Minute]] is not undefined, then
// a. Let minute be partialTime.[[Minute]].
// 15. Else,
// 11. Else,
// a. Let minute be temporalTime.[[ISOMinute]].
auto minute = partial_time.minute.value_or(temporal_time->iso_minute());
// 16. If partialTime.[[Second]] is not undefined, then
// 12. If partialTime.[[Second]] is not undefined, then
// a. Let second be partialTime.[[Second]].
// 17. Else,
// 13. Else,
// a. Let second be temporalTime.[[ISOSecond]].
auto second = partial_time.second.value_or(temporal_time->iso_second());
// 18. If partialTime.[[Millisecond]] is not undefined, then
// 14. If partialTime.[[Millisecond]] is not undefined, then
// a. Let millisecond be partialTime.[[Millisecond]].
// 19. Else,
// 15. Else,
// a. Let millisecond be temporalTime.[[ISOMillisecond]].
auto millisecond = partial_time.millisecond.value_or(temporal_time->iso_millisecond());
// 20. If partialTime.[[Microsecond]] is not undefined, then
// 16. If partialTime.[[Microsecond]] is not undefined, then
// a. Let microsecond be partialTime.[[Microsecond]].
// 21. Else,
// 17. Else,
// a. Let microsecond be temporalTime.[[ISOMicrosecond]].
auto microsecond = partial_time.microsecond.value_or(temporal_time->iso_microsecond());
// 22. If partialTime.[[Nanosecond]] is not undefined, then
// 18. If partialTime.[[Nanosecond]] is not undefined, then
// a. Let nanosecond be partialTime.[[Nanosecond]].
// 23. Else,
// 19. Else,
// a. Let nanosecond be temporalTime.[[ISONanosecond]].
auto nanosecond = partial_time.nanosecond.value_or(temporal_time->iso_nanosecond());
// 24. Let result be ? RegulateTime(hour, minute, second, millisecond, microsecond, nanosecond, overflow).
// 20. Let result be ? RegulateTime(hour, minute, second, millisecond, microsecond, nanosecond, overflow).
auto result = TRY(regulate_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, overflow));
// 25. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
// 21. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
return TRY(create_temporal_time(global_object, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond));
}

View file

@ -46,7 +46,7 @@ describe("correct behaviour", () => {
new Temporal.PlainTime().with({
calendar: undefined,
});
}).not.toThrowWithMessage(TypeError, "Argument must not have a defined calendar property");
}).not.toThrowWithMessage(TypeError, "Object must not have a defined calendar property");
});
test("argument can have a timeZone property as long as it's undefined", () => {
@ -54,7 +54,7 @@ describe("correct behaviour", () => {
new Temporal.PlainTime().with({
timeZone: undefined,
});
}).not.toThrowWithMessage(TypeError, "Argument must not have a defined timeZone property");
}).not.toThrowWithMessage(TypeError, "Object must not have a defined timeZone property");
});
});
@ -140,12 +140,12 @@ describe("errors", () => {
new Temporal.PlainTime().with({
calendar: null,
});
}).toThrowWithMessage(TypeError, "Argument must not have a defined calendar property");
}).toThrowWithMessage(TypeError, "Object must not have a defined calendar property");
expect(() => {
new Temporal.PlainTime().with({
calendar: 1,
});
}).toThrowWithMessage(TypeError, "Argument must not have a defined calendar property");
}).toThrowWithMessage(TypeError, "Object must not have a defined calendar property");
});
test("argument must not have a defined timeZone property", () => {
@ -153,12 +153,12 @@ describe("errors", () => {
new Temporal.PlainTime().with({
timeZone: null,
});
}).toThrowWithMessage(TypeError, "Argument must not have a defined timeZone property");
}).toThrowWithMessage(TypeError, "Object must not have a defined timeZone property");
expect(() => {
new Temporal.PlainTime().with({
timeZone: 1,
});
}).toThrowWithMessage(TypeError, "Argument must not have a defined timeZone property");
}).toThrowWithMessage(TypeError, "Object must not have a defined timeZone property");
});
test("error when getting calendar", () => {
@ -187,7 +187,7 @@ describe("errors", () => {
new Temporal.PlainTime().with(new typeWithCalendar(1, 1, 1));
}).toThrowWithMessage(
TypeError,
"Argument must not have a defined calendar or timeZone property"
"Object must not have a defined calendar or timeZone property"
);
}
@ -196,7 +196,7 @@ describe("errors", () => {
new Temporal.PlainTime().with(new typeWithCalendar(1, 1));
}).toThrowWithMessage(
TypeError,
"Argument must not have a defined calendar or timeZone property"
"Object must not have a defined calendar or timeZone property"
);
}
@ -204,7 +204,7 @@ describe("errors", () => {
new Temporal.PlainTime().with(new Temporal.ZonedDateTime(1n, {}));
}).toThrowWithMessage(
TypeError,
"Argument must not have a defined calendar or timeZone property"
"Object must not have a defined calendar or timeZone property"
);
});
});