1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:17:45 +00:00

LibJS: Add Date.prototype.setDate()

This commit is contained in:
Idan Horowitz 2021-06-06 16:25:33 +03:00 committed by Linus Groh
parent 3eb05d9413
commit b893963651
4 changed files with 73 additions and 0 deletions

View file

@ -215,6 +215,7 @@ namespace JS {
P(round) \
P(seal) \
P(set) \
P(setDate) \
P(setFullYear) \
P(setHours) \
P(setMilliseconds) \

View file

@ -39,6 +39,7 @@ void DatePrototype::initialize(GlobalObject& global_object)
Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.getDate, get_date, 0, attr);
define_native_function(vm.names.setDate, set_date, 1, 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);
@ -102,6 +103,29 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_date)
return Value(this_object->date());
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_date)
{
auto* this_object = typed_this(vm, global_object);
if (!this_object)
return {};
auto& datetime = this_object->datetime();
auto new_date_value = vm.argument(0).to_number(global_object);
if (vm.exception())
return {};
if (!new_date_value.is_finite_number()) {
this_object->set_is_invalid(true);
return js_nan();
}
auto new_date = new_date_value.as_i32();
this_object->set_is_invalid(false);
datetime.set_time(datetime.year(), datetime.month(), new_date, datetime.hour(), datetime.minute(), datetime.second());
return Value(this_object->time());
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_day)
{
auto* this_object = typed_this(vm, global_object);

View file

@ -20,6 +20,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(get_date);
JS_DECLARE_NATIVE_FUNCTION(set_date);
JS_DECLARE_NATIVE_FUNCTION(get_day);
JS_DECLARE_NATIVE_FUNCTION(get_full_year);
JS_DECLARE_NATIVE_FUNCTION(set_full_year);

View file

@ -0,0 +1,47 @@
test("no arguments", () => {
let date = new Date(2021, 0, 1);
date.setDate();
expect(date.getTime()).toBe(NaN);
});
test("NaN or undefined as only argument", () => {
let date = new Date(2021, 0, 1);
date.setDate(NaN);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setDate(undefined);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setDate("a");
expect(date.getTime()).toBe(NaN);
});
test("Day as argument", () => {
let date = new Date(2021, 0, 1);
date.setDate(17);
expect(date.getDate()).toBe(17);
expect(date.getMonth()).toBe(0);
expect(date.getFullYear()).toBe(2021);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
expect(date.getSeconds()).toBe(0);
expect(date.getMilliseconds()).toBe(0);
});
test("Make Invalid Date valid again", () => {
let date = new Date(2021, 0, 1);
date.setDate(NaN);
expect(date.getTime()).toBe(NaN);
date.setDate(16);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(0);
expect(date.getDate()).toBe(16);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
expect(date.getSeconds()).toBe(0);
expect(date.getMilliseconds()).toBe(0);
});