diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index eb57556a9d..d486089e3c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -5,6 +5,7 @@ */ #include +#include #include namespace JS::Temporal { @@ -23,6 +24,34 @@ void PlainDatePrototype::initialize(GlobalObject& global_object) // 3.3.2 Temporal.PlainDate.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainDate"), Attribute::Configurable); + + define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable); +} + +static PlainDate* typed_this(GlobalObject& global_object) +{ + auto& vm = global_object.vm(); + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return {}; + if (!is(this_object)) { + vm.throw_exception(global_object, ErrorType::NotA, "Temporal.PlainDate"); + return {}; + } + return static_cast(this_object); +} + +// 3.3.3 get Temporal.PlainDate.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendar +JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::calendar_getter) +{ + // 1. Let temporalDate be the this value. + // Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). + auto* temporal_date = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Return temporalDate.[[Calendar]]. + return Value(&temporal_date->calendar()); } } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h index 88c5fbd4b9..69f93afa6f 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -17,6 +17,9 @@ public: explicit PlainDatePrototype(GlobalObject&); virtual void initialize(GlobalObject&) override; virtual ~PlainDatePrototype() override = default; + +private: + JS_DECLARE_NATIVE_FUNCTION(calendar_getter); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendar.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendar.js new file mode 100644 index 0000000000..2a122a82fd --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.calendar.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const calendar = { hello: "friends" }; + const plain_date = new Temporal.PlainDate(1, 1, 1, calendar); + expect(plain_date.calendar).toBe(calendar); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.Duration object", () => { + expect(() => { + Reflect.get(Temporal.PlainDate.prototype, "calendar", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDate"); + }); +});