فهرست منبع

LibJS: Partial support for Date.prototype.setFullYear()

Andreas Kling 4 سال پیش
والد
کامیت
3596c42deb

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -199,6 +199,7 @@ namespace JS {
     P(reverse)                               \
     P(round)                                 \
     P(set)                                   \
+    P(setFullYear)                           \
     P(setPrototypeOf)                        \
     P(shift)                                 \
     P(sign)                                  \

+ 17 - 0
Userland/Libraries/LibJS/Runtime/DatePrototype.cpp

@@ -60,6 +60,7 @@ void DatePrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.getDate, get_date, 0, attr);
     define_native_function(vm.names.getDay, get_day, 0, attr);
     define_native_function(vm.names.getFullYear, get_full_year, 0, attr);
+    define_native_function(vm.names.setFullYear, set_full_year, 3, attr);
     define_native_function(vm.names.getHours, get_hours, 0, attr);
     define_native_function(vm.names.getMilliseconds, get_milliseconds, 0, attr);
     define_native_function(vm.names.getMinutes, get_minutes, 0, attr);
@@ -118,6 +119,22 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_full_year)
     return Value(static_cast<double>(this_object->full_year()));
 }
 
+JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year)
+{
+    auto* this_object = typed_this(vm, global_object);
+    if (!this_object)
+        return {};
+    auto new_year = vm.argument(0).to_i32(global_object);
+    if (vm.exception())
+        return {};
+    // FIXME: Support specifying new month and new day as well.
+    auto& datetime = this_object->datetime();
+    auto new_month = datetime.month();
+    auto new_day = datetime.day();
+    datetime.set_time(new_year, new_month, new_day, datetime.hour(), datetime.minute(), datetime.second());
+    return Value { this_object->time() };
+}
+
 JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours)
 {
     auto* this_object = typed_this(vm, global_object);

+ 1 - 0
Userland/Libraries/LibJS/Runtime/DatePrototype.h

@@ -42,6 +42,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(get_date);
     JS_DECLARE_NATIVE_FUNCTION(get_day);
     JS_DECLARE_NATIVE_FUNCTION(get_full_year);
+    JS_DECLARE_NATIVE_FUNCTION(set_full_year);
     JS_DECLARE_NATIVE_FUNCTION(get_hours);
     JS_DECLARE_NATIVE_FUNCTION(get_milliseconds);
     JS_DECLARE_NATIVE_FUNCTION(get_minutes);