mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS: Add Date.prototype.{get, set}Year()
This commit is contained in:
parent
96ee5e36ed
commit
09233b9e41
Notes:
sideshowbarker
2024-07-18 17:11:28 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/09233b9e413 Pull-request: https://github.com/SerenityOS/serenity/pull/7571
3 changed files with 43 additions and 0 deletions
|
@ -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) \
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue