Repository navigation
Write a JavaScript long literal past the safe-integer range as a BigInt [patch] - #213
Merged
Merged
Conversation
…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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #168
What changed
A JavaScript number is a double, so a
longliteral larger in magnitude thanNumber.MAX_SAFE_INTEGER(2^53 − 1) was rounded when parsed.9007199254740993read back as9007199254740992, with no error.LanguageGeneratorBasegains aFormatLong(long)hook.TryGenerateCommonNodenow writesLiteralExpression<long>through it, and by default it writes the decimal digits as before.JavaScriptGeneratoroverrides 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 aTypeErrorinstead of quietly computing with a different number.Lspelling, 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.MaxValueandlong.MinValue.OtherTargets_AreUnchanged(Python, Go, Rust, C++, C): these targets still write the bare digits.JavaScript_KeepsTheExactValue: generates alongdeclaration for each value, runs it under Node, and checks thatString(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