2021-12-06 12:26:49 -05:00
|
|
|
/*
|
2024-11-27 16:09:41 -05:00
|
|
|
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@ladybird.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>
|
2023-10-06 17:54:21 +02:00
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2021-12-06 12:26:49 -05:00
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(DateTimeFormatFunction);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
2023-07-21 22:13:44 -04:00
|
|
|
// 11.5.4 DateTime Format Functions, https://tc39.es/ecma402/#sec-datetime-format-functions
|
2024-11-27 16:09:41 -05:00
|
|
|
// 15.9.3 DateTime Format Functions, https://tc39.es/proposal-temporal/#sec-datetime-format-functions
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<DateTimeFormatFunction> DateTimeFormatFunction::create(Realm& realm, DateTimeFormat& date_time_format)
|
2021-12-06 12:26:49 -05:00
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<DateTimeFormatFunction>(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-08-07 08:41:28 +02:00
|
|
|
void DateTimeFormatFunction::initialize(Realm& realm)
|
2021-12-06 12:26:49 -05:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
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);
|
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
|
|
|
|
2024-11-27 16:09:41 -05:00
|
|
|
auto date_value = vm.argument(0);
|
2021-12-06 12:26:49 -05:00
|
|
|
|
|
|
|
// 1. Let dtf be F.[[DateTimeFormat]].
|
|
|
|
// 2. Assert: Type(dtf) is Object and dtf has an [[InitializedDateTimeFormat]] internal slot.
|
|
|
|
|
2024-11-27 16:09:41 -05:00
|
|
|
FormattableDateTime date { 0 };
|
2022-05-06 20:45:25 +02:00
|
|
|
|
2021-12-06 12:26:49 -05:00
|
|
|
// 3. If date is not provided or is undefined, then
|
2024-11-27 16:09:41 -05:00
|
|
|
if (date_value.is_undefined()) {
|
2022-03-28 08:30:48 -04:00
|
|
|
// a. Let x be ! Call(%Date.now%, undefined).
|
2024-11-27 16:09:41 -05:00
|
|
|
date = 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 {
|
2024-11-27 16:09:41 -05:00
|
|
|
// a. Let x be ? ToDateTimeFormattable(date).
|
|
|
|
date = TRY(to_date_time_formattable(vm, date_value));
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Return ? FormatDateTime(dtf, x).
|
2024-11-27 16:09:41 -05:00
|
|
|
auto formatted = TRY(format_date_time(vm, m_date_time_format, date));
|
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);
|
2023-02-26 16:09:02 -07:00
|
|
|
visitor.visit(m_date_time_format);
|
2021-12-06 12:26:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|