Repository navigation
Decimal: literals, operators, structured clone and toJSON() - #1077
cirospaciari wants to merge 11 commits into
Conversation
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.
|
Preview build of 7958537: |
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Adds four things to
Decimalthat the TC39 draft does not have, each behind an option of its own. Stacked on #1004, which has the draft alone;--useDecimal=1stays just that.--useDecimal=1and--useDecimalStructuredClone=1toJSON()answers a string--useDecimalToJSON=11.5m--useDecimalLiterals=1--useDecimalOperators=1new Decimal("1.5").==compares values,===identity;**, bitwise and unary+throw. Compiled programs lack them. With the option,==of plain objects takes 5.5 ns for 1.cachedTypesFormatRevisionis 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
mainas it is; a squash makes it conflict.Testing
run-jsc-stress-tests --filter decimalandtestaot run --filter=decimalpass on Linux x64, Release and Debug.Generated by Claude Code