From 1e3e0477cbb06e7510523d4a24b2bf9f3a400434 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 8 Nov 2021 21:34:16 +0000 Subject: [PATCH] LibJS: Implement Temporal.PlainMonthDay.prototype.with() --- .../Temporal/PlainMonthDayPrototype.cpp | 44 ++++++++++++ .../Runtime/Temporal/PlainMonthDayPrototype.h | 1 + .../PlainMonthDay.prototype.with.js | 69 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.with.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 1307b5c488..c6d1aab0f2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp @@ -33,6 +33,7 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.with, with, 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); @@ -81,6 +82,49 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter) return Value(TRY(calendar_day(global_object, calendar, *month_day))); } +// 10.3.6 Temporal.PlainMonthDay.prototype.with ( temporalMonthDayLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.with +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::with) +{ + auto temporal_month_day_like = vm.argument(0); + + // 1. Let monthDay be the this value. + // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). + auto* month_day = TRY(typed_this_object(global_object)); + + // 3. If Type(temporalMonthDayLike) is not Object, then + if (!temporal_month_day_like.is_object()) { + // a. Throw a TypeError exception. + return vm.throw_completion(global_object, ErrorType::NotAnObject, temporal_month_day_like.to_string_without_side_effects()); + } + + // 4. Perform ? RejectObjectWithCalendarOrTimeZone(temporalMonthDayLike). + TRY(reject_object_with_calendar_or_time_zone(global_object, temporal_month_day_like.as_object())); + + // 5. Let calendar be monthDay.[[Calendar]]. + auto& calendar = month_day->calendar(); + + // 6. Let fieldNames be ? CalendarFields(calendar, « "day", "month", "monthCode", "year" »). + auto field_names = TRY(calendar_fields(global_object, calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv })); + + // 7. Let partialMonthDay be ? PreparePartialTemporalFields(temporalMonthDayLike, fieldNames). + auto* partial_month_day = TRY(prepare_partial_temporal_fields(global_object, temporal_month_day_like.as_object(), field_names)); + + // 8. Set options to ? GetOptionsObject(options). + auto* options = TRY(get_options_object(global_object, vm.argument(1))); + + // 9. Let fields be ? PrepareTemporalFields(monthDay, fieldNames, «»). + auto* fields = TRY(prepare_temporal_fields(global_object, *month_day, field_names, {})); + + // 10. Set fields to ? CalendarMergeFields(calendar, fields, partialMonthDay). + fields = TRY(calendar_merge_fields(global_object, calendar, *fields, *partial_month_day)); + + // 11. Set fields to ? PrepareTemporalFields(fields, fieldNames, «»). + fields = TRY(prepare_temporal_fields(global_object, *fields, field_names, {})); + + // 12. Return ? MonthDayFromFields(calendar, fields, options). + return TRY(month_day_from_fields(global_object, calendar, *fields, options)); +} + // 10.3.7 Temporal.PlainMonthDay.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.equals JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::equals) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index 4750c77f8c..066faa83c8 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(month_code_getter); JS_DECLARE_NATIVE_FUNCTION(day_getter); + JS_DECLARE_NATIVE_FUNCTION(with); JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.with.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.with.js new file mode 100644 index 0000000000..cd23e68588 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.with.js @@ -0,0 +1,69 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainMonthDay.prototype.with).toHaveLength(1); + }); + + test("basic functionality", () => { + const plainMonthDay = new Temporal.PlainMonthDay(1, 1); + const values = [ + [{ monthCode: "M07" }, new Temporal.PlainMonthDay(7, 1)], + [{ monthCode: "M07", day: 6 }, new Temporal.PlainMonthDay(7, 6)], + [{ year: 0, month: 7, day: 6 }, new Temporal.PlainMonthDay(7, 6)], + ]; + for (const [arg, expected] of values) { + expect(plainMonthDay.with(arg).equals(expected)).toBeTrue(); + } + + // Supplying the same values doesn't change the month/day, but still creates a new object + const plainMonthDayLike = { + month: plainMonthDay.month, + day: plainMonthDay.day, + }; + expect(plainMonthDay.with(plainMonthDayLike)).not.toBe(plainMonthDay); + expect(plainMonthDay.with(plainMonthDayLike).equals(plainMonthDay)).toBeTrue(); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainMonthDay object", () => { + expect(() => { + Temporal.PlainMonthDay.prototype.with.call("foo"); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainMonthDay"); + }); + + test("argument must be an object", () => { + expect(() => { + new Temporal.PlainMonthDay(1, 1).with("foo"); + }).toThrowWithMessage(TypeError, "foo is not an object"); + expect(() => { + new Temporal.PlainMonthDay(1, 1).with(42); + }).toThrowWithMessage(TypeError, "42 is not an object"); + }); + + test("argument must have one of 'day', 'month', 'monthCode', 'year'", () => { + expect(() => { + new Temporal.PlainMonthDay(1, 1).with({}); + }).toThrowWithMessage( + TypeError, + "Object must have at least one of the following properties: day, month, monthCode, year" + ); + }); + + test("argument must not have 'calendar' or 'timeZone'", () => { + expect(() => { + new Temporal.PlainMonthDay(1, 1).with({ calendar: {} }); + }).toThrowWithMessage(TypeError, "Object must not have a defined calendar property"); + expect(() => { + new Temporal.PlainMonthDay(1, 1).with({ timeZone: {} }); + }).toThrowWithMessage(TypeError, "Object must not have a defined timeZone property"); + }); + + test("month property only works in combination with year", () => { + expect(() => { + new Temporal.PlainMonthDay(1, 1).with({ month: 1 }); + }).toThrowWithMessage( + TypeError, + "Required property monthCode or year is missing or undefined" + ); + }); +});