/* * Copyright (c) 2021-2022, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace JS::Intl { // 11.5.5 DateTime Format Functions, https://tc39.es/ecma402/#sec-datetime-format-functions DateTimeFormatFunction* DateTimeFormatFunction::create(GlobalObject& global_object, DateTimeFormat& date_time_format) { return global_object.heap().allocate(global_object, date_time_format, *global_object.function_prototype()); } DateTimeFormatFunction::DateTimeFormatFunction(DateTimeFormat& date_time_format, Object& prototype) : NativeFunction(prototype) , m_date_time_format(date_time_format) { } void DateTimeFormatFunction::initialize(GlobalObject& global_object) { auto& vm = this->vm(); Base::initialize(global_object); define_direct_property(vm.names.length, Value(1), Attribute::Configurable); define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable); } ThrowCompletionOr DateTimeFormatFunction::call() { auto& global_object = this->global_object(); auto& vm = global_object.vm(); auto date = vm.argument(0); // 1. Let dtf be F.[[DateTimeFormat]]. // 2. Assert: Type(dtf) is Object and dtf has an [[InitializedDateTimeFormat]] internal slot. double date_value; // 3. If date is not provided or is undefined, then if (date.is_undefined()) { // a. Let x be ! Call(%Date.now%, undefined). date_value = MUST(JS::call(global_object, global_object.date_constructor_now_function(), js_undefined())).as_double(); } // 4. Else, else { // a. Let x be ? ToNumber(date). date_value = TRY(date.to_number(global_object)).as_double(); } // 5. Return ? FormatDateTime(dtf, x). auto formatted = TRY(format_date_time(global_object, m_date_time_format, date_value)); return js_string(vm, move(formatted)); } void DateTimeFormatFunction::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(&m_date_time_format); } }