2021-11-10 14:34:14 -03:00
|
|
|
/*
|
2025-02-28 10:54:42 -03:00
|
|
|
* Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org>
|
2021-11-10 14:34:14 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
#include <LibJS/Runtime/Intl/NumberFormat.h>
|
|
|
|
|
#include <LibJS/Runtime/Intl/NumberFormatFunction.h>
|
|
|
|
|
|
|
|
|
|
namespace JS::Intl {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(NumberFormatFunction);
|
2023-12-23 11:15:27 -03:00
|
|
|
|
2025-02-28 10:54:42 -03:00
|
|
|
// 16.5.2 Number Format Functions, https://tc39.es/ecma402/#sec-number-format-functions
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<NumberFormatFunction> NumberFormatFunction::create(Realm& realm, NumberFormat& number_format)
|
2021-11-10 14:34:14 -03:00
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
return realm.create<NumberFormatFunction>(number_format, realm.intrinsics().function_prototype());
|
2021-11-10 14:34:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NumberFormatFunction::NumberFormatFunction(NumberFormat& number_format, Object& prototype)
|
|
|
|
|
: NativeFunction(prototype)
|
|
|
|
|
, m_number_format(number_format)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void NumberFormatFunction::initialize(Realm& realm)
|
2021-11-10 14:34:14 -03:00
|
|
|
{
|
2021-11-14 12:10:44 -03:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm);
|
2021-11-14 12:10:44 -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-11-10 14:34:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThrowCompletionOr<Value> NumberFormatFunction::call()
|
|
|
|
|
{
|
2022-08-20 04:25:24 -03:00
|
|
|
auto& vm = this->vm();
|
2021-11-10 14:34:14 -03:00
|
|
|
|
|
|
|
|
// 1. Let nf be F.[[NumberFormat]].
|
|
|
|
|
// 2. Assert: Type(nf) is Object and nf has an [[InitializedNumberFormat]] internal slot.
|
|
|
|
|
// 3. If value is not provided, let value be undefined.
|
|
|
|
|
auto value = vm.argument(0);
|
|
|
|
|
|
2022-07-19 13:11:34 -03:00
|
|
|
// 4. Let x be ? ToIntlMathematicalValue(value).
|
2022-08-20 04:25:24 -03:00
|
|
|
auto mathematical_value = TRY(to_intl_mathematical_value(vm, value));
|
2021-11-10 14:34:14 -03:00
|
|
|
|
|
|
|
|
// 5. Return ? FormatNumeric(nf, x).
|
2024-06-09 15:36:48 -03:00
|
|
|
auto formatted = format_numeric(m_number_format, move(mathematical_value));
|
2022-12-06 19:17:27 -03:00
|
|
|
return PrimitiveString::create(vm, move(formatted));
|
2021-11-10 14:34:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NumberFormatFunction::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-02-26 20:09:02 -03:00
|
|
|
visitor.visit(m_number_format);
|
2021-11-10 14:34:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|