Skip to content

Truly independent web browser

LibJS: Fold ** with the runtime's Number::exponentiate

Shannon Booth50e302385492
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
index f453f7d166f..c01a5004994 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
@@ -10319,18 +10319,6 @@ fn try_constant_fold_bigint_binary(
}
}
-/// Try to constant-fold a binary operation when both operands are constants.
-// 6.1.6.1.3 Number::exponentiate ( base, exponent )
-// https://tc39.es/ecma262/#sec-numeric-types-number-exponentiate
-// Rust's f64::powf follows C's pow() which returns 1.0 for pow(±1, ±∞),
-// but JS specifies NaN when abs(base) is 1 and exponent is ±∞.
-fn js_exponentiate(base: f64, exponent: f64) -> f64 {
- if exponent.is_infinite() && base.abs() == 1.0 {
- return f64::NAN;
- }
- base.powf(exponent)
-}
-
/// Parse a non-decimal integer string via BigInt to f64, matching
/// UnsignedBigInteger::from_base() + to_double(). This avoids u64 overflow
/// for large literals like 0x10000000000000000.
@@ -10453,6 +10441,7 @@ fn compare_bigint_and_number(bigint_str: &str, number: f64) -> Option<Option<std
Some(Some(bigint.cmp(&number_bigint)))
}
+/// Try to constant-fold a binary operation when both operands are constants.
fn try_constant_fold_binary(
generator: &mut Generator,
op: BinaryOp,
@@ -10508,7 +10497,7 @@ fn try_constant_fold_binary(
BinaryOp::Exponentiation => {
let a = constant_to_number(lhs_const)?;
let b = constant_to_number(rhs_const)?;
- Some(generator.add_constant_number(js_exponentiate(a, b)))
+ Some(generator.add_constant_number(crate::host::exponentiate(a, b)))
}
BinaryOp::StrictlyEquals | BinaryOp::StrictlyInequals => {
let equal = match (lhs_const, rhs_const) {
diff --git a/Libraries/LibJS/Rust/src/frontend_host.rs b/Libraries/LibJS/Rust/src/frontend_host.rs
index a535d99dced..a39f28e9e4c 100644
--- a/Libraries/LibJS/Rust/src/frontend_host.rs
+++ b/Libraries/LibJS/Rust/src/frontend_host.rs
@@ -11,6 +11,7 @@ use libregex_rust::regex::Regex;
use crate::runtime::regexp_object::parse_regex_pattern;
use crate::runtime::value::number_to_string;
+use crate::runtime::value_conversions;
/// Number::toString(value), which the frontend uses for numeric property names.
pub fn number_to_utf16(value: f64) -> Vec<u16> {
@@ -43,3 +44,8 @@ pub fn validate_regex(pattern: &[u16], flags: &[u16]) -> Result<(), String> {
.map_err(|error| format!("RegExp compile error: {error}"))?;
Ok(())
}
+
+/// Number::exponentiate(base, exponent), which the frontend uses to fold `**`.
+pub fn exponentiate(base: f64, exponent: f64) -> f64 {
+ value_conversions::exp_double(base, exponent)
+}
diff --git a/Libraries/LibJS/Rust/src/host.rs b/Libraries/LibJS/Rust/src/host.rs
index c9d33cf73a4..dbc3ab6b79c 100644
--- a/Libraries/LibJS/Rust/src/host.rs
+++ b/Libraries/LibJS/Rust/src/host.rs
@@ -27,3 +27,8 @@ pub fn js_number_to_utf16(value: f64) -> Utf16String {
pub fn validate_regex(pattern: &[u16], flags: &[u16]) -> Result<(), Utf16String> {
implementation::validate_regex(pattern, flags).map_err(|message| Utf16String(message.encode_utf16().collect()))
}
+
+/// Number::exponentiate(base, exponent), which the host implements.
+pub fn exponentiate(base: f64, exponent: f64) -> f64 {
+ implementation::exponentiate(base, exponent)
+}
diff --git a/Libraries/LibJS/Rust/src/test_host.rs b/Libraries/LibJS/Rust/src/test_host.rs
index ee7dbe940ec..6f6e15ab602 100644
--- a/Libraries/LibJS/Rust/src/test_host.rs
+++ b/Libraries/LibJS/Rust/src/test_host.rs
@@ -25,6 +25,10 @@ pub fn number_to_utf16(value: f64) -> Vec<u16> {
text.encode_utf16().collect()
}
+pub fn exponentiate(_base: f64, _exponent: f64) -> f64 {
+ unimplemented!("no cargo test folds constants")
+}
+
#[unsafe(no_mangle)]
extern "C" fn unicode_code_point_has_space_separator_general_category(code_point: u32) -> bool {
matches!(code_point, 0x1680 | 0x2000..=0x200a | 0x202f | 0x205f | 0x3000)
diff --git a/Tests/LibJS/Runtime/exponentiation-basic.js b/Tests/LibJS/Runtime/exponentiation-basic.js
index cba52519b26..396f471e7c2 100644
--- a/Tests/LibJS/Runtime/exponentiation-basic.js
+++ b/Tests/LibJS/Runtime/exponentiation-basic.js
@@ -52,6 +52,9 @@ test("exponentiation that produces NaN", () => {
expect(null ** undefined).toBeNaN();
expect(2 ** "foo").toBeNaN();
expect("foo" ** 2).toBeNaN();
+ expect(1 ** NaN).toBeNaN();
+ expect(1 ** undefined).toBeNaN();
+ expect(1 ** "foo").toBeNaN();
});
test("exponentiation with infinities", () => {