2021-12-06 12:26:49 -05:00
|
|
|
/*
|
2022-03-15 10:30:33 -04:00
|
|
|
* Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
|
2021-12-06 12:26:49 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
|
|
#include <LibJS/Runtime/Date.h>
|
|
|
|
#include <LibJS/Runtime/DateConstructor.h>
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
|
|
|
#include <LibJS/Runtime/Intl/DateTimeFormatFunction.h>
|
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
2022-03-15 10:30:33 -04:00
|
|
|
// 11.5.5 DateTime Format Functions, https://tc39.es/ecma402/#sec-datetime-format-functions
|
2022-12-13 20:49:50 +00:00
|
|
|
NonnullGCPtr<DateTimeFormatFunction> DateTimeFormatFunction::create(Realm& realm, DateTimeFormat& date_time_format)
|
2021-12-06 12:26:49 -05:00
|
|
|
{
|
2022-12-14 17:40:33 +00:00
|
|
|
return realm.heap().allocate<DateTimeFormatFunction>(realm, date_time_format, *realm.intrinsics().function_prototype());
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DateTimeFormatFunction::DateTimeFormatFunction(DateTimeFormat& date_time_format, Object& prototype)
|
|
|
|
: NativeFunction(prototype)
|
|
|
|
, m_date_time_format(date_time_format)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
ThrowCompletionOr<void> DateTimeFormatFunction::initialize(Realm& realm)
|
2021-12-06 12:26:49 -05:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2021-12-06 12:26:49 -05:00
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2023-01-21 10:42:46 -05:00
|
|
|
define_direct_property(vm.names.name, PrimitiveString::create(vm, String {}), Attribute::Configurable);
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ThrowCompletionOr<Value> DateTimeFormatFunction::call()
|
|
|
|
{
|
2022-08-22 19:00:49 +01:00
|
|
|
auto& vm = this->vm();
|
|
|
|
auto& realm = *vm.current_realm();
|
2021-12-06 12:26:49 -05:00
|
|
|
|
|
|
|
auto date = vm.argument(0);
|
|
|
|
|
|
|
|
// 1. Let dtf be F.[[DateTimeFormat]].
|
|
|
|
// 2. Assert: Type(dtf) is Object and dtf has an [[InitializedDateTimeFormat]] internal slot.
|
|
|
|
|
2022-05-06 20:45:25 +02:00
|
|
|
double date_value;
|
|
|
|
|
2021-12-06 12:26:49 -05:00
|
|
|
// 3. If date is not provided or is undefined, then
|
|
|
|
if (date.is_undefined()) {
|
2022-03-28 08:30:48 -04:00
|
|
|
// a. Let x be ! Call(%Date.now%, undefined).
|
2022-08-27 00:54:55 +01:00
|
|
|
date_value = MUST(JS::call(vm, realm.intrinsics().date_constructor_now_function(), js_undefined())).as_double();
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
// 4. Else,
|
|
|
|
else {
|
|
|
|
// a. Let x be ? ToNumber(date).
|
2022-08-21 14:00:56 +01:00
|
|
|
date_value = TRY(date.to_number(vm)).as_double();
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Return ? FormatDateTime(dtf, x).
|
2022-08-20 08:25:24 +01:00
|
|
|
auto formatted = TRY(format_date_time(vm, m_date_time_format, date_value));
|
2022-12-06 22:17:27 +00:00
|
|
|
return PrimitiveString::create(vm, move(formatted));
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DateTimeFormatFunction::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(&m_date_time_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|