فهرست منبع

LibJS: Implement Temporal.PlainMonthDay.prototype.valueOf()

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

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

@@ -30,6 +30,9 @@ void PlainMonthDayPrototype::initialize(GlobalObject& global_object)
     define_native_accessor(vm.names.calendar, calendar_getter, {}, Attribute::Configurable);
     define_native_accessor(vm.names.monthCode, month_code_getter, {}, Attribute::Configurable);
     define_native_accessor(vm.names.day, day_getter, {}, Attribute::Configurable);
+
+    u8 attr = Attribute::Writable | Attribute::Configurable;
+    define_native_function(vm.names.valueOf, value_of, 0, attr);
 }
 
 static PlainMonthDay* typed_this(GlobalObject& global_object)
@@ -90,4 +93,12 @@ JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::day_getter)
     return Value(calendar_day(global_object, calendar, *month_day));
 }
 
+// 10.3.11 Temporal.PlainMonthDay.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainmonthday.prototype.valueof
+JS_DEFINE_NATIVE_FUNCTION(PlainMonthDayPrototype::value_of)
+{
+    // 1. Throw a TypeError exception.
+    vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "Temporal.PlainMonthDay", "a primitive value");
+    return {};
+}
+
 }

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

@@ -22,6 +22,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(calendar_getter);
     JS_DECLARE_NATIVE_FUNCTION(month_code_getter);
     JS_DECLARE_NATIVE_FUNCTION(day_getter);
+    JS_DECLARE_NATIVE_FUNCTION(value_of);
 };
 
 }

+ 11 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainMonthDay/PlainMonthDay.prototype.valueOf.js

@@ -0,0 +1,11 @@
+describe("errors", () => {
+    test("throws TypeError", () => {
+        const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
+        expect(() => {
+            plainMonthDay.valueOf();
+        }).toThrowWithMessage(
+            TypeError,
+            "Cannot convert Temporal.PlainMonthDay to a primitive value"
+        );
+    });
+});