From df9a016781a5358abc3a501ca39602c2923e0137 Mon Sep 17 00:00:00 2001 From: Haled Odat Date: Thu, 4 May 2023 18:58:24 +0200 Subject: [PATCH] Fix `Number.prototype.toFixed()` --- Cargo.lock | 3 +-- boa_engine/Cargo.toml | 2 +- boa_engine/src/builtins/number/mod.rs | 18 +++++------------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce02ccdca6e..5b4f4351eab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3515,8 +3515,7 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "ryu-js" version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6518fc26bced4d53678a22d6e423e9d8716377def84545fe328236e3af070e7f" +source = "git+https://github.com/boa-dev/ryu-js.git?branch=feature/to-fixed#570d2502dcbbba9230fad3f90730f366cca90395" [[package]] name = "same-file" diff --git a/boa_engine/Cargo.toml b/boa_engine/Cargo.toml index 325ad5b896c..36c9d1e7194 100644 --- a/boa_engine/Cargo.toml +++ b/boa_engine/Cargo.toml @@ -63,7 +63,7 @@ num-bigint = { version = "0.4.3", features = ["serde"] } num-integer = "0.1.45" bitflags = "2.2.1" indexmap = "1.9.3" -ryu-js = "0.2.2" +ryu-js = { git = "https://github.com/boa-dev/ryu-js.git", branch = "feature/to-fixed" } chrono = { version = "0.4.24", default-features = false, features = ["clock", "std"] } fast-float = "0.2.0" once_cell = "1.17.1" diff --git a/boa_engine/src/builtins/number/mod.rs b/boa_engine/src/builtins/number/mod.rs index d87e2b2732b..44a789037ff 100644 --- a/boa_engine/src/builtins/number/mod.rs +++ b/boa_engine/src/builtins/number/mod.rs @@ -272,20 +272,12 @@ impl Number { .ok_or_else(|| { JsNativeError::range() .with_message("toFixed() digits argument must be between 0 and 100") - })? as usize; + })? as u8; - // 6. If x is not finite, return ! Number::toString(x). - if !this_num.is_finite() { - Ok(JsValue::new(Self::to_native_string(this_num))) - // 10. If x ≥ 10^21, then let m be ! ToString(𝔽(x)). - } else if this_num >= 1.0e21 { - Ok(JsValue::new(f64_to_exponential(this_num))) - } else { - // Get rid of the '-' sign for -0.0 because of 9. If x < 0, then set s to "-". - let this_num = if this_num == 0_f64 { 0_f64 } else { this_num }; - let this_fixed_num = format!("{this_num:.precision$}"); - Ok(JsValue::new(this_fixed_num)) - } + let mut buffer = ryu_js::Buffer::new(); + let string = buffer.format_to_fixed(this_num, precision); + + Ok(string.into()) } /// `Number.prototype.toLocaleString( [locales [, options]] )`