Skip to main content

shape_jit/ffi/call_method/
number.rs

1// Heap allocation audit (PR-9 V8 Gap Closure):
2//   Category A (NaN-boxed returns): 2 sites
3//     jit_box(HK_STRING, ...) — toFixed, toString
4//   Category B (intermediate/consumed): 0 sites
5//   Category C (heap islands): 0 sites
6//!
7//! Number method implementations for JIT
8
9use crate::nan_boxing::*;
10
11/// Call a method on a number value
12#[inline(always)]
13pub fn call_number_method(receiver_bits: u64, method_name: &str, args: &[u64]) -> u64 {
14    let num = unbox_number(receiver_bits);
15    match method_name {
16        "abs" => box_number(num.abs()),
17        "floor" => box_number(num.floor()),
18        "ceil" => box_number(num.ceil()),
19        "round" => box_number(num.round()),
20        "sqrt" => box_number(num.sqrt()),
21        "toFixed" | "to_fixed" => {
22            let precision = if !args.is_empty() && is_number(args[0]) {
23                unbox_number(args[0]) as usize
24            } else {
25                2
26            };
27            let formatted = format!("{:.prec$}", num, prec = precision);
28            jit_box(HK_STRING, formatted)
29        }
30        "toString" | "to_string" => {
31            let s = format!("{}", num);
32            jit_box(HK_STRING, s)
33        }
34        _ => TAG_NULL,
35    }
36}