Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

TryGenerateCommonNode writes a LiteralExpression<long> as its decimal digits, and JavaScriptGenerator does not override that:

case LiteralExpression<long> longLit:
code.Write(longLit.Value.ToString(CultureInfo.InvariantCulture));
return true;

JavaScript numbers are IEEE doubles, so any integer larger in magnitude than Number.MAX_SAFE_INTEGER is rounded when it is parsed.

Repro

new VariableDeclaration("big", "long", new LiteralExpression<long>(9007199254740993L)) generates:

let big = 9007199254740993;

Node 22 prints 9007199254740992.

The other six targets keep the exact value:

  • C# emits …993L.
  • Rust, Go, C and C++ use 64-bit integer types.
  • Python's int has arbitrary precision.

Why it matters

This silently corrupts data rather than failing to compile. It affects 64-bit IDs, hashes, nanosecond timestamps and bit masks. The AST says long, but the JavaScript output computes with a different number.

Suggested fix / acceptance criteria

  • In JavaScriptGenerator, write a long literal whose magnitude is above 9007199254740991 as a BigInt literal, e.g. 9007199254740993n. The value stays exact, and mixing a BigInt with Number arithmetic throws a TypeError instead of silently giving the wrong answer.
    • Alternative: keep the plain literal but add a note, following the repo's convention of stating a limitation rather than dropping it silently.
  • Literals within the safe range stay unchanged.
  • Add a test that runs the generated output under Node and checks the exact value.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. The generated JavaScript compiles and runs, but silently changes the value of any long literal outside ±(2^53−1). That corrupts IDs, hashes and bit masks with no error.
    • Area / suggested owner: JavaScriptGenerator literal emission (override of TryGenerateCommonNode for LiteralExpression<long>).
    • Duplicates / in progress: No duplicate found. No open PR covers it. Coder#124 maps long to int for Python, which is a different target.
    • Next step: Emit a BigInt literal (…n) when the magnitude exceeds Number.MAX_SAFE_INTEGER, and add a Node-executed test that checks the exact value.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions