From 3548b08de2d903d77a467f19c8d060bd9769a699 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Fri, 10 Sep 2021 17:45:32 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainMonthDay.prototype.equals --- .../Temporal/PlainMonthDayPrototype.cpp | 31 +++++++++++++++++++ .../Runtime/Temporal/PlainMonthDayPrototype.h | 1 + .../PlainMonthDay.prototype.equals.js | 15 +++++++++ 3 files changed, 47 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.equals.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 0cc9bf35d6..67900d8d9a 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.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); @@ -98,6 +99,36 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter) return Value(calendar_day(global_object, calendar, *month_day)); } +// 10.3.7 Temporal.PlainMonthDay.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.equals +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::equals) +{ + // 1. Let monthDay be the this value. + // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). + auto* month_day = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Set other to ? ToTemporalMonthDay(other). + auto* other = to_temporal_month_day(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 4. If monthDay.[[ISOMonth]] ≠ other.[[ISOMonth]], return false. + if (month_day->iso_month() != other->iso_month()) + return Value(false); + + // 5. If monthDay.[[ISODay]] ≠ other.[[ISODay]], return false. + if (month_day->iso_day() != other->iso_day()) + return Value(false); + + // 6. If monthDay.[[ISOYear]] ≠ other.[[ISOYear]], return false. + if (month_day->iso_year() != other->iso_year()) + return Value(false); + + // 7. Return ? CalendarEquals(monthDay.[[Calendar]], other.[[Calendar]]). + return Value(calendar_equals(global_object, month_day->calendar(), other->calendar())); +} + // 10.3.8 Temporal.PlainMonthDay.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.tostring JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index 7af50f88ae..e512fe24a2 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h @@ -22,6 +22,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(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/PlainMonthDay/PlainMonthDay.prototype.equals.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.equals.js new file mode 100644 index 0000000000..5e7d21bcb1 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.equals.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainMonthDay.prototype.equals).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = { hello: "friends" }; + const firstPlainMonthDay = new Temporal.PlainMonthDay(2, 1, calendar); + const secondPlainMonthDay = new Temporal.PlainMonthDay(1, 1, calendar); + expect(firstPlainMonthDay.equals(firstPlainMonthDay)).toBeTrue(); + expect(secondPlainMonthDay.equals(secondPlainMonthDay)).toBeTrue(); + expect(firstPlainMonthDay.equals(secondPlainMonthDay)).toBeFalse(); + expect(secondPlainMonthDay.equals(firstPlainMonthDay)).toBeFalse(); + }); +});