mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 20:28:11 +00:00
LibJS: Move string-parsing code into ToTemporalDurationRecord
This is an editorial change in the Temporal spec.
See: a68b97b
This commit is contained in:
parent
87fb005a8d
commit
0d06f3655f
7 changed files with 51 additions and 67 deletions
|
@ -113,56 +113,53 @@ ThrowCompletionOr<TimeDurationRecord> create_time_duration_record(GlobalObject&
|
|||
// 7.5.8 ToTemporalDuration ( item ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalduration
|
||||
ThrowCompletionOr<Duration*> to_temporal_duration(GlobalObject& global_object, Value item)
|
||||
{
|
||||
DurationRecord result;
|
||||
|
||||
// 1. If Type(item) is Object, then
|
||||
if (item.is_object()) {
|
||||
// a. If item has an [[InitializedTemporalDuration]] internal slot, then
|
||||
if (is<Duration>(item.as_object())) {
|
||||
// i. Return item.
|
||||
return &static_cast<Duration&>(item.as_object());
|
||||
}
|
||||
// b. Let result be ? ToTemporalDurationRecord(item).
|
||||
result = TRY(to_temporal_duration_record(global_object, item.as_object()));
|
||||
// 1. If Type(item) is Object and item has an [[InitializedTemporalDuration]] internal slot, then
|
||||
if (item.is_object() && is<Duration>(item.as_object())) {
|
||||
// a. Return item.
|
||||
return &static_cast<Duration&>(item.as_object());
|
||||
}
|
||||
// 2. Else,
|
||||
else {
|
||||
// a. Let string be ? ToString(item).
|
||||
auto string = TRY(item.to_string(global_object));
|
||||
|
||||
// b. Let result be ? ParseTemporalDurationString(string).
|
||||
result = TRY(parse_temporal_duration_string(global_object, string));
|
||||
}
|
||||
// 2. Let result be ? ToTemporalDurationRecord(item).
|
||||
auto result = TRY(to_temporal_duration_record(global_object, item));
|
||||
|
||||
// 3. Return ! CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
|
||||
return MUST(create_temporal_duration(global_object, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds));
|
||||
}
|
||||
|
||||
// 7.5.9 ToTemporalDurationRecord ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldurationrecord
|
||||
ThrowCompletionOr<DurationRecord> to_temporal_duration_record(GlobalObject& global_object, Object const& temporal_duration_like)
|
||||
ThrowCompletionOr<DurationRecord> to_temporal_duration_record(GlobalObject& global_object, Value temporal_duration_like)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. If temporalDurationLike has an [[InitializedTemporalDuration]] internal slot, then
|
||||
if (is<Duration>(temporal_duration_like)) {
|
||||
auto& duration = static_cast<Duration const&>(temporal_duration_like);
|
||||
// 1. If Type(temporalDurationLike) is not Object, then
|
||||
if (!temporal_duration_like.is_object()) {
|
||||
// a. Let string be ? ToString(temporalDurationLike).
|
||||
auto string = TRY(temporal_duration_like.to_string(global_object));
|
||||
|
||||
// b. Return ? ParseTemporalDurationString(string).
|
||||
return parse_temporal_duration_string(global_object, string);
|
||||
}
|
||||
|
||||
// 2. If temporalDurationLike has an [[InitializedTemporalDuration]] internal slot, then
|
||||
if (is<Duration>(temporal_duration_like.as_object())) {
|
||||
auto& duration = static_cast<Duration const&>(temporal_duration_like.as_object());
|
||||
|
||||
// a. Return ! CreateDurationRecord(temporalDurationLike.[[Years]], temporalDurationLike.[[Months]], temporalDurationLike.[[Weeks]], temporalDurationLike.[[Days]], temporalDurationLike.[[Hours]], temporalDurationLike.[[Minutes]], temporalDurationLike.[[Seconds]], temporalDurationLike.[[Milliseconds]], temporalDurationLike.[[Microseconds]], temporalDurationLike.[[Nanoseconds]]).
|
||||
return create_duration_record(duration.years(), duration.months(), duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds(), duration.microseconds(), duration.nanoseconds());
|
||||
}
|
||||
|
||||
// 2. Let result be a new Duration Record.
|
||||
// 3. Let result be a new Duration Record.
|
||||
auto result = DurationRecord {};
|
||||
|
||||
// 3. Let any be false.
|
||||
// 4. Let any be false.
|
||||
auto any = false;
|
||||
|
||||
// 4. For each row of Table 7, except the header row, in table order, do
|
||||
// 5. For each row of Table 7, except the header row, in table order, do
|
||||
for (auto& [field, property] : temporal_duration_like_properties<DurationRecord, double>(vm)) {
|
||||
// a. Let prop be the Property Name value of the current row.
|
||||
|
||||
// b. Let val be ? Get(temporalDurationLike, prop).
|
||||
auto value = TRY(temporal_duration_like.get(property));
|
||||
auto value = TRY(temporal_duration_like.as_object().get(property));
|
||||
|
||||
// c. If val is undefined, then
|
||||
if (value.is_undefined()) {
|
||||
|
@ -182,19 +179,19 @@ ThrowCompletionOr<DurationRecord> to_temporal_duration_record(GlobalObject& glob
|
|||
}
|
||||
}
|
||||
|
||||
// 5. If any is false, then
|
||||
// 6. If any is false, then
|
||||
if (!any) {
|
||||
// a. Throw a TypeError exception.
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
|
||||
}
|
||||
|
||||
// 6. If ! IsValidDuration(result.[[Years]], result.[[Months]], result.[[Weeks]] result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]) is false, then
|
||||
// 7. If ! IsValidDuration(result.[[Years]], result.[[Months]], result.[[Weeks]] result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]) is false, then
|
||||
if (!is_valid_duration(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)) {
|
||||
// a. Throw a RangeError exception.
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
|
||||
}
|
||||
|
||||
// 7. Return result.
|
||||
// 8. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1608,23 +1605,10 @@ ThrowCompletionOr<DurationRecord> to_limited_temporal_duration(GlobalObject& glo
|
|||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
DurationRecord duration;
|
||||
// 1. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, temporal_duration_like));
|
||||
|
||||
// 1. If Type(temporalDurationLike) is not Object, then
|
||||
if (!temporal_duration_like.is_object()) {
|
||||
// a. Let str be ? ToString(temporalDurationLike).
|
||||
auto str = TRY(temporal_duration_like.to_string(global_object));
|
||||
|
||||
// b. Let duration be ? ParseTemporalDurationString(str).
|
||||
duration = TRY(parse_temporal_duration_string(global_object, str));
|
||||
}
|
||||
// 2. Else,
|
||||
else {
|
||||
// a. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
duration = TRY(to_temporal_duration_record(global_object, temporal_duration_like.as_object()));
|
||||
}
|
||||
|
||||
// 3. For each row of Table 7, except the header row, in table order, do
|
||||
// 2. For each row of Table 7, except the header row, in table order, do
|
||||
for (auto& [field, property] : temporal_duration_like_properties<DurationRecord, double>(vm)) {
|
||||
// a. Let prop be the Property Name value of the current row.
|
||||
|
||||
|
@ -1638,7 +1622,7 @@ ThrowCompletionOr<DurationRecord> to_limited_temporal_duration(GlobalObject& glo
|
|||
}
|
||||
}
|
||||
|
||||
// 4. Return duration.
|
||||
// 3. Return duration.
|
||||
return duration;
|
||||
}
|
||||
|
||||
|
|
|
@ -140,7 +140,7 @@ ThrowCompletionOr<DateDurationRecord> create_date_duration_record(GlobalObject&,
|
|||
TimeDurationRecord create_time_duration_record(double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
|
||||
ThrowCompletionOr<TimeDurationRecord> create_time_duration_record(GlobalObject&, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
|
||||
ThrowCompletionOr<Duration*> to_temporal_duration(GlobalObject&, Value item);
|
||||
ThrowCompletionOr<DurationRecord> to_temporal_duration_record(GlobalObject&, Object const& temporal_duration_like);
|
||||
ThrowCompletionOr<DurationRecord> to_temporal_duration_record(GlobalObject&, Value temporal_duration_like);
|
||||
i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
|
||||
bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
|
||||
StringView default_temporal_largest_unit(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds);
|
||||
|
|
|
@ -298,8 +298,8 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::add)
|
|||
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
||||
auto* duration = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Set other to ? ToLimitedTemporalDuration(other, « »).
|
||||
auto other = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Set other to ? ToTemporalDurationRecord(other).
|
||||
auto other = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
@ -321,8 +321,8 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::subtract)
|
|||
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
||||
auto* duration = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Set other to ? ToLimitedTemporalDuration(other, « »).
|
||||
auto other = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Set other to ? ToTemporalDurationRecord(other).
|
||||
auto other = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
|
|
@ -465,8 +465,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::add)
|
|||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto* date_time = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
@ -491,8 +491,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::subtract)
|
|||
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
|
||||
auto* date_time = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
|
|
@ -145,8 +145,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::add)
|
|||
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
|
||||
auto* temporal_time = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, temporal_duration_like, {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, temporal_duration_like));
|
||||
|
||||
// 4. Let result be ! AddTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
|
||||
auto result = add_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds);
|
||||
|
@ -167,8 +167,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::subtract)
|
|||
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
|
||||
auto* temporal_time = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, temporal_duration_like, {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, temporal_duration_like));
|
||||
|
||||
// 4. Let result be ! AddTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], −duration.[[Hours]], −duration.[[Minutes]], −duration.[[Seconds]], −duration.[[Milliseconds]], −duration.[[Microseconds]], −duration.[[Nanoseconds]]).
|
||||
auto result = add_time(temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), -duration.hours, -duration.minutes, -duration.seconds, -duration.milliseconds, -duration.microseconds, -duration.nanoseconds);
|
||||
|
|
|
@ -243,8 +243,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::add)
|
|||
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto* year_month = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Let balanceResult be ? BalanceDuration(duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "day").
|
||||
auto balance_result = TRY(balance_duration(global_object, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, *js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)duration.nanoseconds)), "day"sv));
|
||||
|
@ -315,8 +315,8 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::subtract)
|
|||
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto* year_month = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Let balanceResult be ? BalanceDuration(duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "day").
|
||||
auto balance_result = TRY(balance_duration(global_object, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, *js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)duration.nanoseconds)), "day"sv));
|
||||
|
|
|
@ -899,8 +899,8 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::add)
|
|||
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
|
||||
auto* zoned_date_time = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
@ -925,8 +925,8 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::subtract)
|
|||
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
|
||||
auto* zoned_date_time = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
|
||||
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
|
||||
// 3. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
|
||||
auto duration = TRY(to_temporal_duration_record(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue