Selaa lähdekoodia

LibJS: Implement Temporal.Calendar.prototype.id

Linus Groh 4 vuotta sitten
vanhempi
commit
466c5bc96d

+ 11 - 0
Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp

@@ -26,6 +26,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
     define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Temporal.Calendar"), Attribute::Configurable);
 
     u8 attr = Attribute::Writable | Attribute::Configurable;
+    define_native_accessor(vm.names.id, id_getter, {}, Attribute::Configurable);
     define_native_function(vm.names.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 0, attr);
 }
@@ -43,6 +44,16 @@ static Calendar* typed_this(GlobalObject& global_object)
     return static_cast<Calendar*>(this_object);
 }
 
+// 12.4.3 get Temporal.Calendar.prototype.id, https://tc39.es/proposal-temporal/#sec-get-temporal.calendar.prototype.id
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::id_getter)
+{
+    // 1. Let calendar be the this value.
+    auto calendar = vm.this_value(global_object);
+
+    // 2. Return ? ToString(calendar).
+    return js_string(vm, calendar.to_string(global_object));
+}
+
 // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
 JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
 {

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

@@ -19,6 +19,7 @@ public:
     virtual ~CalendarPrototype() override = default;
 
 private:
+    JS_DECLARE_NATIVE_FUNCTION(id_getter);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
 };

+ 1 - 2
Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.js

@@ -29,8 +29,7 @@ describe("normal behavior", () => {
 
     test("basic functionality", () => {
         const calendar = new Temporal.Calendar("iso8601");
-        // FIXME: Enable this once Temporal.Calendar.prototype.id is implemented
-        // expect(calendar.id).toBe("iso8601");
+        expect(calendar.id).toBe("iso8601");
         expect(typeof calendar).toBe("object");
         expect(calendar).toBeInstanceOf(Temporal.Calendar);
         expect(Object.getPrototypeOf(calendar)).toBe(Temporal.Calendar.prototype);

+ 10 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.id.js

@@ -0,0 +1,10 @@
+describe("correct behavior", () => {
+    test("basic functionality", () => {
+        const calendar = new Temporal.Calendar("iso8601");
+        expect(calendar.id).toBe("iso8601");
+    });
+
+    test("works with any this value", () => {
+        expect(Reflect.get(Temporal.Calendar.prototype, "id", "foo")).toBe("foo");
+    });
+});