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