Skip to content

Write a JavaScript long literal past the safe-integer range as a BigInt [patch] - #213

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/168-js-bigint-long
Oct 13, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/168-js-bigint-long

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #168

What changed

A JavaScript number is a double, so a long literal larger in magnitude than Number.MAX_SAFE_INTEGER (2^53 − 1) was rounded when parsed. 9007199254740993 read back as 9007199254740992, with no error.

  • LanguageGeneratorBase gains a FormatLong(long) hook. TryGenerateCommonNode now writes LiteralExpression<long> through it, and by default it writes the decimal digits as before.
  • JavaScriptGenerator overrides the hook. Any value outside ±(2^53 − 1) gets the BigInt suffix (9007199254740993n), so the value stays exact. If it is later mixed with number arithmetic, JavaScript throws a TypeError instead of quietly computing with a different number.
  • Literals inside the safe range stay plain numbers. C#, which has its own L spelling, is untouched, and so are Python, Go, Rust, C and C++.

Tests (LongLiteralTests)

  • JavaScript_SpellsAnUnsafeLongAsABigInt: checks both edges of the safe range, one past each edge, long.MaxValue and long.MinValue.
  • OtherTargets_AreUnchanged (Python, Go, Rust, C++, C): these targets still write the bare digits.
  • JavaScript_KeepsTheExactValue: generates a long declaration for each value, runs it under Node, and checks that String(big) gives back the exact digits.

With the generator change reverted, 5 of the 13 fail. The 8 that still pass are the in-range rows and the other-target rows. With the change, the full suite passes (1166/1166).

🤖 Generated with Claude Code

https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw


Generated by Claude Code

…nt [patch]

A JavaScript number is a double, so a long literal larger in magnitude than
Number.MAX_SAFE_INTEGER was rounded when parsed: 9007199254740993 read back as
9007199254740992. LanguageGeneratorBase gains a FormatLong hook, and
JavaScriptGenerator writes such a value with the BigInt suffix. Literals in
the safe range, and every other target, are unchanged.

Fixes #168

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 8f1d6ef into main Oct 13, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/168-js-bigint-long branch October 13, 2026 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavaScript: a long literal outside ±(2^53−1) is written as a plain Number and silently changes value

1 participant