fix(date): ECMAScript ToNumber for Date arguments (Symbol/BigInt throw, single valueOf)#4393
Merged
Conversation
…w, single valueOf)
Date argument coercion (the `new Date(v)` numeric path, `Date.UTC`, the
multi-arg `new Date(y,m,d,...)` constructor, and `Date.prototype.set*`
components) read raw NaN-boxed bits for any non-string/non-sentinel value,
so a Symbol/object/array/boolean produced garbage or an Invalid Date
instead of following ToNumber:
- `new Date(Symbol())` / `d.setHours(Symbol())` / `Date.UTC(2020, Symbol())`
now throw TypeError (Symbol and BigInt are not numerically convertible).
- objects run a single `valueOf`/`toString` via OrdinaryToPrimitive(number)
(`new Date(2020, {valueOf(){...}})` — valueOf invoked exactly once).
- booleans/null coerce numerically (`new Date(true)` → 1).
Routes the shared `jsvalue_to_number` helper, the constructor's numeric
branch, and the multi-arg constructor's `coerce` through one ToNumber that
handles Symbol/BigInt (throw), Date (time value), and object/array
(OrdinaryToPrimitive) before the primitive numeric coercion.
test262 built-ins/Date: 202 -> 158 (44 fixed, no regressions). Verified
Node-identical in both PERRY_NO_AUTO_OPTIMIZE and default builds.
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.
Summary
Date argument coercion bypassed ECMAScript
ToNumber: for any non-string / non-sentinel value the helpers read the raw NaN-boxed bits as a double, so aSymbol/object/array/boolean produced garbage or anInvalid Dateinstead of throwing or coercing.Before → after (vs
node --experimental-strip-types)Fix
Routes the shared
jsvalue_to_numberhelper, thenew Date(v)numeric branch, and the multi-argnew Date(y,m,d,…)coercethrough one ECMAScriptToNumber:TypeError(not numerically convertible);valueOf/toStringviaOrdinaryToPrimitive(number)(sovalueOfis invoked exactly once), then the primitive numeric coercion;All in
date.rs(no new file; under the line cap).Results
test262 built-ins/Date: 202 → 158 (44 fixed, no regressions), measured uncapped via
scripts/parity_gap_report.py. Verified Node-identical in bothPERRY_NO_AUTO_OPTIMIZE=1and default builds.cargo test -p perry-runtime --lib: 979 passed, 0 failed.The remaining Date failures are separate root causes (a follow-up will add
Date.prototypemethodthisbrand-checks, ~52 cases; plus arg-vs-date-value read ordering andtoISOStringRangeError edges).