Skip to content

Decimal: literals, operators, structured clone and toJSON() - #1077

Open
cirospaciari wants to merge 11 commits into
claude/gallant-feynman-0m5ti5from
claude/gallant-feynman-0m5ti5-extensions
Open

cirospaciari wants to merge 11 commits into
claude/gallant-feynman-0m5ti5from
claude/gallant-feynman-0m5ti5-extensions

Conversation

@cirospaciari

@cirospaciari cirospaciari commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Adds four things to Decimal that the TC39 draft does not have, each behind an option of its own. Stacked on #1004, which has the draft alone; --useDecimal=1 stays just that.

On with --useDecimal=1 and In the draft
Structured clone --useDecimalStructuredClone=1 no steps
toJSON() answers a string --useDecimalToJSON=1 throws
Literals, 1.5m --useDecimalLiterals=1 none; future work
Operators --useDecimalOperators=1 throw; future work
  • A literal makes a new object each time, and is a constant to optimized and compiled code. Without a JIT it saves about 10 ns over new Decimal("1.5").
  • Operators take two Decimals. == compares values, === identity; **, bitwise and unary + throw. Compiled programs lack them. With the option, == of plain objects takes 5.5 ns for 1.
  • cachedTypesFormatRevision is 16. Cached bytecode is used only with the options for literals and operators that it was made with.

Merging

After the lower one. A merge commit there lets this be retargeted to main as it is; a squash makes it conflict.

Testing

  • Outside tests, the first four commits restore 2ee227a byte for byte, where all 86 CI jobs passed. The others add two options and fix two findings of the review.
  • run-jsc-stress-tests --filter decimal and testaot run --filter=decimal pass on Linux x64, Release and Debug.
  • The tests find all 22 deliberate mistakes in this code.
  • Not run on arm64 locally. That cloned bytes and cached bytecode are rejected under other options is tested only in Bun.

Generated by Claude Code

An exact copy, with identity kept, as for a BigInt object. Bytes with a Decimal
are rejected where useDecimal is off. The draft has no steps for it.
With --useDecimalToJSON=1 it writes the exact value. In the draft it throws.
1.5m makes a new object of the intrinsic %Decimal%. It is a call of a link-time
function, and a constant to the DFG, the FTL and compiled programs. The draft
has no literals.
With --useDecimalOperators=1 arithmetic and comparisons work on two Decimals,
and == compares them by value. In the draft they throw. Without the option the
bytecode is unchanged.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Preview build of 7958537: autobuild-preview-pr-1077-7958537a

It needs --useDecimalStructuredClone=1. Without it structuredClone() throws, and
bytes with a Decimal are rejected.
They need --useDecimalLiterals=1, so --useDecimal=1 alone is the draft and
nothing else.
@cirospaciari
cirospaciari marked this pull request as ready for review November 19, 2026 20:43

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the inline findings, I also checked the new DecimalLiteralCache packing in DecimalStringCache.h (the overlapping 32-bit loads plus the length check give a unique key for 1-8 Latin1 characters, and the cache is only reached from the mutator via DecimalPrototype.cpp:677, so no compiler-thread race) and the structured-clone path (serializer gated on canBeCloned, deserializer fails cleanly on unparsable text; a Decimal without the clone option falls to the existing plain-object path, as before this PR).

Extended reasoning...

The PR adds four option-gated Decimal extensions across the lexer, parser, bytecode generator, DFG parser, bytecode cache, structured clone and prototype, about 1850 lines over 49 files; it touches no auth, crypto or injection surface, though the new DecimalTag in serialized payloads and the cache-format revision bump change persisted-data compatibility. Two confirmed findings are posted inline, so human review is already signalled; this note only records what else was examined and ruled out.

-1.5m was NaN once a string of 16 bits with the same digits had been used as a
key, like one from toLocaleString().
…made with

Without the option a program started, and threw a SyntaxError where it parsed
what the cache lacks, like the code of a function for new.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-reviewed this PR after the latest commits and found no new bugs; because it adds a lexer token, new bytecode shapes, DFG intrinsics, a bytecode-cache version salt and a new structured-clone tag, a human look would still be worthwhile.

What was reviewed:

  • The latest commits address both earlier inline comments: currentCacheVersion() in CachedTypes.cpp now also salts with useDecimalLiterals, and DecimalNode::emitBytecode builds the negative literal via Identifier::fromString(makeString(...)) instead of span8().
  • Structured clone round trip: the serializer writes the exponential string only when canBeCloned(), the deserializer rejects DecimalTag without the option and fails on unparsable text; the tag value 192 sits away from upstream's range.
  • Host decimalOperator and DFG handleDecimalOperator agree on unordered (NaN) comparisons (== false, != true, ordered ops false), and DecimalLiteralCache::pack is injective per text length so cache hits cannot alias.
  • Ruled out: repeated == of a Decimal with a non-Decimal object exiting the DFG via CheckNotJSCast is a perf-only effect, not a correctness one.
Extended reasoning...

The change spans the JSC lexer/parser (DECIMALLITERAL token, DecimalNode), the bytecode generator (decimalLiteral/decimalOperator link-time constants and operator fallbacks behind useDecimalOperators), the DFG bytecode parser (constant-folding literals and lowering operator calls), the bytecode cache version, structured clone serializer/deserializer with a new tag, Decimal.prototype.toJSON, and about 870 lines of new tests, roughly 1860 lines total. It touches no auth, injection or data-exposure surface; the deserializer path is the closest, and it validates text through Decimal128::parse and refuses the tag without the option. The two findings from the prior review are addressed in the new commits, and no new bugs were found this run (dry streak), but the size and the number of engine tiers involved put it well outside what should be approved without a human reviewer.

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.

2 participants