From 09233b9e4139baedb516659fa30876edcb7bf436 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 29 May 2021 23:52:17 +0300 Subject: [PATCH] LibJS: Add Date.prototype.{get, set}Year() --- .../LibJS/Runtime/CommonPropertyNames.h | 2 + .../Libraries/LibJS/Runtime/DatePrototype.cpp | 39 +++++++++++++++++++ .../Libraries/LibJS/Runtime/DatePrototype.h | 2 + 3 files changed, 43 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 8c4f3c4861f..552695acf89 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -133,6 +133,7 @@ namespace JS { P(getUTCMinutes) \ P(getUTCMonth) \ P(getUTCSeconds) \ + P(getYear) \ P(global) \ P(globalThis) \ P(groups) \ @@ -207,6 +208,7 @@ namespace JS { P(setMinutes) \ P(setPrototypeOf) \ P(setSeconds) \ + P(setYear) \ P(shift) \ P(sign) \ P(sin) \ diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 958b1b223bd..4bef4e13678 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -42,6 +42,8 @@ void DatePrototype::initialize(GlobalObject& global_object) 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.getYear, get_year, 0, attr); + define_native_function(vm.names.setYear, set_year, 1, attr); define_native_function(vm.names.getHours, get_hours, 0, attr); define_native_function(vm.names.setHours, set_hours, 4, attr); define_native_function(vm.names.getMilliseconds, get_milliseconds, 0, attr); @@ -161,6 +163,43 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year) return Value(this_object->time()); } +JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_year) +{ + auto* this_object = typed_this(vm, global_object); + if (!this_object) + return {}; + + if (this_object->is_invalid()) + return js_nan(); + + return Value(this_object->year() - 1900); +} + +JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_year) +{ + auto* this_object = typed_this(vm, global_object); + if (!this_object) + return {}; + + auto& datetime = this_object->datetime(); + + auto new_year_value = vm.argument(0).to_number(global_object); + if (vm.exception()) + return {}; + if (!new_year_value.is_finite_number()) { + this_object->set_is_invalid(true); + return js_nan(); + } + auto new_year = new_year_value.as_i32(); + if (new_year >= 0 && new_year <= 99) + new_year += 1900; + + datetime.set_time(new_year, datetime.month(), datetime.day(), datetime.hour(), datetime.minute(), datetime.second()); + this_object->set_is_invalid(false); + + return Value(this_object->time()); +} + JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_hours) { auto* this_object = typed_this(vm, global_object); diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.h b/Userland/Libraries/LibJS/Runtime/DatePrototype.h index e7fb64ce681..2ded6d79ad0 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.h @@ -23,6 +23,8 @@ private: 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_year); + JS_DECLARE_NATIVE_FUNCTION(set_year); JS_DECLARE_NATIVE_FUNCTION(get_hours); JS_DECLARE_NATIVE_FUNCTION(set_hours); JS_DECLARE_NATIVE_FUNCTION(get_milliseconds);