summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-11-27 18:55:05 +0000
committerLinus Groh <mail@linusgroh.de>2021-11-27 19:11:31 +0000
commitbdd2c357fd0fd8ff1ae234efb43850ca1f762066 (patch)
tree3e5fe2c80775476bc7c5c15b8ccff653e35da00e /Userland/Libraries/LibJS/Runtime
parent6f2e0b33552a41e2c9ae3a1bc257d1e4dd2ae01e (diff)
downloadserenity-bdd2c357fd0fd8ff1ae234efb43850ca1f762066.zip
LibJS: Implement Temporal.PlainYearMonth.prototype.until()
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp81
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h1
2 files changed, 82 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
index f790e53a6e..fc027a64aa 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
@@ -45,6 +45,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.with, with, 1, attr);
define_native_function(vm.names.add, add, 1, attr);
define_native_function(vm.names.subtract, subtract, 1, attr);
+ define_native_function(vm.names.until, until, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
@@ -378,6 +379,86 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::subtract)
return TRY(year_month_from_fields(global_object, calendar, *added_date_fields, options_copy));
}
+// 9.3.14 Temporal.PlainYearMonth.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.until
+JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::until)
+{
+ // 1. Let yearMonth be the this value.
+ // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
+ auto* year_month = TRY(typed_this_object(global_object));
+
+ // 3. Set other to ? ToTemporalYearMonth(other).
+ auto* other = TRY(to_temporal_year_month(global_object, vm.argument(0)));
+
+ // 4. Let calendar be yearMonth.[[Calendar]].
+ auto& calendar = year_month->calendar();
+
+ // 5. If ? CalendarEquals(calendar, other.[[Calendar]]) is false, throw a RangeError exception.
+ if (!TRY(calendar_equals(global_object, calendar, other->calendar())))
+ return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalDifferentCalendars);
+
+ // 6. Set options to ? GetOptionsObject(options).
+ auto* options = TRY(get_options_object(global_object, vm.argument(1)));
+
+ // 7. Let disallowedUnits be « "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond" ».
+ auto disallowed_units = Vector<StringView> { "week"sv, "day"sv, "hour"sv, "minute"sv, "second"sv, "millisecond"sv, "microsecond"sv, "nanosecond"sv };
+
+ // 8. Let smallestUnit be ? ToSmallestTemporalUnit(options, disallowedUnits, "month").
+ auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, *options, disallowed_units, "month"sv));
+
+ // 9. Let largestUnit be ? ToLargestTemporalUnit(options, disallowedUnits, "auto", "year").
+ auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, disallowed_units, "auto"sv, "year"));
+
+ // 10. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit).
+ TRY(validate_temporal_unit_range(global_object, *largest_unit, *smallest_unit));
+
+ // 11. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
+ auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
+
+ // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, undefined, false).
+ auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, {}, false));
+
+ // 13. Let fieldNames be ? CalendarFields(calendar, « "monthCode", "year" »).
+ auto field_names = TRY(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv }));
+
+ // 14. Let otherFields be ? PrepareTemporalFields(other, fieldNames, «»).
+ auto* other_fields = TRY(prepare_temporal_fields(global_object, *other, field_names, {}));
+
+ // 15. Perform ! CreateDataPropertyOrThrow(otherFields, "day", 1𝔽).
+ MUST(other_fields->create_data_property_or_throw(vm.names.day, Value(1)));
+
+ // 16. Let otherDate be ? DateFromFields(calendar, otherFields).
+ // FIXME: Spec doesn't pass required options, see https://github.com/tc39/proposal-temporal/issues/1685.
+ auto* other_date = TRY(date_from_fields(global_object, calendar, *other_fields, *options));
+
+ // 17. Let thisFields be ? PrepareTemporalFields(yearMonth, fieldNames, «»).
+ auto* this_fields = TRY(prepare_temporal_fields(global_object, *year_month, field_names, {}));
+
+ // 18. Perform ! CreateDataPropertyOrThrow(thisFields, "day", 1𝔽).
+ MUST(this_fields->create_data_property_or_throw(vm.names.day, Value(1)));
+
+ // 19. Let thisDate be ? DateFromFields(calendar, thisFields).
+ // FIXME: Spec doesn't pass required options, see https://github.com/tc39/proposal-temporal/issues/1685.
+ auto* this_date = TRY(date_from_fields(global_object, calendar, *this_fields, *options));
+
+ // 20. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit).
+ auto* until_options = TRY(merge_largest_unit_option(global_object, *options, *largest_unit));
+
+ // 21. Let result be ? CalendarDateUntil(calendar, thisDate, otherDate, untilOptions).
+ auto* result = TRY(calendar_date_until(global_object, calendar, this_date, other_date, *until_options));
+
+ // 22. If smallestUnit is "month" and roundingIncrement = 1, then
+ if (smallest_unit == "month"sv && rounding_increment == 1) {
+ // a. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0).
+ return TRY(create_temporal_duration(global_object, result->years(), result->months(), 0, 0, 0, 0, 0, 0, 0, 0));
+ }
+
+ // 23. Let result be ? RoundDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0, roundingIncrement, smallestUnit, roundingMode, thisDate).
+ auto round_result = TRY(round_duration(global_object, result->years(), result->months(), 0, 0, 0, 0, 0, 0, 0, 0, rounding_increment, *smallest_unit, rounding_mode, this_date));
+
+ // 24. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0).
+ return TRY(create_temporal_duration(global_object, round_result.years, round_result.months, 0, 0, 0, 0, 0, 0, 0, 0));
+}
+
// 9.3.16 Temporal.PlainYearMonth.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.equals
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::equals)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
index f3a5e5b216..a30d25ac2b 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
@@ -33,6 +33,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with);
JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_NATIVE_FUNCTION(subtract);
+ JS_DECLARE_NATIVE_FUNCTION(until);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);