From 58e2597dc2e755c1451a0383528375507e86a06b Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Thu, 9 Sep 2021 03:53:47 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainYearMonth.prototype.equals --- .../Temporal/PlainYearMonthPrototype.cpp | 31 +++++++++++++++++++ .../Temporal/PlainYearMonthPrototype.h | 1 + .../PlainYearMonth.prototype.equals.js | 15 +++++++++ 3 files changed, 47 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.equals.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index d92e9c75c0..36b5efb9ae 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -40,6 +40,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; + 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); define_native_function(vm.names.toJSON, to_json, 0, attr); @@ -217,6 +218,36 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_year_getter) return calendar_era_year(global_object, calendar, *plain_year_month); } +// 9.3.16 Temporal.PlainYearMonth.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.equals +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::equals) +{ + // 1. Let yearMonth be the this value. + // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto* year_month = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Set other to ? ToTemporalYearMonth(other). + auto* other = to_temporal_year_month(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 4. If yearMonth.[[ISOYear]] ≠ other.[[ISOYear]], return false. + if (year_month->iso_year() != other->iso_year()) + return Value(false); + + // 5. If yearMonth.[[ISOMonth]] ≠ other.[[ISOMonth]], return false. + if (year_month->iso_month() != other->iso_month()) + return Value(false); + + // 6. If yearMonth.[[ISODay]] ≠ other.[[ISODay]], return false. + if (year_month->iso_day() != other->iso_day()) + return Value(false); + + // 7. Return ? CalendarEquals(yearMonth.[[Calendar]], other.[[Calendar]]). + return Value(calendar_equals(global_object, year_month->calendar(), other->calendar())); +} + // 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index eb4fa4a93a..169dd79a05 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -29,6 +29,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(era_getter); JS_DECLARE_NATIVE_FUNCTION(era_year_getter); + JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string); JS_DECLARE_NATIVE_FUNCTION(to_json); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.equals.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.equals.js new file mode 100644 index 0000000000..d3b6aa22ec --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.equals.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainYearMonth.prototype.equals).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = { hello: "friends" }; + const firstPlainDateTime = new Temporal.PlainYearMonth(1, 1, calendar); + const secondPlainDateTime = new Temporal.PlainYearMonth(0, 1, calendar); + expect(firstPlainDateTime.equals(firstPlainDateTime)).toBeTrue(); + expect(secondPlainDateTime.equals(secondPlainDateTime)).toBeTrue(); + expect(firstPlainDateTime.equals(secondPlainDateTime)).toBeFalse(); + expect(secondPlainDateTime.equals(firstPlainDateTime)).toBeFalse(); + }); +});