1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:57:44 +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

@ -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&>(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<u16>(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);