From 32fc81c1868451163a423af286065b8f73df1bfd Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 27 Aug 2021 16:50:01 +0300 Subject: [PATCH] LibJS: Implement Temporal.PlainDateTime.prototype.equals() --- .../Temporal/PlainDateTimePrototype.cpp | 26 +++++++++++++++++++ .../Runtime/Temporal/PlainDateTimePrototype.h | 1 + .../PlainDateTime.prototype.equals.js | 13 ++++++++++ 3 files changed, 40 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.equals.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index fcd3ed8cde..a3a29d5929 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -53,6 +53,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.withPlainDate, with_plain_date, 1, attr); define_native_function(vm.names.withCalendar, with_calendar, 1, attr); + define_native_function(vm.names.equals, equals, 1, attr); define_native_function(vm.names.valueOf, value_of, 0, attr); define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr); define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr); @@ -398,6 +399,31 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::with_calendar) return create_temporal_date_time(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), *calendar); } +// 5.3.31 Temporal.PlainDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.equals +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::equals) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Set other to ? ToTemporalDateTime(other). + auto* other = to_temporal_date_time(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 4. Let result be ! CompareISODateTime(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], other.[[ISOYear]], other.[[ISOMonth]], other.[[ISODay]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]]). + auto result = compare_iso_date_time(date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), other->iso_year(), other->iso_month(), other->iso_day(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond()); + + // 5. If result is not 0, return false. + if (result != 0) + return Value(false); + + // 6. Return ? CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]). + return Value(calendar_equals(global_object, date_time->calendar(), other->calendar())); +} + // 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index ed64b8fad6..20f9b10192 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -40,6 +40,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(with_plain_date); JS_DECLARE_NATIVE_FUNCTION(with_calendar); + JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.equals.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.equals.js new file mode 100644 index 0000000000..76badb443d --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.equals.js @@ -0,0 +1,13 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainDateTime.prototype.equals).toHaveLength(1); + }); + + test("basic functionality", () => { + const calendar = { hello: "friends" }; + const firstPlainDateTime = new Temporal.PlainDateTime(1, 1, 1, 1, 1, 1, 1, 1, 1, calendar); + const secondPlainDateTime = new Temporal.PlainDateTime(0, 1, 1, 1, 1, 1, 1, 1, 1, calendar); + expect(firstPlainDateTime.equals(firstPlainDateTime)); + expect(!firstPlainDateTime.equals(secondPlainDateTime)); + }); +});