From 44926b3f8093334166fab3f29f2b31af69c17b6f Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sun, 6 Jun 2021 15:42:19 +0300 Subject: [PATCH] LibJS: Ignore arguments in Date's constructor when called as a function Since theres no way to drop the arguments before the call to the constructor (or to signal to the constructor that it was not called directly), we simply reuse the code for the no arguments provided special case. (And to prevent code duplication, the code was extracted into the separate static function Date::now(GlobalObject&). --- Userland/Libraries/LibJS/Runtime/Date.cpp | 11 +++++++++++ Userland/Libraries/LibJS/Runtime/Date.h | 1 + .../Libraries/LibJS/Runtime/DateConstructor.cpp | 14 +++----------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index c96addb14e..39a07cdcc6 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include namespace JS { @@ -17,6 +19,15 @@ Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 mil return global_object.heap().allocate(global_object, datetime, milliseconds, is_invalid, *global_object.date_prototype()); } +Date* Date::now(GlobalObject& global_object) +{ + struct timeval tv; + gettimeofday(&tv, nullptr); + auto datetime = Core::DateTime::now(); + auto milliseconds = static_cast(tv.tv_usec / 1000); + return create(global_object, datetime, milliseconds); +} + Date::Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype) : Object(prototype) , m_datetime(datetime) diff --git a/Userland/Libraries/LibJS/Runtime/Date.h b/Userland/Libraries/LibJS/Runtime/Date.h index fd17c68481..fc75f2d280 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.h +++ b/Userland/Libraries/LibJS/Runtime/Date.h @@ -16,6 +16,7 @@ class Date final : public Object { public: static Date* create(GlobalObject&, Core::DateTime, u16 milliseconds, bool is_invalid = false); + static Date* now(GlobalObject&); Date(Core::DateTime datetime, u16 milliseconds, bool is_invalid, Object& prototype); virtual ~Date() override; diff --git a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp index 9d21a7907f..33c2bf5259 100644 --- a/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -141,22 +141,14 @@ DateConstructor::~DateConstructor() Value DateConstructor::call() { - auto date = construct(*this); - if (!date.is_object()) - return {}; - return js_string(heap(), static_cast(date.as_object()).string()); + return js_string(heap(), Date::now(global_object())->string()); } Value DateConstructor::construct(Function&) { auto& vm = this->vm(); - if (vm.argument_count() == 0) { - struct timeval tv; - gettimeofday(&tv, nullptr); - auto datetime = Core::DateTime::now(); - auto milliseconds = static_cast(tv.tv_usec / 1000); - return Date::create(global_object(), datetime, milliseconds); - } + if (vm.argument_count() == 0) + return Date::now(global_object()); auto create_invalid_date = [this]() { auto datetime = Core::DateTime::create(1970, 1, 1, 0, 0, 0);