From 2c222ba40bfa1f2422880b6a4f753fe6ab602c31 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 10 Oct 2021 21:30:15 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainYearMonth.prototype.toPlainDate() --- .../LibJS/Runtime/Temporal/Calendar.cpp | 25 ++++++++ .../LibJS/Runtime/Temporal/Calendar.h | 1 + .../Temporal/PlainYearMonthPrototype.cpp | 61 +++++++++++++++++++ .../Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.toPlainDate.js | 27 ++++++++ 5 files changed, 115 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toPlainDate.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 2777d9b79a..17f1a05788 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -113,6 +113,31 @@ ThrowCompletionOr> calendar_fields(GlobalObject& global_object, O return result; } +// 12.1.6 CalendarMergeFields ( calendar, fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmergefields +ThrowCompletionOr calendar_merge_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object& additional_fields) +{ + auto& vm = global_object.vm(); + + // 1. Let mergeFields be ? GetMethod(calendar, "mergeFields"). + auto* merge_fields = TRY(Value(&calendar).get_method(global_object, vm.names.mergeFields)); + + // 2. If mergeFields is undefined, then + if (!merge_fields) { + // a. Return ? DefaultMergeFields(fields, additionalFields). + return TRY(default_merge_fields(global_object, fields, additional_fields)); + } + + // 3. Let result be ? Call(mergeFields, calendar, « fields, additionalFields »). + auto result = TRY(call(global_object, merge_fields, &calendar, &fields, &additional_fields)); + + // 4. If Type(result) is not Object, throw a TypeError exception. + if (!result.is_object()) + return vm.throw_completion(global_object, ErrorType::NotAnObject, result.to_string_without_side_effects()); + + // 5. Return result. + return &result.as_object(); +} + // 12.1.9 CalendarYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryear ThrowCompletionOr calendar_year(GlobalObject& global_object, Object& calendar, Object& date_like) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index d9b9e91f4a..0e96b2074a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -35,6 +35,7 @@ bool is_builtin_calendar(String const& identifier); ThrowCompletionOr get_builtin_calendar(GlobalObject&, String const& identifier); Calendar* get_iso8601_calendar(GlobalObject&); ThrowCompletionOr> calendar_fields(GlobalObject&, Object& calendar, Vector const& field_names); +ThrowCompletionOr calendar_merge_fields(GlobalObject&, Object& calendar, Object& fields, Object& additional_fields); ThrowCompletionOr calendar_year(GlobalObject&, Object& calendar, Object& date_like); ThrowCompletionOr calendar_month(GlobalObject&, Object& calendar, Object& date_like); ThrowCompletionOr calendar_month_code(GlobalObject&, Object& calendar, Object& date_like); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index b1486a6121..59be1fcc7e 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.toLocaleString, to_locale_string, 0, attr); define_native_function(vm.names.toJSON, to_json, 0, attr); define_native_function(vm.names.valueOf, value_of, 0, attr); + define_native_function(vm.names.toPlainDate, to_plain_date, 1, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } @@ -289,6 +290,66 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of) return {}; } +// 9.3.21 Temporal.PlainYearMonth.prototype.toPlainDate ( item ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.toplaindate +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date) +{ + auto item = vm.argument(0); + + // 1. Let yearMonth be the this value. + // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto* year_month = typed_this_object(global_object); + if (vm.exception()) + return {}; + + // 3. If Type(item) is not Object, then + if (!item.is_object()) { + // a. Throw a TypeError exception. + vm.throw_exception(global_object, ErrorType::NotAnObject, item); + return {}; + } + + // 4. Let calendar be yearMonth.[[Calendar]]. + auto& calendar = year_month->calendar(); + + // 5. Let receiverFieldNames be ? CalendarFields(calendar, « "monthCode", "year" »). + auto receiver_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv })); + + // 6. Let fields be ? PrepareTemporalFields(yearMonth, receiverFieldNames, «»). + auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *year_month, receiver_field_names, {})); + + // 7. Let inputFieldNames be ? CalendarFields(calendar, « "day" »). + auto input_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "day"sv })); + + // 8. Let inputFields be ? PrepareTemporalFields(item, inputFieldNames, «»). + auto* input_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item.as_object(), input_field_names, {})); + + // 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields). + auto* merged_fields = TRY_OR_DISCARD(calendar_merge_fields(global_object, calendar, *fields, *input_fields)); + + // 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed. + Vector merged_field_names; + for (auto& field_name : receiver_field_names) { + if (!merged_field_names.contains_slow(field_name)) + merged_field_names.append(move(field_name)); + } + for (auto& field_name : input_field_names) { + if (!merged_field_names.contains_slow(field_name)) + merged_field_names.append(move(field_name)); + } + + // 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»). + merged_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, {})); + + // 12. Let options be ! OrdinaryObjectCreate(null). + auto* options = Object::create(global_object, nullptr); + + // 13. Perform ! CreateDataPropertyOrThrow(options, "overflow", "reject"). + MUST(options->create_data_property_or_throw(vm.names.overflow, js_string(vm, vm.names.reject.as_string()))); + + // 14. Return ? DateFromFields(calendar, mergedFields, options). + return TRY_OR_DISCARD(date_from_fields(global_object, calendar, *merged_fields, *options)); +} + // 9.3.22 Temporal.PlainYearMonth.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.getisofields JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::get_iso_fields) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index 6c33055ef7..4dc0ed0135 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -35,6 +35,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(to_json); JS_DECLARE_NATIVE_FUNCTION(value_of); + JS_DECLARE_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toPlainDate.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toPlainDate.js new file mode 100644 index 0000000000..cf0f945da7 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.toPlainDate.js @@ -0,0 +1,27 @@ +describe("normal behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainYearMonth.prototype.toPlainDate).toHaveLength(1); + }); + + test("basic functionality", () => { + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + const plainDate = plainYearMonth.toPlainDate({ day: 6 }); + expect(plainDate.equals(new Temporal.PlainDate(2021, 7, 6))).toBeTrue(); + }); +}); + +describe("errors", () => { + test("argument must be an object", () => { + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(() => { + plainYearMonth.toPlainDate(42); + }).toThrowWithMessage(TypeError, "42 is not an object"); + }); + + test("day field is required", () => { + const plainYearMonth = new Temporal.PlainYearMonth(2021, 7); + expect(() => { + plainYearMonth.toPlainDate({}); + }).toThrowWithMessage(TypeError, "Required property day is missing or undefined"); + }); +});