LibJS: Add Date.prototype.setMonth()

This commit is contained in:
Idan Horowitz 2021-06-06 16:26:29 +03:00 committed by Linus Groh
parent 59034554a4
commit a93b1c7ea0
4 changed files with 124 additions and 0 deletions

View file

@ -220,6 +220,7 @@ namespace JS {
P(setHours) \
P(setMilliseconds) \
P(setMinutes) \
P(setMonth) \
P(setPrototypeOf) \
P(setSeconds) \
P(setYear) \

View file

@ -52,6 +52,7 @@ void DatePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.getMinutes, get_minutes, 0, attr);
define_native_function(vm.names.setMinutes, set_minutes, 3, attr);
define_native_function(vm.names.getMonth, get_month, 0, attr);
define_native_function(vm.names.setMonth, set_month, 2, attr);
define_native_function(vm.names.getSeconds, get_seconds, 0, attr);
define_native_function(vm.names.setSeconds, set_seconds, 2, attr);
define_native_function(vm.names.getTime, get_time, 0, attr);
@ -409,6 +410,40 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_month)
return Value(this_object->month());
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_month)
{
auto* this_object = typed_this(vm, global_object);
if (!this_object)
return {};
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); };
auto& datetime = this_object->datetime();
auto new_month_value = vm.argument(0).to_number(global_object);
if (vm.exception())
return {};
if (!new_month_value.is_finite_number()) {
this_object->set_is_invalid(true);
return js_nan();
}
auto new_month = new_month_value.as_i32() + 1; // JS Months: 0 - 11, DateTime months: 1-12
auto new_date_value = arg_or(1, this_object->date());
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(), new_month, new_date, datetime.hour(), datetime.minute(), datetime.second());
return Value(this_object->time());
}
JS_DEFINE_NATIVE_FUNCTION(DatePrototype::get_seconds)
{
auto* this_object = typed_this(vm, global_object);

View file

@ -33,6 +33,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(get_minutes);
JS_DECLARE_NATIVE_FUNCTION(set_minutes);
JS_DECLARE_NATIVE_FUNCTION(get_month);
JS_DECLARE_NATIVE_FUNCTION(set_month);
JS_DECLARE_NATIVE_FUNCTION(get_seconds);
JS_DECLARE_NATIVE_FUNCTION(set_seconds);
JS_DECLARE_NATIVE_FUNCTION(get_time);

View file

@ -0,0 +1,87 @@
test("no arguments", () => {
let date = new Date(2021, 0, 1);
date.setMonth();
expect(date.getTime()).toBe(NaN);
});
test("NaN or undefined as only argument", () => {
let date = new Date(2021, 0, 1);
date.setMonth(NaN);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setMonth(undefined);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setMonth("a");
expect(date.getTime()).toBe(NaN);
});
test("Only month as argument", () => {
let date = new Date(2021, 0, 1);
date.setMonth(7);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(7);
expect(date.getDate()).toBe(1);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
expect(date.getSeconds()).toBe(0);
expect(date.getMilliseconds()).toBe(0);
});
test("Month and date as arguments", () => {
let date = new Date(2021, 0, 1);
date.setMonth(7, 3);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(7);
expect(date.getDate()).toBe(3);
expect(date.getHours()).toBe(0);
expect(date.getMinutes()).toBe(0);
expect(date.getSeconds()).toBe(0);
expect(date.getMilliseconds()).toBe(0);
});
test("NaN or undefined in any arguments", () => {
let date = new Date(2021, 0, 1);
date.setMonth(NaN, 3);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setMonth(2021, NaN);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setMonth(undefined, 3);
expect(date.getTime()).toBe(NaN);
date = new Date(2021, 0, 1);
date.setMonth(2021, undefined);
expect(date.getTime()).toBe(NaN);
date.setMonth(3, 16);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(3);
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);
});
test("Make Invalid Date valid again", () => {
let date = new Date(2021, 0, 1);
date.setMonth(NaN, 3, 16);
expect(date.getTime()).toBe(NaN);
date.setMonth(3, 16);
expect(date.getFullYear()).toBe(2021);
expect(date.getMonth()).toBe(3);
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);
});