1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:57:46 +00:00

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&).
This commit is contained in:
Idan Horowitz 2021-06-06 15:42:19 +03:00 committed by Linus Groh
parent ba9d3bc38c
commit 44926b3f80
3 changed files with 15 additions and 11 deletions

View file

@ -9,6 +9,8 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <sys/time.h>
#include <time.h>
namespace JS {
@ -17,6 +19,15 @@ Date* Date::create(GlobalObject& global_object, Core::DateTime datetime, u16 mil
return global_object.heap().allocate<Date>(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<u16>(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)