Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
use core::cell::Cell;
use core::ops::Deref;
use libjs_runtime_macros::Trace;
use crate::gc::class::{GcCell, cast_ref, define_cell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::function_object::FunctionObject;
use crate::runtime::intl::number_format::{NumberFormat, format_numeric, to_intl_mathematical_value};
use crate::runtime::native_function::{
NATIVE_FUNCTION_METHODS, NATIVE_FUNCTION_VIRTUAL_METHODS, NativeFunction, NativeFunctionMethods,
};
use crate::runtime::object::ObjectMethods;
use crate::runtime::primitive_string::PrimitiveString;
use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
use crate::runtime::realm::Realm;
#[repr(C)]
#[derive(Trace)]
pub struct NumberFormatFunction {
base: NativeFunction,
number_format: Cell<Gc<NumberFormat>>, // [[NumberFormat]]
}
static NUMBER_FORMAT_FUNCTION_VIRTUAL_METHODS: NativeFunctionMethods = NativeFunctionMethods {
call: NumberFormatFunction::call,
..NATIVE_FUNCTION_VIRTUAL_METHODS
};
static NUMBER_FORMAT_FUNCTION_METHODS: ObjectMethods = ObjectMethods {
initialize: NumberFormatFunction::initialize,
native_function: Some(&NUMBER_FORMAT_FUNCTION_VIRTUAL_METHODS),
..NATIVE_FUNCTION_METHODS
};
define_cell!(
NumberFormatFunction,
Object,
extends: [NativeFunction, FunctionObject, Object],
methods: NUMBER_FORMAT_FUNCTION_METHODS
);
impl Deref for NumberFormatFunction {
type Target = NativeFunction;
fn deref(&self) -> &NativeFunction {
&self.base
}
}
impl NumberFormatFunction {
// 16.5.2 Number Format Functions, https://tc39.es/ecma402/#sec-number-format-functions
pub fn create(vm: &Vm, realm: Gc<Realm>, number_format: Gc<NumberFormat>) -> Gc<NumberFormatFunction> {
realm.create_object(vm, |_| NumberFormatFunction {
base: NativeFunction::new_with_prototype(vm, Self::CLASS, realm, realm.function_prototype(), None),
number_format: Cell::new(number_format),
})
}
fn initialize(object: &Object, vm: &Vm, realm: Gc<Realm>) {
(NATIVE_FUNCTION_METHODS.initialize)(object, vm, realm);
object.define_direct_property(
vm,
&vm.names.length,
Value::from_i32(1),
PropertyAttributes::new(Attribute::CONFIGURABLE),
);
object.define_direct_property(
vm,
&vm.names.name,
Value::from_string(vm.empty_string()),
PropertyAttributes::new(Attribute::CONFIGURABLE),
);
}
fn call(function: &NativeFunction, vm: &Vm) -> ThrowCompletionOr<Value> {
let function: &NumberFormatFunction = cast_ref(function);
// 1. Let nf be F.[[NumberFormat]].
// 2. Assert: Type(nf) is Object and nf has an [[InitializedNumberFormat]] internal slot.
let number_format = function.number_format.get();
// 3. If value is not provided, let value be undefined.
let value = vm.argument(0);
// 4. Let x be ? ToIntlMathematicalValue(value).
let mathematical_value = to_intl_mathematical_value(vm, value)?;
// 5. Return ? FormatNumeric(nf, x).
let formatted = format_numeric(&number_format, &mathematical_value);
Ok(Value::from_string(PrimitiveString::create(vm, formatted)))
}
}