Преглед изворни кода

LibJS: Implement Temporal.PlainDate.prototype.valueOf

Timothy Flynn пре 8 месеци
родитељ
комит
06ef32818e

+ 8 - 0
Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp

@@ -62,6 +62,7 @@ void PlainDatePrototype::initialize(Realm& realm)
     define_native_function(realm, vm.names.toString, to_string, 0, attr);
     define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
     define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
+    define_native_function(realm, vm.names.valueOf, value_of, 0, attr);
 }
 
 // 3.3.3 get Temporal.PlainDate.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindate.prototype.calendarid
@@ -401,4 +402,11 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_json)
     return PrimitiveString::create(vm, temporal_date_to_string(temporal_date, ShowCalendar::Auto));
 }
 
+// 3.3.33 Temporal.PlainDate.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.valueof
+JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::value_of)
+{
+    // 1. Throw a TypeError exception.
+    return vm.throw_completion<TypeError>(ErrorType::Convert, "Temporal.PlainDate", "a primitive value");
+}
+
 }

+ 1 - 0
Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h

@@ -51,6 +51,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
+    JS_DECLARE_NATIVE_FUNCTION(value_of);
 };
 
 }

+ 7 - 0
Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.valueOf.js

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