fix(runtime): Number("0x…") wider than u64 rounds to f64 instead of NaN#6123
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughModifies ChangesNon-decimal number parsing overflow handling
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/perry-runtime/src/builtins/numbers.rs`:
- Around line 441-444: The non-decimal parsing loop in numbers::parse_float
currently accumulates into an f64 per digit with value = value * radix_f + ...,
which can round incorrectly for long base-2/8/16 literals. Update the conversion
path in the digits handling logic to preserve exact integer-to-f64 behavior,
either by using an exact conversion path or by tracking guard/round/sticky
information instead of repeated f64 accumulation. Add boundary tests around the
relevant parsing function to cover long non-decimal literals and ensure they
match exact conversion results.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 85bcfa05-35e7-471c-9e4b-553dc45e97fd
📒 Files selected for processing (2)
crates/perry-runtime/src/builtins/numbers.rstest-files/test_gap_number_nondecimal_overflow.ts
… of NaN
The non-decimal literal path used `u64::from_str_radix`, which overflows (→
NaN) once the value exceeds u64::MAX — but ECMAScript's NonDecimalIntegerLiteral
has no width limit, so `Number("0xFFFFFFFFFFFFFFFFF")` must round to the nearest
double (node: 295147905179352830000), not NaN.
Convert with a correctly-rounded (round-to-nearest, ties-to-even) bit
accumulation: collect significant bits into a u128, fold anything that overflows
into a sticky bit, then round — a naive `value*radix + digit` in f64 rounds at
every step and mis-rounds past 2^53 (e.g. 0x3fffffffffffff8). Verified the exact
doubles match node via `===` for 2^53 tie boundaries, 2^58, and >f64::MAX →
Infinity. Adds test_gap_number_nondecimal_overflow.
9ce27d4 to
3a4b1b2
Compare
|
Good catch — addressed. The parse is now correctly-rounded: I replaced the naive f64 accumulation with a round-to-nearest, ties-to-even conversion that accumulates significant bits into a Verified the exact doubles match node via Note on your script's decimal diff: the value your cases produce is already the correct double (e.g. |
Another #6079 residual.
Problem
Number("0x…"/"0o…"/"0b…")usedu64::from_str_radix, which returnsErr(→NaN) once the value exceedsu64::MAX. But ECMAScript'sNonDecimalIntegerLiteralhas no width limit, so oversized literals must round to the nearest double:Fix
Accumulate the digits directly in
f64(value = value * radix + digit), matching ToNumber rounding. Out-of-radix digits ("0b12") and empty bodies ("0x") still yieldNaN.Test
test-files/test_gap_number_nondecimal_overflow.ts— overflow values, small values, error cases, and uppercase prefixes, all byte-matched vsnode --experimental-strip-types.Summary by CodeRabbit
Number(...)handling for non-decimal (hexadecimal, octal, binary) numeric literals, including very large magnitudes.Infinityoverflow) instead of returningNaN.0x/0b/0o) still returnNaN.