mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:57:44 +00:00
LibJS: Begin using CalendarMethodsRecord for AOs
This begins the process of aligning our implementation with the spec with regard to using CalendarMethodsRecord. The main intent here is to make it much easier to make normative changes to AOs which have been updated to CalendarMethodsRecord. While this does resolve various FIXMEs, many others above need to be added in order to be able to pass through a CalendarMethodsRecord. The use here aligns with what I can gather from the spec of what the arguments to CreateCalendarMethodsRecord should be, but various AOs have been updated so much with other changes it's not completely obvious. Other AOs do not even exist in the latest version of the spec, but we still rely on them. As part of these updates, this commit coincidentally also fixes two PlainDate roundingmode issues seen in test262 - a test of which is also added in test-js. This issue boiled down to what appears to be an observable optimization in the spec, where it can avoid calling dateUntil in certain situations (roundingGranularityIsNoop). However, the main goal here is to make it much easier to fix many more issues in the future :^) since/calendar-dateuntil-called-with-singular-largestunit.js ❌ -> ✅ until/calendar-dateuntil-called-with-singular-largestunit.js ❌ -> ✅
This commit is contained in:
parent
6d4eda0028
commit
bb8dad5bb0
11 changed files with 147 additions and 59 deletions
|
@ -395,24 +395,22 @@ ThrowCompletionOr<PlainDate*> calendar_date_add(VM& vm, Object& calendar, Value
|
|||
}
|
||||
|
||||
// 12.2.7 CalendarDateUntil ( calendar, one, two, options [ , dateUntil ] ), https://tc39.es/proposal-temporal/#sec-temporal-calendardateuntil
|
||||
ThrowCompletionOr<Duration*> calendar_date_until(VM& vm, Object const& calendar, Value one, Value two, Object const& options, FunctionObject const* date_until)
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> calendar_date_until(VM& vm, CalendarMethods const& calendar_record, Value one, Value two, Object const& options)
|
||||
{
|
||||
// 1. Assert: Type(calendar) is Object.
|
||||
// 1. Let duration be ? CalendarMethodsRecordCall(calendarRec, DATE-UNTIL, « one, two, options »).
|
||||
auto duration = TRY(calendar_methods_record_call(vm, calendar_record, CalendarMethod::DateUntil, Vector<Value> { one, two, &options }));
|
||||
|
||||
// 2. If dateUntil is not present, set dateUntil to ? GetMethod(calendar, "dateUntil").
|
||||
if (!date_until)
|
||||
date_until = TRY(Value(&calendar).get_method(vm, vm.names.dateUntil));
|
||||
// 2. If CalendarMethodsRecordIsBuiltin(calendarRec) is true, return duration.
|
||||
if (calendar_methods_record_is_builtin(calendar_record))
|
||||
return verify_cast<Duration>(duration.as_object());
|
||||
|
||||
// 3. Let duration be ? Call(dateUntil, calendar, « one, two, options »).
|
||||
auto duration = TRY(call(vm, date_until ?: js_undefined(), &calendar, one, two, &options));
|
||||
|
||||
// 4. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
||||
// 3. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
|
||||
auto duration_object = TRY(duration.to_object(vm));
|
||||
if (!is<Duration>(*duration_object))
|
||||
return vm.throw_completion<TypeError>(ErrorType::NotAnObjectOfType, "Temporal.Duration");
|
||||
|
||||
// 5. Return duration.
|
||||
return static_cast<Duration*>(duration_object.ptr());
|
||||
// 4. Return duration.
|
||||
return static_cast<Duration&>(duration.as_object());
|
||||
}
|
||||
|
||||
// 12.2.8 CalendarYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryear
|
||||
|
|
|
@ -47,7 +47,7 @@ Calendar* get_iso8601_calendar(VM&);
|
|||
ThrowCompletionOr<Vector<String>> calendar_fields(VM&, Object& calendar, Vector<StringView> const& field_names);
|
||||
ThrowCompletionOr<Object*> calendar_merge_fields(VM&, Object& calendar, Object& fields, Object& additional_fields);
|
||||
ThrowCompletionOr<PlainDate*> calendar_date_add(VM&, Object& calendar, Value date, Duration&, Object* options = nullptr, FunctionObject* date_add = nullptr);
|
||||
ThrowCompletionOr<Duration*> calendar_date_until(VM&, Object const& calendar, Value one, Value two, Object const& options, FunctionObject const* date_until = nullptr);
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> calendar_date_until(VM&, CalendarMethods const&, Value one, Value two, Object const& options);
|
||||
ThrowCompletionOr<double> calendar_year(VM&, Object& calendar, Object& date_like);
|
||||
ThrowCompletionOr<double> calendar_month(VM&, Object& calendar, Object& date_like);
|
||||
ThrowCompletionOr<String> calendar_month_code(VM&, Object& calendar, Object& date_like);
|
||||
|
|
|
@ -865,8 +865,8 @@ ThrowCompletionOr<DateDurationRecord> unbalance_duration_relative(VM& vm, double
|
|||
// b. Let dateAdd be ? GetMethod(calendar, "dateAdd").
|
||||
auto date_add = TRY(Value(calendar).get_method(vm, vm.names.dateAdd));
|
||||
|
||||
// FIXME: This AO is out of date, this is no longer needed.
|
||||
// c. Let dateUntil be ? GetMethod(calendar, "dateUntil").
|
||||
auto date_until = TRY(Value(calendar).get_method(vm, vm.names.dateUntil));
|
||||
|
||||
// d. Repeat, while years ≠ 0,
|
||||
while (years != 0) {
|
||||
|
@ -879,8 +879,10 @@ ThrowCompletionOr<DateDurationRecord> unbalance_duration_relative(VM& vm, double
|
|||
// iii. Perform ! CreateDataPropertyOrThrow(untilOptions, "largestUnit", "month").
|
||||
MUST(until_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, "month"_string)));
|
||||
|
||||
// FIXME: AD-HOC calendar records use as this AO is not up to date with latest spec
|
||||
// iv. Let untilResult be ? CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil).
|
||||
auto* until_result = TRY(calendar_date_until(vm, *calendar, relative_to, new_relative_to, *until_options, date_until));
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { *calendar }, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
auto until_result = TRY(calendar_date_until(vm, calendar_record, relative_to, new_relative_to, *until_options));
|
||||
|
||||
// v. Let oneYearMonths be untilResult.[[Months]].
|
||||
auto one_year_months = until_result->months();
|
||||
|
@ -1106,8 +1108,8 @@ ThrowCompletionOr<DateDurationRecord> balance_duration_relative(VM& vm, double y
|
|||
// j. Set newRelativeTo to ? CalendarDateAdd(calendar, relativeTo, oneYear, undefined, dateAdd).
|
||||
new_relative_to = TRY(calendar_date_add(vm, calendar, relative_to, *one_year, nullptr, date_add));
|
||||
|
||||
// FIXME: This AO is out of date, this is no longer needed.
|
||||
// k. Let dateUntil be ? GetMethod(calendar, "dateUntil").
|
||||
auto date_until = TRY(Value(&calendar).get_method(vm, vm.names.dateUntil));
|
||||
|
||||
// l. Let untilOptions be OrdinaryObjectCreate(null).
|
||||
auto until_options = Object::create(realm, nullptr);
|
||||
|
@ -1115,8 +1117,10 @@ ThrowCompletionOr<DateDurationRecord> balance_duration_relative(VM& vm, double y
|
|||
// m. Perform ! CreateDataPropertyOrThrow(untilOptions, "largestUnit", "month").
|
||||
MUST(until_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, "month"_string)));
|
||||
|
||||
// FIXME: AD-HOC calendar records use as this AO is not up to date with latest spec
|
||||
// n. Let untilResult be ? CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil).
|
||||
auto* until_result = TRY(calendar_date_until(vm, calendar, relative_to, new_relative_to, *until_options, date_until));
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { calendar }, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
auto until_result = TRY(calendar_date_until(vm, calendar_record, relative_to, new_relative_to, *until_options));
|
||||
|
||||
// o. Let oneYearMonths be untilResult.[[Months]].
|
||||
auto one_year_months = until_result->months();
|
||||
|
@ -1142,7 +1146,7 @@ ThrowCompletionOr<DateDurationRecord> balance_duration_relative(VM& vm, double y
|
|||
MUST(until_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, "month"_string)));
|
||||
|
||||
// vii. Set untilResult to ? CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil).
|
||||
until_result = TRY(calendar_date_until(vm, calendar, relative_to, new_relative_to, *until_options, date_until));
|
||||
until_result = TRY(calendar_date_until(vm, calendar_record, relative_to, new_relative_to, *until_options));
|
||||
|
||||
// viii. Set oneYearMonths to untilResult.[[Months]].
|
||||
one_year_months = until_result->months();
|
||||
|
@ -1297,7 +1301,9 @@ ThrowCompletionOr<DurationRecord> add_duration(VM& vm, double years1, double mon
|
|||
MUST(difference_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, date_largest_unit)));
|
||||
|
||||
// j. Let dateDifference be ? CalendarDateUntil(calendar, relativeTo, end, differenceOptions).
|
||||
auto* date_difference = TRY(calendar_date_until(vm, calendar, &relative_to, end, *difference_options));
|
||||
// FIXME: AD-HOC calendar records use as this AO is not up to date with latest spec
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { calendar }, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
auto date_difference = TRY(calendar_date_until(vm, calendar_record, &relative_to, end, *difference_options));
|
||||
|
||||
// k. Let result be ? BalanceDuration(dateDifference.[[Days]], h1 + h2, min1 + min2, s1 + s2, ms1 + ms2, mus1 + mus2, ns1 + ns2, largestUnit).
|
||||
// NOTE: Nanoseconds is the only one that can overflow the safe integer range of a double
|
||||
|
@ -1367,7 +1373,7 @@ ThrowCompletionOr<ZonedDateTime*> move_relative_zoned_date_time(VM& vm, ZonedDat
|
|||
|
||||
// 7.5.27 RoundDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, increment, unit, roundingMode [ , plainRelativeTo [ , calendarRec [ , zonedRelativeTo [ , timeZoneRec [ , precalculatedPlainDateTime ] ] ] ] ] ), https://tc39.es/proposal-temporal/#sec-temporal-roundduration
|
||||
// FIXME: Support calendarRec, zonedRelativeTo, timeZoneRec, precalculatedPlainDateTime
|
||||
ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* plain_relative_to_object)
|
||||
ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* plain_relative_to_object, Optional<CalendarMethods> const& calendar_record)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
|
@ -1377,6 +1383,9 @@ ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double m
|
|||
double fractional_days = 0;
|
||||
|
||||
// FIXME: 1. Assert: If either of plainRelativeTo or zonedRelativeTo are present and not undefined, calendarRec is not undefined.
|
||||
if (plain_relative_to_object)
|
||||
VERIFY(calendar_record.has_value());
|
||||
|
||||
// FIXME: 2. Assert: If zonedRelativeTo is present and not undefined, timeZoneRec is not undefined.
|
||||
|
||||
// 3. If plainRelativeTo is not present, set plainRelativeTo to undefined.
|
||||
|
@ -1398,8 +1407,11 @@ ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double m
|
|||
if (!plain_relative_to_object)
|
||||
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, unit, "smallestUnit"sv);
|
||||
|
||||
// FIXME: b. Assert: CalendarMethodsRecordHasLookedUp(calendarRec, dateAdd) is true.
|
||||
// FIXME: c. Assert: CalendarMethodsRecordHasLookedUp(calendarRec, dateUntil) is true.
|
||||
// b. Assert: CalendarMethodsRecordHasLookedUp(calendarRec, dateAdd) is true.
|
||||
VERIFY(calendar_methods_record_has_looked_up(calendar_record.value(), CalendarMethod::DateAdd));
|
||||
|
||||
// c. Assert: CalendarMethodsRecordHasLookedUp(calendarRec, dateUntil) is true.
|
||||
VERIFY(calendar_methods_record_has_looked_up(calendar_record.value(), CalendarMethod::DateAdd));
|
||||
}
|
||||
|
||||
// FIXME: AD-HOC, should be coming from arguments given to this function.
|
||||
|
@ -1505,8 +1517,7 @@ ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double m
|
|||
MUST(until_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, "year"_string)));
|
||||
|
||||
// l. Let timePassed be ? DifferenceDate(calendarRec, plainRelativeTo, wholeDaysLater, untilOptions).
|
||||
// FIXME: Pass through calendarRec
|
||||
auto time_passed = TRY(difference_date(vm, *calendar, *plain_relative_to, *whole_days_later, *until_options));
|
||||
auto time_passed = TRY(difference_date(vm, calendar_record.value(), *plain_relative_to, *whole_days_later, *until_options));
|
||||
|
||||
// m. Let yearsPassed be timePassed.[[Years]].
|
||||
auto years_passed = time_passed->years();
|
||||
|
@ -1600,8 +1611,7 @@ ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double m
|
|||
MUST(until_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, "month"_string)));
|
||||
|
||||
// l. Let timePassed be ? DifferenceDate(calendarRec, plainRelativeTo, wholeDaysLater, untilOptions).
|
||||
// FIXME: Pass through receiver from calendarRec
|
||||
auto time_passed = TRY(difference_date(vm, *calendar, *plain_relative_to, *whole_days_later, *until_options));
|
||||
auto time_passed = TRY(difference_date(vm, calendar_record.value(), *plain_relative_to, *whole_days_later, *until_options));
|
||||
|
||||
// m. Let monthsPassed be timePassed.[[Months]].
|
||||
auto months_passed = time_passed->months();
|
||||
|
@ -1672,8 +1682,7 @@ ThrowCompletionOr<RoundedDuration> round_duration(VM& vm, double years, double m
|
|||
MUST(until_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, "month"_string)));
|
||||
|
||||
// e. Let timePassed be ? DifferenceDate(calendarRec, plainRelativeTo, wholeDaysLater, untilOptions).
|
||||
// FIXME: Pass through calendarRec
|
||||
auto time_passed = TRY(difference_date(vm, *calendar, *plain_relative_to, *whole_days_later, *until_options));
|
||||
auto time_passed = TRY(difference_date(vm, calendar_record.value(), *plain_relative_to, *whole_days_later, *until_options));
|
||||
|
||||
// f. Let weeksPassed be timePassed.[[Weeks]].
|
||||
auto weeks_passed = time_passed->weeks();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -11,6 +12,7 @@
|
|||
#include <LibJS/Runtime/Completion.h>
|
||||
#include <LibJS/Runtime/Date.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDate.h>
|
||||
#include <LibJS/Runtime/Temporal/ZonedDateTime.h>
|
||||
#include <LibJS/Runtime/VM.h>
|
||||
|
@ -148,7 +150,7 @@ ThrowCompletionOr<DateDurationRecord> balance_duration_relative(VM&, double year
|
|||
ThrowCompletionOr<DurationRecord> add_duration(VM&, double years1, double months1, double weeks1, double days1, double hours1, double minutes1, double seconds1, double milliseconds1, double microseconds1, double nanoseconds1, double years2, double months2, double weeks2, double days2, double hours2, double minutes2, double seconds2, double milliseconds2, double microseconds2, double nanoseconds2, Value relative_to_value);
|
||||
ThrowCompletionOr<MoveRelativeDateResult> move_relative_date(VM&, Object& calendar, PlainDate& relative_to, Duration& duration, FunctionObject* date_add);
|
||||
ThrowCompletionOr<ZonedDateTime*> move_relative_zoned_date_time(VM&, ZonedDateTime&, double years, double months, double weeks, double days);
|
||||
ThrowCompletionOr<RoundedDuration> round_duration(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object = nullptr);
|
||||
ThrowCompletionOr<RoundedDuration> round_duration(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object = nullptr, Optional<CalendarMethods> const& = {});
|
||||
ThrowCompletionOr<DurationRecord> adjust_rounded_duration_days(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object);
|
||||
ThrowCompletionOr<String> temporal_duration_to_string(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Variant<StringView, u8> const& precision);
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> add_duration_to_or_subtract_duration_from_duration(VM&, ArithmeticOperation, Duration const&, Value other_value, Value options_value);
|
||||
|
|
|
@ -420,8 +420,22 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::round)
|
|||
// 21. Let unbalanceResult be ? UnbalanceDurationRelative(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], largestUnit, relativeTo).
|
||||
auto unbalance_result = TRY(unbalance_duration_relative(vm, duration->years(), duration->months(), duration->weeks(), duration->days(), *largest_unit, relative_to));
|
||||
|
||||
// FIXME: AD-HOC - this function is not up to date with latest spec.
|
||||
PlainDate* plain_relative_to_ptr = nullptr;
|
||||
ZonedDateTime* zoned_relative_to_ptr = nullptr;
|
||||
|
||||
if (relative_to.is_object()) {
|
||||
if (is<PlainDate>(relative_to.as_object()))
|
||||
plain_relative_to_ptr = &static_cast<PlainDate&>(relative_to.as_object());
|
||||
else if (is<ZonedDateTime>(relative_to.as_object()))
|
||||
zoned_relative_to_ptr = &static_cast<ZonedDateTime&>(relative_to.as_object());
|
||||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
auto calendar_record = TRY(create_calendar_methods_record_from_relative_to(vm, plain_relative_to_ptr, zoned_relative_to_ptr, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
// 22. Let roundResult be (? RoundDuration(unbalanceResult.[[Years]], unbalanceResult.[[Months]], unbalanceResult.[[Weeks]], unbalanceResult.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode, relativeTo)).[[DurationRecord]].
|
||||
auto round_result = TRY(round_duration(vm, unbalance_result.years, unbalance_result.months, unbalance_result.weeks, unbalance_result.days, duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode, relative_to.is_object() ? &relative_to.as_object() : nullptr)).duration_record;
|
||||
auto round_result = TRY(round_duration(vm, unbalance_result.years, unbalance_result.months, unbalance_result.weeks, unbalance_result.days, duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode, relative_to.is_object() ? &relative_to.as_object() : nullptr, calendar_record)).duration_record;
|
||||
|
||||
// 23. Let adjustResult be ? AdjustRoundedDurationDays(roundResult.[[Years]], roundResult.[[Months]], roundResult.[[Weeks]], roundResult.[[Days]], roundResult.[[Hours]], roundResult.[[Minutes]], roundResult.[[Seconds]], roundResult.[[Milliseconds]], roundResult.[[Microseconds]], roundResult.[[Nanoseconds]], roundingIncrement, smallestUnit, roundingMode, relativeTo).
|
||||
auto adjust_result = TRY(adjust_rounded_duration_days(vm, round_result.years, round_result.months, round_result.weeks, round_result.days, round_result.hours, round_result.minutes, round_result.seconds, round_result.milliseconds, round_result.microseconds, round_result.nanoseconds, rounding_increment, *smallest_unit, rounding_mode, relative_to.is_object() ? &relative_to.as_object() : nullptr));
|
||||
|
@ -500,7 +514,22 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::total)
|
|||
auto balance_result = TRY(balance_duration(vm, unbalance_result.days, duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), Crypto::SignedBigInteger { duration->nanoseconds() }, *unit, intermediate));
|
||||
|
||||
// 12. Let roundRecord be ? RoundDuration(unbalanceResult.[[Years]], unbalanceResult.[[Months]], unbalanceResult.[[Weeks]], balanceResult.[[Days]], balanceResult.[[Hours]], balanceResult.[[Minutes]], balanceResult.[[Seconds]], balanceResult.[[Milliseconds]], balanceResult.[[Microseconds]], balanceResult.[[Nanoseconds]], 1, unit, "trunc", relativeTo).
|
||||
auto round_record = TRY(round_duration(vm, unbalance_result.years, unbalance_result.months, unbalance_result.weeks, balance_result.days, balance_result.hours, balance_result.minutes, balance_result.seconds, balance_result.milliseconds, balance_result.microseconds, balance_result.nanoseconds, 1, *unit, "trunc"sv, relative_to.is_object() ? &relative_to.as_object() : nullptr));
|
||||
// FIXME: AD-HOC - this function is not up to date with latest spec.
|
||||
PlainDate* plain_relative_to_ptr = nullptr;
|
||||
ZonedDateTime* zoned_relative_to_ptr = nullptr;
|
||||
|
||||
if (relative_to.is_object()) {
|
||||
if (is<PlainDate>(relative_to.as_object()))
|
||||
plain_relative_to_ptr = &static_cast<PlainDate&>(relative_to.as_object());
|
||||
else if (is<ZonedDateTime>(relative_to.as_object()))
|
||||
zoned_relative_to_ptr = &static_cast<ZonedDateTime&>(relative_to.as_object());
|
||||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
auto calendar_record = TRY(create_calendar_methods_record_from_relative_to(vm, plain_relative_to_ptr, zoned_relative_to_ptr, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
|
||||
auto round_record = TRY(round_duration(vm, unbalance_result.years, unbalance_result.months, unbalance_result.weeks, balance_result.days, balance_result.hours, balance_result.minutes, balance_result.seconds, balance_result.milliseconds, balance_result.microseconds, balance_result.nanoseconds, 1, *unit, "trunc"sv, relative_to.is_object() ? &relative_to.as_object() : nullptr, calendar_record));
|
||||
|
||||
// 13. Return 𝔽(roundRecord.[[Total]]).
|
||||
return Value(round_record.total);
|
||||
|
|
|
@ -382,7 +382,7 @@ bool is_valid_iso_date(i32 year, u8 month, u8 day)
|
|||
}
|
||||
|
||||
// 3.5.6 DifferenceDate ( calendarRec, one, two, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencedate
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_date(VM& vm, Object const& calendar, PlainDate const& one, PlainDate const& two, Object const& options)
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_date(VM& vm, CalendarMethods const& calendar_record, PlainDate const& one, PlainDate const& two, Object const& options)
|
||||
{
|
||||
// FIXME: 1. Assert: one.[[Calendar]] and two.[[Calendar]] have been determined to be equivalent as with CalendarEquals.
|
||||
// FIXME: 2. Assert: options is an ordinary Object.
|
||||
|
@ -410,7 +410,7 @@ ThrowCompletionOr<NonnullGCPtr<Duration>> difference_date(VM& vm, Object const&
|
|||
}
|
||||
|
||||
// 7. Return ? CalendarDateUntil(calendarRec, one, two, options).
|
||||
return *TRY(calendar_date_until(vm, calendar, Value { &one }, Value { &two }, options));
|
||||
return TRY(calendar_date_until(vm, calendar_record, Value { &one }, Value { &two }, options));
|
||||
}
|
||||
|
||||
// 3.5.6 BalanceISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisodate
|
||||
|
@ -529,9 +529,9 @@ i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2
|
|||
}
|
||||
|
||||
// 3.5.11 DifferenceTemporalPlainDate ( operation, temporalDate, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaindate
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_date(VM& vm, DifferenceOperation operation, PlainDate& temporal_date, Value other_value, Value options_value)
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_date(VM& vm, DifferenceOperation operation, PlainDate& temporal_date, Value other_value, Value options)
|
||||
{
|
||||
// 1. If operation is since, let sign be -1. Otherwise, let sign be 1.
|
||||
// 1. If operation is SINCE, let sign be -1. Otherwise, let sign be 1.
|
||||
i8 sign = operation == DifferenceOperation::Since ? -1 : 1;
|
||||
|
||||
// 2. Set other to ? ToTemporalDate(other).
|
||||
|
@ -541,25 +541,43 @@ ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_date(VM& vm,
|
|||
if (!TRY(calendar_equals(vm, temporal_date.calendar(), other->calendar())))
|
||||
return vm.throw_completion<RangeError>(ErrorType::TemporalDifferentCalendars);
|
||||
|
||||
// 4. Let settings be ? GetDifferenceSettings(operation, options, date, « », "day", "day").
|
||||
auto settings = TRY(get_difference_settings(vm, operation, options_value, UnitGroup::Date, {}, { "day"sv }, "day"sv));
|
||||
// 4. Let resolvedOptions be ? SnapshotOwnProperties(? GetOptionsObject(options), null).
|
||||
auto resolved_options = TRY(TRY(get_options_object(vm, options))->snapshot_own_properties(vm, nullptr));
|
||||
|
||||
// 5. Let untilOptions be ? MergeLargestUnitOption(settings.[[Options]], settings.[[LargestUnit]]).
|
||||
auto* until_options = TRY(merge_largest_unit_option(vm, settings.options, move(settings.largest_unit)));
|
||||
// 5. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, DATE, « », "day", "day").
|
||||
auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::Date, {}, { "day"sv }, "day"sv));
|
||||
|
||||
// 6. Let result be ? CalendarDateUntil(temporalDate.[[Calendar]], temporalDate, other, untilOptions).
|
||||
auto* duration = TRY(calendar_date_until(vm, temporal_date.calendar(), &temporal_date, other, *until_options));
|
||||
// 6. If temporalDate.[[ISOYear]] = other.[[ISOYear]], and temporalDate.[[ISOMonth]] = other.[[ISOMonth]], and temporalDate.[[ISODay]] = other.[[ISODay]], then
|
||||
if (temporal_date.iso_year() == other->iso_year() && temporal_date.iso_month() == other->iso_month() && temporal_date.iso_day() == other->iso_day()) {
|
||||
// a. Return ! CreateTemporalDuration(0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
|
||||
return MUST(create_temporal_duration(vm, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
auto result = DurationRecord { duration->years(), duration->months(), duration->weeks(), duration->days(), 0, 0, 0, 0, 0, 0 };
|
||||
// 7. Let calendarRec be ? CreateCalendarMethodsRecord(temporalDate.[[Calendar]], « DATE-ADD, DATE-UNTIL »).
|
||||
// FIXME: The type of calendar in PlainDate does not align with latest spec
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { temporal_date.calendar() }, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
|
||||
// 7. If settings.[[SmallestUnit]] is not "day" or settings.[[RoundingIncrement]] ≠ 1, then
|
||||
if (settings.smallest_unit != "day"sv || settings.rounding_increment != 1) {
|
||||
// a. Set result to (? RoundDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], 0, 0, 0, 0, 0, 0, settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]], temporalDate)).[[DurationRecord]].
|
||||
result = TRY(round_duration(vm, result.years, result.months, result.weeks, result.days, 0, 0, 0, 0, 0, 0, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, &temporal_date)).duration_record;
|
||||
// 8. Perform ! CreateDataPropertyOrThrow(resolvedOptions, "largestUnit", settings.[[LargestUnit]]).
|
||||
MUST(resolved_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, settings.largest_unit)));
|
||||
|
||||
// 9. Let result be ? DifferenceDate(calendarRec, temporalDate, other, resolvedOptions).
|
||||
auto result = TRY(difference_date(vm, calendar_record, temporal_date, *other, resolved_options));
|
||||
|
||||
// 10. If settings.[[SmallestUnit]] is "day" and settings.[[RoundingIncrement]] = 1, let roundingGranularityIsNoop be true; else let roundingGranularityIsNoop be false.
|
||||
bool rounding_granularity_is_noop = settings.smallest_unit == "day"sv && settings.rounding_increment == 1;
|
||||
|
||||
// 11. If roundingGranularityIsNoop is false, then
|
||||
if (!rounding_granularity_is_noop) {
|
||||
// a. Let roundRecord be ? RoundDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], ZeroTimeDuration(), settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]], temporalDate, calendarRec).
|
||||
auto round_record = TRY(round_duration(vm, result->years(), result->months(), result->weeks(), result->days(), 0, 0, 0, 0, 0, 0, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, &temporal_date, calendar_record)).duration_record;
|
||||
|
||||
// FIXME: b. Let roundResult be roundRecord.[[NormalizedDuration]].
|
||||
// FIXME: c. Set result to ? BalanceDateDurationRelative(roundResult.[[Years]], roundResult.[[Months]], roundResult.[[Weeks]], roundResult.[[Days]], settings.[[LargestUnit]], settings.[[SmallestUnit]], temporalDate, calendarRec).
|
||||
result = MUST(create_temporal_duration(vm, round_record.years, round_record.months, round_record.weeks, round_record.days, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
// 16. Return ! CreateTemporalDuration(sign × result.[[Years]], sign × result.[[Months]], sign × result.[[Weeks]], sign × result.[[Days]], 0, 0, 0, 0, 0, 0).
|
||||
return TRY(create_temporal_duration(vm, sign * result.years, sign * result.months, sign * result.weeks, sign * result.days, 0, 0, 0, 0, 0, 0));
|
||||
return TRY(create_temporal_duration(vm, sign * result->years(), sign * result->months(), sign * result->weeks(), sign * result->days(), 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -59,6 +59,6 @@ ThrowCompletionOr<String> temporal_date_to_string(VM&, PlainDate&, StringView sh
|
|||
ThrowCompletionOr<ISODateRecord> add_iso_date(VM&, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow);
|
||||
i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2);
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_date(VM&, DifferenceOperation, PlainDate&, Value other, Value options);
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_date(VM& vm, Object const& calendar, PlainDate const& one, PlainDate const& two, Object const& options);
|
||||
ThrowCompletionOr<NonnullGCPtr<Duration>> difference_date(VM& vm, CalendarMethods const&, PlainDate const& one, PlainDate const& two, Object const& options);
|
||||
|
||||
}
|
||||
|
|
|
@ -374,7 +374,9 @@ ThrowCompletionOr<DurationRecord> difference_iso_date_time(VM& vm, i32 year1, u8
|
|||
auto* until_options = TRY(merge_largest_unit_option(vm, options, TRY_OR_THROW_OOM(vm, String::from_utf8(date_largest_unit))));
|
||||
|
||||
// 12. Let dateDifference be ? CalendarDateUntil(calendar, date1, date2, untilOptions).
|
||||
auto* date_difference = TRY(calendar_date_until(vm, calendar, date1, date2, *until_options));
|
||||
// FIXME: AD-HOC calendar records use as this AO is not up to date with latest spec
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { calendar }, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
auto date_difference = TRY(calendar_date_until(vm, calendar_record, date1, date2, *until_options));
|
||||
|
||||
// 13. Let balanceResult be ? BalanceDuration(dateDifference.[[Days]], timeDifference.[[Hours]], timeDifference.[[Minutes]], timeDifference.[[Seconds]], timeDifference.[[Milliseconds]], timeDifference.[[Microseconds]], timeDifference.[[Nanoseconds]], largestUnit).
|
||||
auto balance_result = TRY(balance_duration(vm, date_difference->days(), time_difference.hours, time_difference.minutes, time_difference.seconds, time_difference.milliseconds, time_difference.microseconds, Crypto::SignedBigInteger { time_difference.nanoseconds }, largest_unit));
|
||||
|
@ -406,7 +408,9 @@ ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_date_time(VM
|
|||
auto* relative_to = MUST(create_temporal_date(vm, date_time.iso_year(), date_time.iso_month(), date_time.iso_day(), date_time.calendar()));
|
||||
|
||||
// 7. Let roundResult be (? RoundDuration(diff.[[Years]], diff.[[Months]], diff.[[Weeks]], diff.[[Days]], diff.[[Hours]], diff.[[Minutes]], diff.[[Seconds]], diff.[[Milliseconds]], diff.[[Microseconds]], diff.[[Nanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]], relativeTo)).[[DurationRecord]].
|
||||
auto round_result = TRY(round_duration(vm, diff.years, diff.months, diff.weeks, diff.days, diff.hours, diff.minutes, diff.seconds, diff.milliseconds, diff.microseconds, diff.nanoseconds, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, relative_to)).duration_record;
|
||||
// FIXME: AD-HOC calendar records use as this AO is not up to date with latest spec
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { date_time.calendar() }, { { CalendarMethod::DateAdd, CalendarMethod::DateUntil } }));
|
||||
auto round_result = TRY(round_duration(vm, diff.years, diff.months, diff.weeks, diff.days, diff.hours, diff.minutes, diff.seconds, diff.milliseconds, diff.microseconds, diff.nanoseconds, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, relative_to, calendar_record)).duration_record;
|
||||
|
||||
// 8. Let result be ? BalanceDuration(roundResult.[[Days]], roundResult.[[Hours]], roundResult.[[Minutes]], roundResult.[[Seconds]], roundResult.[[Milliseconds]], roundResult.[[Microseconds]], roundResult.[[Nanoseconds]], settings.[[LargestUnit]]).
|
||||
auto result = MUST(balance_duration(vm, round_result.days, round_result.hours, round_result.minutes, round_result.seconds, round_result.milliseconds, round_result.microseconds, Crypto::SignedBigInteger { round_result.nanoseconds }, settings.largest_unit));
|
||||
|
|
|
@ -262,7 +262,9 @@ ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_year_month(V
|
|||
// 8. Perform ! CreateDataPropertyOrThrow(resolvedOptions, "largestUnit", settings.[[LargestUnit]]).
|
||||
MUST(resolved_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, settings.largest_unit)));
|
||||
|
||||
// FIXME: 9. Let calendarRec be ? CreateCalendarMethodsRecord(calendar, « dateAdd, dateFromFields, dateUntil, fields »).
|
||||
// 9. Let calendarRec be ? CreateCalendarMethodsRecord(calendar, « dateAdd, dateFromFields, dateUntil, fields »).
|
||||
// FIXME: The type of calendar in PlainYearMonth does not align with latest spec
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { calendar }, { { CalendarMethod::DateAdd, CalendarMethod::DateFromFields, CalendarMethod::DateUntil, CalendarMethod::Fields } }));
|
||||
|
||||
// 10. Let fieldNames be ? CalendarFields(calendarRec, « "monthCode", "year" »).
|
||||
// FIXME: Pass through calendar record
|
||||
|
@ -292,23 +294,22 @@ ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_year_month(V
|
|||
MUST(resolved_options->create_data_property_or_throw(vm.names.largestUnit, PrimitiveString::create(vm, settings.largest_unit)));
|
||||
|
||||
// 18. Let result be ? CalendarDateUntil(calendarRec, thisDate, otherDate, resolvedOptions).
|
||||
// FIXME: Pass through calendar record
|
||||
auto* duration = TRY(calendar_date_until(vm, calendar, this_date, other_date, *resolved_options));
|
||||
auto result = DurationRecord { duration->years(), duration->months(), 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
auto result = TRY(calendar_date_until(vm, calendar_record, this_date, other_date, *resolved_options));
|
||||
|
||||
// 19. If settings.[[SmallestUnit]] is not "month" or settings.[[RoundingIncrement]] ≠ 1, then
|
||||
if (settings.smallest_unit != "month"sv || settings.rounding_increment != 1) {
|
||||
// a. Let roundRecord be ? RoundDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0, settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]], thisDate, calendarRec).
|
||||
// FIXME: Pass through calendar record
|
||||
auto round_record = TRY(round_duration(vm, result.years, result.months, 0, 0, 0, 0, 0, 0, 0, 0, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, this_date));
|
||||
auto round_record = TRY(round_duration(vm, result->years(), result->months(), 0, 0, 0, 0, 0, 0, 0, 0, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, this_date, calendar_record));
|
||||
|
||||
// b. Let roundResult be roundRecord.[[DurationRecord]].
|
||||
auto round_result = round_record.duration_record;
|
||||
|
||||
// FIXME: b. Let roundResult be roundRecord.[[DurationRecord]].
|
||||
// FIXME: c. Set result to ? BalanceDateDurationRelative(roundResult.[[Years]], roundResult.[[Months]], 0, 0, settings.[[LargestUnit]], settings.[[SmallestUnit]], thisDate, calendarRec).
|
||||
result = round_record.duration_record;
|
||||
result = MUST(create_temporal_duration(vm, round_result.years, round_result.months, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
// 20. Return ! CreateTemporalDuration(sign × result.[[Years]], sign × result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0).
|
||||
return MUST(create_temporal_duration(vm, sign * result.years, sign * result.months, 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
return MUST(create_temporal_duration(vm, sign * result->years(), sign * result->months(), 0, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
// 9.5.8 AddDurationToOrSubtractDurationFromPlainYearMonth ( operation, yearMonth, temporalDurationLike, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtoorsubtractdurationfromplainyearmonth
|
||||
|
|
|
@ -609,8 +609,10 @@ ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_zoned_date_time(VM
|
|||
// 8. Let difference be ? DifferenceZonedDateTime(zonedDateTime.[[Nanoseconds]], other.[[Nanoseconds]], zonedDateTime.[[TimeZone]], zonedDateTime.[[Calendar]], settings.[[LargestUnit]], untilOptions).
|
||||
auto difference = TRY(difference_zoned_date_time(vm, zoned_date_time.nanoseconds(), other->nanoseconds(), zoned_date_time.time_zone(), zoned_date_time.calendar(), settings.largest_unit, *until_options));
|
||||
|
||||
auto calendar_record = TRY(create_calendar_methods_record(vm, NonnullGCPtr<Object> { zoned_date_time.calendar() }, { { CalendarMethod::DateAdd, CalendarMethod::DateFromFields, CalendarMethod::DateUntil, CalendarMethod::Fields } }));
|
||||
|
||||
// 9. Let roundResult be (? RoundDuration(difference.[[Years]], difference.[[Months]], difference.[[Weeks]], difference.[[Days]], difference.[[Hours]], difference.[[Minutes]], difference.[[Seconds]], difference.[[Milliseconds]], difference.[[Microseconds]], difference.[[Nanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]], zonedDateTime)).[[DurationRecord]].
|
||||
auto round_result = TRY(round_duration(vm, difference.years, difference.months, difference.weeks, difference.days, difference.hours, difference.minutes, difference.seconds, difference.milliseconds, difference.microseconds, difference.nanoseconds, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, &zoned_date_time)).duration_record;
|
||||
auto round_result = TRY(round_duration(vm, difference.years, difference.months, difference.weeks, difference.days, difference.hours, difference.minutes, difference.seconds, difference.milliseconds, difference.microseconds, difference.nanoseconds, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, &zoned_date_time, calendar_record)).duration_record;
|
||||
|
||||
// 10. Let result be ? AdjustRoundedDurationDays(roundResult.[[Years]], roundResult.[[Months]], roundResult.[[Weeks]], roundResult.[[Days]], roundResult.[[Hours]], roundResult.[[Minutes]], roundResult.[[Seconds]], roundResult.[[Milliseconds]], roundResult.[[Microseconds]], roundResult.[[Nanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]], zonedDateTime).
|
||||
auto result = TRY(adjust_rounded_duration_days(vm, round_result.years, round_result.months, round_result.weeks, round_result.days, round_result.hours, round_result.minutes, round_result.seconds, round_result.milliseconds, round_result.microseconds, round_result.nanoseconds, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode, &zoned_date_time));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue