diff options
author | Luke Wilde <lukew@serenityos.org> | 2021-09-09 06:27:55 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-09 09:06:23 +0100 |
commit | ff0b01a5051378e5a3616ebaa407dff999562f8e (patch) | |
tree | da5b12378caef2d917fb32d8d95663a01fd94bd1 /Userland/Libraries/LibJS/Runtime/Temporal | |
parent | d3fcf5a570f43b897de97916f03b7d706b211416 (diff) | |
download | serenity-ff0b01a5051378e5a3616ebaa407dff999562f8e.zip |
LibJS: Implement ToTemporalYearMonth AO
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal')
4 files changed, 101 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp index 39bb40121b..acdfda04ca 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp @@ -1062,6 +1062,26 @@ Optional<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject& global_ return TemporalTimeZone { .z = false, .offset = offset, .name = name }; } +// 13.45 ParseTemporalYearMonthString ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalyearmonthstring +Optional<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject& global_object, String const& iso_string) +{ + auto& vm = global_object.vm(); + + // 1. Assert: Type(isoString) is String. + + // 2. If isoString does not satisfy the syntax of a TemporalYearMonthString (see 13.33), then + // a. Throw a RangeError exception. + // TODO + + // 3. Let result be ? ParseISODateTime(isoString). + auto result = parse_iso_date_time(global_object, iso_string); + if (vm.exception()) + return {}; + + // 4. Return the Record { [[Year]]: result.[[Year]], [[Month]]: result.[[Month]], [[Day]]: result.[[Day]], [[Calendar]]: result.[[Calendar]] }. + return TemporalYearMonth { .year = result->year, .month = result->month, .day = result->day, .calendar = move(result->calendar) }; +} + // 13.46 ToPositiveInteger ( argument ), https://tc39.es/proposal-temporal/#sec-temporal-topositiveinteger double to_positive_integer(GlobalObject& global_object, Value argument) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h index e441a89d64..8e256c8294 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h @@ -70,6 +70,13 @@ struct TemporalTimeZone { Optional<String> name; }; +struct TemporalYearMonth { + i32 year; + u8 month; + u8 day; + Optional<String> calendar = {}; +}; + struct SecondsStringPrecision { Variant<StringView, u8> precision; String unit; @@ -103,6 +110,7 @@ Optional<ISODateTime> parse_temporal_date_time_string(GlobalObject&, String cons Optional<TemporalDuration> parse_temporal_duration_string(GlobalObject&, String const& iso_string); Optional<TemporalTime> parse_temporal_time_string(GlobalObject&, String const& iso_string); Optional<TemporalTimeZone> parse_temporal_time_zone_string(GlobalObject&, String const& iso_string); +Optional<TemporalYearMonth> parse_temporal_year_month_string(GlobalObject&, String const& iso_string); double to_positive_integer(GlobalObject&, Value argument); Object* prepare_temporal_fields(GlobalObject&, Object& fields, Vector<String> const& field_names, Vector<StringView> const& required_fields); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp index a27f060b12..d07d8d36d6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp @@ -30,6 +30,78 @@ void PlainYearMonth::visit_edges(Visitor& visitor) visitor.visit(&m_calendar); } +// 9.5.1 ToTemporalYearMonth ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalyearmonth +PlainYearMonth* to_temporal_year_month(GlobalObject& global_object, Value item, Object* options) +{ + auto& vm = global_object.vm(); + + // 1. If options is not present, set options to ! OrdinaryObjectCreate(null). + if (!options) + options = Object::create(global_object, nullptr); + + // 2. Assert: Type(options) is Object. + + // 3. If Type(item) is Object, then + if (item.is_object()) { + auto& item_object = item.as_object(); + + // a. If item has an [[InitializedTemporalYearMonth]] internal slot, then + if (is<PlainYearMonth>(item_object)) { + // i. Return item. + return static_cast<PlainYearMonth*>(&item_object); + } + + // b. Let calendar be ? GetTemporalCalendarWithISODefault(item). + auto* calendar = get_temporal_calendar_with_iso_default(global_object, item_object); + if (vm.exception()) + return {}; + + // c. Let fieldNames be ? CalendarFields(calendar, « "month", "monthCode", "year" »). + auto field_names = calendar_fields(global_object, *calendar, { "month"sv, "monthCode"sv, "year"sv }); + if (vm.exception()) + return {}; + + // d. Let fields be ? PrepareTemporalFields(item, fieldNames, «»). + auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {}); + if (vm.exception()) + return {}; + + // e. Return ? YearMonthFromFields(calendar, fields, options). + return year_month_from_fields(global_object, *calendar, *fields, options); + } + + // 4. Perform ? ToTemporalOverflow(options). + (void)to_temporal_overflow(global_object, *options); + if (vm.exception()) + return {}; + + // 5. Let string be ? ToString(item). + auto string = item.to_string(global_object); + if (vm.exception()) + return {}; + + // 6. Let result be ? ParseTemporalYearMonthString(string). + auto result = parse_temporal_year_month_string(global_object, string); + if (vm.exception()) + return {}; + + // 7. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]). + auto* calendar = to_temporal_calendar_with_iso_default(global_object, result->calendar.has_value() ? js_string(vm, *result->calendar) : js_undefined()); + if (vm.exception()) + return {}; + + // 8. Set result to ? CreateTemporalYearMonth(result.[[Year]], result.[[Month]], calendar, result.[[Day]]). + auto* creation_result = create_temporal_year_month(global_object, result->year, result->month, *calendar, result->day); + if (vm.exception()) + return {}; + + // 9. Let canonicalYearMonthOptions be ! OrdinaryObjectCreate(null). + auto* canonical_year_month_options = Object::create(global_object, nullptr); + + // 10. Return ? YearMonthFromFields(calendar, result, canonicalYearMonthOptions). + return year_month_from_fields(global_object, *calendar, *creation_result, canonical_year_month_options); +} + // 9.5.2 RegulateISOYearMonth ( year, month, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulateisoyearmonth Optional<ISOYearMonth> regulate_iso_year_month(GlobalObject& global_object, double year, double month, StringView overflow) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.h index 07550b7110..ca0c1b6b67 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.h @@ -39,6 +39,7 @@ struct ISOYearMonth { u8 reference_iso_day; }; +PlainYearMonth* to_temporal_year_month(GlobalObject& global_object, Value item, Object* options = nullptr); Optional<ISOYearMonth> regulate_iso_year_month(GlobalObject&, double year, double month, StringView overflow); bool is_valid_iso_month(u8 month); bool iso_year_month_within_limits(i32 year, u8 month); |