From a93b1c7ea04976dd8d4ce0d260042b42766bf678 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sun, 6 Jun 2021 16:26:29 +0300 Subject: [PATCH] LibJS: Add Date.prototype.setMonth() --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../Libraries/LibJS/Runtime/DatePrototype.cpp | 35 ++++++++ .../Libraries/LibJS/Runtime/DatePrototype.h | 1 + .../builtins/Date/Date.prototype.setMonth.js | 87 +++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setMonth.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 6b4ddd987b..c5910cbaff 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -220,6 +220,7 @@ namespace JS { P(setHours) \ P(setMilliseconds) \ P(setMinutes) \ + P(setMonth) \ P(setPrototypeOf) \ P(setSeconds) \ P(setYear) \ diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp index 2b59e36945..3acf65c054 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.cpp @@ -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); diff --git a/Userland/Libraries/LibJS/Runtime/DatePrototype.h b/Userland/Libraries/LibJS/Runtime/DatePrototype.h index 496d90907a..c994e93b95 100644 --- a/Userland/Libraries/LibJS/Runtime/DatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/DatePrototype.h @@ -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); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setMonth.js b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setMonth.js new file mode 100644 index 0000000000..a7ffee3c01 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Date/Date.prototype.setMonth.js @@ -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); +});