فهرست منبع

LibJS: Implement Temporal.PlainDateTime.prototype.calendar

Linus Groh 4 سال پیش
والد
کامیت
aa2c8b6b91

+ 29 - 0
Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp

@@ -5,6 +5,7 @@
  */
 
 #include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Temporal/PlainDateTime.h>
 #include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
 
 namespace JS::Temporal {
@@ -24,10 +25,38 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
     // 5.3.2 Temporal.PlainDateTime.prototype[ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype-@@tostringtag
     define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.PlainDateTime"), Attribute::Configurable);
 
+    define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
+
     u8 attr = Attribute::Writable | Attribute::Configurable;
     define_native_function(vm.names.valueOf, value_of, 0, attr);
 }
 
+static PlainDateTime* 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<PlainDateTime>(this_object)) {
+        vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainDateTime");
+        return {};
+    }
+    return static_cast<PlainDateTime*>(this_object);
+}
+
+// 5.3.3 get Temporal.PlainDateTime.prototype.calendar, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.calendar
+JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::calendar_getter)
+{
+    // 1. Let dateTime be the this value.
+    // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
+    auto* date_time = typed_this(global_object);
+    if (vm.exception())
+        return {};
+
+    // 3. Return dateTime.[[Calendar]].
+    return Value(&date_time->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)
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h

@@ -19,6 +19,7 @@ public:
     virtual ~PlainDateTimePrototype() override = default;
 
 private:
+    JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
     JS_DECLARE_NATIVE_FUNCTION(value_of);
 };
 

+ 15 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.calendar.js

@@ -0,0 +1,15 @@
+describe("correct behavior", () => {
+    test("basic functionality", () => {
+        const calendar = { hello: "friends" };
+        const plainDateTime = new Temporal.PlainDateTime(0, 1, 1, 0, 0, 0, 0, 0, 0, calendar);
+        expect(plainDateTime.calendar).toBe(calendar);
+    });
+});
+
+test("errors", () => {
+    test("this value must be a Temporal.PlainDateTime object", () => {
+        expect(() => {
+            Reflect.get(Temporal.PlainDateTime.prototype, "calendar", "foo");
+        }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime");
+    });
+});