From 15498453897bbb3496793e26ddf11d2a00ba6b72 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 15 Aug 2021 01:17:52 +0100 Subject: [PATCH] LibJS: Implement Temporal.PlainMonthDay.prototype.getISOFields() --- .../Temporal/PlainMonthDayPrototype.cpp | 29 +++++++++++++++++++ .../Runtime/Temporal/PlainMonthDayPrototype.h | 1 + .../PlainMonthDay.prototype.getISOFields.js | 27 +++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.getISOFields.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.cpp index 8df4dd72f0..a5edb3ec60 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) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.valueOf, value_of, 0, attr); + define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } static PlainMonthDay* typed_this(GlobalObject& global_object) @@ -101,4 +102,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of) return {}; } +// 10.3.13 Temporal.PlainMonthDay.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.getisofields +JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::get_iso_fields) +{ + // 1. Let monthDay be the this value. + // 2. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). + auto* month_day = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%). + auto* fields = Object::create(global_object, global_object.object_prototype()); + + // 4. Perform ! CreateDataPropertyOrThrow(fields, "calendar", monthDay.[[Calendar]]). + fields->create_data_property_or_throw(vm.names.calendar, Value(&month_day->calendar())); + + // 5. Perform ! CreateDataPropertyOrThrow(fields, "isoDay", 𝔽(monthDay.[[ISODay]])). + fields->create_data_property_or_throw(vm.names.isoDay, Value(month_day->iso_day())); + + // 6. Perform ! CreateDataPropertyOrThrow(fields, "isoMonth", 𝔽(monthDay.[[ISOMonth]])). + fields->create_data_property_or_throw(vm.names.isoMonth, Value(month_day->iso_month())); + + // 7. Perform ! CreateDataPropertyOrThrow(fields, "isoYear", 𝔽(monthDay.[[ISOYear]])). + fields->create_data_property_or_throw(vm.names.isoYear, Value(month_day->iso_year())); + + // 8. Return fields. + return fields; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDayPrototype.h index 1a3d09f13e..d20c1ea6b2 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(month_code_getter); JS_DECLARE_NATIVE_FUNCTION(day_getter); JS_DECLARE_NATIVE_FUNCTION(value_of); + JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.getISOFields.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.getISOFields.js new file mode 100644 index 0000000000..29feff968f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.getISOFields.js @@ -0,0 +1,27 @@ +describe("normal behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainMonthDay.prototype.getISOFields).toHaveLength(0); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainMonthDay = new Temporal.PlainMonthDay(7, 6, calendar, 2021); + const fields = plainMonthDay.getISOFields(); + expect(fields).toEqual({ calendar, isoDay: 6, isoMonth: 7, isoYear: 2021 }); + // Test field order + expect(Object.getOwnPropertyNames(fields)).toEqual([ + "calendar", + "isoDay", + "isoMonth", + "isoYear", + ]); + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainMonthDay object", () => { + expect(() => { + Temporal.PlainMonthDay.prototype.getISOFields.call("foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainMonthDay"); + }); +});